plugins/commands.lua

Thu, 20 May 2010 14:33:41 +0100

author
Chris <jugg@hotmail.com>
date
Thu, 20 May 2010 14:33:41 +0100
changeset 16
ae69cea97598
parent 14
3df63aaba9e3
child 33
37caf7bd2021
permissions
-rw-r--r--

Formatting, indentation and cleanup

0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 require "util.xstanza"
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 function riddim.plugins.commands(bot)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local command_pattern = "^%"..(bot.config.command_prefix or "@").."([%a%-%_%d]+)(%s?)(.*)$";
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 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
7 local body = event.body;
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 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
9 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
10
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local command, hasparam, param = body:match(command_pattern);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 if not command then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 command, hasparam, param = body:match("%[([%a%-%_%d]+)(%s?)(.*)%]");
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 if hasparam ~= " " then param = nil; end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 if command then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local command_event = {
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
21 command = command,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
22 param = param,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
23 sender = event.sender,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
24 stanza = event.stanza,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
25 reply = event.reply,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
26 room = event.room, -- groupchat support
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
27 };
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local ret = bot:event("commands/"..command, command_event);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 if type(ret) == "string" then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 event:reply(ret);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 return ret;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
14
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 0
diff changeset
36 -- 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
37 bot:hook("message", process_command);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
14
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 0
diff changeset
39 -- 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
40 bot:hook("groupchat/joining", function (room)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 room:hook("message", process_command);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end

mercurial