plugins/slap.lua

Fri, 11 Jun 2010 01:11:09 +0100

author
Hubert Chathi <hubert@uhoreg.ca>
date
Fri, 11 Jun 2010 01:11:09 +0100
changeset 25
b1f95474104f
child 61
aca019044c51
permissions
-rw-r--r--

plugins.slap: Can't have a bot without this!

25
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
1 -- slap.lua
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
2
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
3 local st = require 'util.stanza'
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
4
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
5 function riddim.plugins.slap(bot)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
6 if type(bot.config.weapons) ~= 'table' then
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
7 -- start off with something to slap people with
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
8 bot.config.weapons = {'large trout'}
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
9 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
10
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
11 -- reply to message (but don't prepend the sender's nick like groupchat's
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
12 -- event:reply does)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
13 local function bare_reply(command, reply)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
14 if command.stanza.attr.type == 'groupchat' then
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
15 local r = st.reply(command.stanza)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
16 local room_jid = jid.bare(command.sender.jid);
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
17 if bot.rooms[room_jid] then
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
18 bot.rooms[room_jid]:send(r:tag("body"):text(reply));
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
19 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
20 else
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
21 return command:reply(reply);
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
22 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
23 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
24
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
25 -- slap someone
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
26 local function slap(command)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
27 local who, weapon
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
28 if command.param then
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
29 who = command.param
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
30 else
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
31 -- slap the sender if they don't specify a target
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
32 if bot.rooms[jid.bare(command.sender.jid)] then
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
33 who = select(3, jid.split(command.sender.jid))
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
34 else
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
35 who = (jid.split(command.sender.jid))
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
36 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
37 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
38 weapon = bot.config.weapons[math.random(#bot.config.weapons)]
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
39 bare_reply(command, string.format('/me slaps %s with %s', who, weapon))
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
40 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
41
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
42 -- pick up a weapon for slapping
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
43 local function weapon(command)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
44 if command.param then
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
45 if command.param:lower() == 'excalibur' then
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
46 command:reply 'Listen -- strange women lying in ponds distributing swords is no basis for a system of government. Supreme executive power derives from a mandate from the masses, not from some farcical aquatic ceremony.'
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
47 elseif command.param:lower() == 'paper' then
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
48 bare_reply(command, '"Reverse primary thrust, Marvin." That\'s what they say to me. "Open airlock number 3, Marvin." "Marvin, can you pick up that piece of paper?" Here I am, brain the size of a planet, and they ask me to pick up a piece of paper.')
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
49 else
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
50 table.insert(bot.config.weapons, command.param)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
51 bare_reply(command, '/me picks up '..command.param)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
52 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
53 else
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
54 command:reply 'Tell me what weapon to pick up'
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
55 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
56 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
57
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
58 -- drop a weapon
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
59 local function drop(command)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
60 if command.param then
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
61 local found
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
62 for i,v in ipairs(bot.config.weapons) do
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
63 local weapons = bot.config.weapons
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
64 if v == command.param then
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
65 if #weapons == 1 then
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
66 bare_reply(command, '/me refuses to drop his last weapon')
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
67 else
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
68 weapons[i] = weapons[#weapons]
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
69 table.remove(weapons)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
70 found = true
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
71 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
72 break
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
73 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
74 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
75 if found then
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
76 bare_reply(command, '/me drops '..command.param)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
77 else
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
78 bare_reply(command, "/me doesn't have "..command.param)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
79 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
80 else
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
81 command:reply 'Tell me what to drop'
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
82 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
83 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
84
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
85 bot:hook('commands/slap', slap)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
86 bot:hook('commands/weapon', weapon)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
87 bot:hook('commands/drop', drop)
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
88 end
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
89
b1f95474104f plugins.slap: Can't have a bot without this!
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
90 -- end of slap.lua

mercurial