plugins/commands.lua

Tue, 22 Sep 2020 14:59:25 +0200

author
Seve Ferrer <seve@delape.net>
date
Tue, 22 Sep 2020 14:59:25 +0200
changeset 159
7ca948b157df
parent 102
b7c9a3707e68
permissions
-rw-r--r--

plugins/commands.lua: The default command_prefix value is set on config.docker.lua.

0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 function riddim.plugins.commands(bot)
159
7ca948b157df plugins/commands.lua: The default command_prefix value is set on config.docker.lua.
Seve Ferrer <seve@delape.net>
parents: 102
diff changeset
2 local command_pattern = "^%"..bot.config.command_prefix.."([%a%-%_%d]+)(%s?)(.*)$";
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
35
0f17e574b6bb plugins.commands: Enable commands to be directed to the bots nick
Kim Alvefur <zash@zash.se>
parents: 33
diff changeset
4 local direct_address_pattern = false;
0f17e574b6bb plugins.commands: Enable commands to be directed to the bots nick
Kim Alvefur <zash@zash.se>
parents: 33
diff changeset
5 if bot.config.nick then
0f17e574b6bb plugins.commands: Enable commands to be directed to the bots nick
Kim Alvefur <zash@zash.se>
parents: 33
diff changeset
6 direct_address_pattern = "^"..bot.config.nick.."[,: ]+([%a%-%_%d]+)(%s?)(.*)";
0f17e574b6bb plugins.commands: Enable commands to be directed to the bots nick
Kim Alvefur <zash@zash.se>
parents: 33
diff changeset
7 end
0f17e574b6bb plugins.commands: Enable commands to be directed to the bots nick
Kim Alvefur <zash@zash.se>
parents: 33
diff changeset
8
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local function process_command(event)
14
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 0
diff changeset
10 local body = event.body;
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 if not body then return; end
14
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 0
diff changeset
12 if event.delay then return; end -- Don't process old messages from groupchat
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local command, hasparam, param = body:match(command_pattern);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
35
0f17e574b6bb plugins.commands: Enable commands to be directed to the bots nick
Kim Alvefur <zash@zash.se>
parents: 33
diff changeset
16 if not command and direct_address_pattern then
0f17e574b6bb plugins.commands: Enable commands to be directed to the bots nick
Kim Alvefur <zash@zash.se>
parents: 33
diff changeset
17 command, hasparam, param = body:match(direct_address_pattern);
0f17e574b6bb plugins.commands: Enable commands to be directed to the bots nick
Kim Alvefur <zash@zash.se>
parents: 33
diff changeset
18 end
0f17e574b6bb plugins.commands: Enable commands to be directed to the bots nick
Kim Alvefur <zash@zash.se>
parents: 33
diff changeset
19
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 if not command then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 command, hasparam, param = body:match("%[([%a%-%_%d]+)(%s?)(.*)%]");
102
b7c9a3707e68 plugins.commands: If a message with a [command] was directed at someone, remember their nickname.
Kim Alvefur <zash@zash.se>
parents: 58
diff changeset
22 if event.room then
b7c9a3707e68 plugins.commands: If a message with a [command] was directed at someone, remember their nickname.
Kim Alvefur <zash@zash.se>
parents: 58
diff changeset
23 local direct_to = body:match"^(.-)[,:]"
b7c9a3707e68 plugins.commands: If a message with a [command] was directed at someone, remember their nickname.
Kim Alvefur <zash@zash.se>
parents: 58
diff changeset
24 if event.room.occupants[direct_to] then
b7c9a3707e68 plugins.commands: If a message with a [command] was directed at someone, remember their nickname.
Kim Alvefur <zash@zash.se>
parents: 58
diff changeset
25 event.reply_to = direct_to
b7c9a3707e68 plugins.commands: If a message with a [command] was directed at someone, remember their nickname.
Kim Alvefur <zash@zash.se>
parents: 58
diff changeset
26 end
b7c9a3707e68 plugins.commands: If a message with a [command] was directed at someone, remember their nickname.
Kim Alvefur <zash@zash.se>
parents: 58
diff changeset
27 end
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 if hasparam ~= " " then param = nil; end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if command then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local command_event = {
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
34 command = command,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
35 param = param,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
36 sender = event.sender,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
37 stanza = event.stanza,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
38 reply = event.reply,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
39 room = event.room, -- groupchat support
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
40 };
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local ret = bot:event("commands/"..command, command_event);
58
3e5b57d44fa0 plugin.commands: Fire unhandled-command event
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
42 if ret == nil then
3e5b57d44fa0 plugin.commands: Fire unhandled-command event
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
43 ret = bot:event("unhandled-command", command_event);
3e5b57d44fa0 plugin.commands: Fire unhandled-command event
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
44 end
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 if type(ret) == "string" then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 event:reply(ret);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return ret;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
14
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 0
diff changeset
52 -- Hook messages sent to bot, fire a command event on the bot
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 bot:hook("message", process_command);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
14
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 0
diff changeset
55 -- Support groupchat plugin: Hook messages from rooms that the bot joins
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 bot:hook("groupchat/joining", function (room)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 room:hook("message", process_command);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end

mercurial