plugins/commands.lua

changeset 0
7d84f4403d67
child 14
3df63aaba9e3
equal deleted inserted replaced
-1:000000000000 0:7d84f4403d67
1 require "util.xstanza"
2
3 function riddim.plugins.commands(bot)
4 local function handle_message(message)
5 -- Parse message body
6 end
7
8 local command_pattern = "^%"..(bot.config.command_prefix or "@").."([%a%-%_%d]+)(%s?)(.*)$";
9
10 local function process_command(event)
11 local body, sender = event.body, event.sender;
12 if not body then return; end
13 if event.delay then return; end -- Don't process old messages
14
15 local command, hasparam, param = body:match(command_pattern);
16
17 if not command then
18 command, hasparam, param = body:match("%[([%a%-%_%d]+)(%s?)(.*)%]");
19 end
20
21 if hasparam ~= " " then param = nil; end
22
23 if command then
24 local command_event = {
25 command = command,
26 param = param,
27 sender = sender,
28 stanza = event.stanza,
29 reply = event.reply,
30 room = event.room,
31 };
32 local ret = bot:event("commands/"..command, command_event);
33 if type(ret) == "string" then
34 event:reply(ret);
35 end
36 return ret;
37 end
38 end
39
40 -- Hook messages to bot and from rooms, fire a command event on the bot
41 bot:hook("message", process_command);
42
43 bot:hook("groupchat/joining", function (room)
44 room:hook("message", process_command);
45 end);
46 end

mercurial