plugins/simple_commands.lua

changeset 26
f056ad661521
equal deleted inserted replaced
25:b1f95474104f 26:f056ad661521
1 -- simple_commands.lua
2
3
4
5 local st = require 'util.stanza'
6
7 function riddim.plugins.simple_commands(bot)
8 -- reply to message (but don't prepend the sender's nick like groupchat's
9 -- event:reply does)
10 local function bare_reply(command, reply)
11 if command.stanza.attr.type == 'groupchat' then
12 local r = st.reply(command.stanza)
13 local room_jid = jid.bare(command.sender.jid);
14 if bot.rooms[room_jid] then
15 bot.rooms[room_jid]:send(r:tag("body"):text(reply));
16 end
17 else
18 return command:reply(reply);
19 end
20 end
21
22 local function exec(command)
23 local reply = bot.config.simple_commands[command.command]
24 if type(reply) == 'table' then
25 reply = reply[math.random(#reply)]
26 end
27 if type(reply) == 'string' then
28 if reply:match('%%s') then
29 if command.param then
30 bare_reply(command, reply:format(command.param))
31 end
32 else
33 bare_reply(command, reply)
34 end
35 elseif type(reply) == 'function' then
36 bare_reply(command, reply(command.param))
37 end
38 end
39
40 for k,v in pairs(bot.config.simple_commands) do
41 bot:hook('commands/'..k, exec)
42 end
43 end
44
45 -- end of simple_commands.lua

mercurial