plugins/simple_commands.lua

Thu, 23 Mar 2023 09:54:45 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 23 Mar 2023 09:54:45 +0000
changeset 174
56316e345595
parent 26
f056ad661521
permissions
-rw-r--r--

squishy: Add missing servercontact plugin

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

mercurial