This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
commands.coffee
executable file
·297 lines (216 loc) · 8.22 KB
/
commands.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# SauceBot Module: Commands
Sauce = require '../sauce'
db = require '../saucedb'
trig = require '../trigger'
vars = require '../vars'
io = require '../ioutil'
util = require 'util'
{ # Import DTO classes
ArrayDTO,
ConfigDTO,
HashDTO,
BucketDTO,
EnumDTO
} = require '../dto'
{Module} = require '../module'
# Module description
exports.name = 'Commands'
exports.version = '1.2'
exports.description = 'Custom commands handler'
exports.strings = {
# Errors
'err-usage' : 'Usage: @1@'
'err-only-forget-set': 'Only forgets commands made with @1@.'
'err-to-forget' : '@1@ or @2@ to forget a command.'
# Actions
'action-mod-set': 'Mod-command set: @1@'
'action-set' : 'Command set: @1@'
'action-unset' : 'Command unset: @1@'
}
io.module '[Commands] Init'
# Commands module
# - Handles:
# !set <command> <message>
# !setmod <command> <message>
# !unset <command> <message>
# !<command>
#
class Commands extends Module
constructor: (@channel) ->
super @channel
@commands = new BucketDTO @channel, 'commands', 'cmdtrigger', ['message', 'level']
@remotes = new BucketDTO @channel, 'remotefields', 'key', [ 'value', 'updatedby', 'updatetime' ]
@triggers = {}
load: ->
@regCmd "set" , Sauce.Level.Mod, @cmdSet
@regCmd "setmod" , Sauce.Level.Mod, @cmdSetMod
@regCmd "unset" , Sauce.Level.Mod, @cmdUnset
@regCmd "remotes", Sauce.Level.Owner, @cmdRemotes
@regCmd "setrem", Sauce.Level.Mod, @cmdSetRem
# Load custom commands
@commands.load =>
for cmd, d of @commands.data
@addTrigger cmd
# Register interface actions
@regActs {
# Commands.get()
'get': (user, params, res) =>
data = {}
for key, cmd of @commands.get()
data[key] = msg: cmd.message
data[key].lvl = cmd.level if cmd.level
res.send data
# Commands.set(key, val, lvl=0)
'set': (user, params, res) =>
{key, val, lvl} = params
unless key? and val?
return res.error "Missing attributes: key, val"
old = @commands.get key
@removeCommand key
@setCommand key, val, lvl ? Sauce.Level.User
@logEvent user, 'set', key, (old ? { }).message, val
res.ok()
# Commands.remove(key)
'remove': (user, params, res) =>
{key} = params
unless key?
return res.error "Missing attribute: key"
old = @commands.get key
@logEvent user, 'remove', key, (old ? { }).message
@removeCommand key
res.ok()
# Commands.clear()
'clear': (user, params, res) =>
@logEvent user, 'clear'
@clearCommands()
res.ok()
# REMOTES
# Commands.setremote(key, val)
'setremote': (user, params, res) =>
{key, val} = params
unless key?
return res.error "Missing attribute: key"
unless val?
@remotes.remove key
else
old = @remotes.get key
@logEvent user, 'setremote', key, (old ? {}).value, val
@remotes.add key, {
value: val
updatedby: user.id
updatetime: ~~(Date.now()/1000)
}
res.ok()
# Commands.getremotes()
'getremotes': (user, params, res) =>
res.send @remotes.get()
}
unload:->
@triggers = {}
addTrigger: (cmd) ->
# Do nothing if the user is just editing an existing command.
return if @triggers[cmd]?
level = @commands.get(cmd).level
# Create a simple trigger that looks up a key in @commands
@triggers[cmd] = trig.buildTrigger this, cmd, level,
(user, args, bot) =>
data = @commands.get cmd
unless data?
return io.error "No such command #{cmd}"
@channel.vars.parse user, data.message, (args.join ' '), (parsed) ->
bot.say parsed
@channel.register @triggers[cmd]
varcmd = "!" + cmd.toLowerCase()
@regVar varcmd, (user, args, cb) =>
cb @channel.vars.strip @commands.get(cmd).message
delTrigger: (cmd) ->
# Do nothing if the trigger doesn't exist.
return unless @triggers[cmd]?
@channel.unregister @triggers[cmd]
@channel.vars.unregister "!#{cmd.toLowerCase()}"
delete @triggers[cmd]
# !(un)?set <command> - Unset command
cmdUnset: (user, args, bot) =>
unless args[0]?
return bot.say @str('err-usage', '!unset <name>') + '. ' + @str('err-only-forget-set', '!set')
cmd = args[0]
a = @delCommandIgnoreCase cmd
b = @delTriggerIgnoreCase cmd
bot.say @str('action-unset', cmd) if a or b
# Removes a command (not case sensitive)
delCommandIgnoreCase: (cmd) ->
cmd = cmd.toLowerCase()
for k, v of @commands.get() when k.toLowerCase() is cmd
@commands.remove k
return true
return false
# Removes a trigger (not case sensitive)
delTriggerIgnoreCase: (cmd) ->
cmd = cmd.toLowerCase()
for k, v of @triggers when k.toLowerCase() is cmd
@delTrigger k
return true
return false
# !set <command> <message> - Set command
# !set <command> - Unset command
cmdSet: (user, args, bot) =>
unless args[0]?
return bot.say @str('err-usage', '!set <name> <message>') + '. ' + @str('err-to-forget', '!set <name>', '!unset <name>')
# !set <command>
if (args.length is 1)
return @cmdUnset user, args, bot
else
@cmdUnset user, args, { say: -> 0 }
cmd = (args.splice 0, 1)[0]
msg = args.join ' '
@setCommand cmd, msg, Sauce.Level.User
return bot.say @str('action-set', cmd)
# !setmod <command> <message> - Set moderator-only command
# !setmod <command> - Unset command
cmdSetMod: (user, args, bot) =>
unless args[0]?
return bot.say @str('err-usage', '!setmod <name> <message>') + '. ' + @str('err-to-forget', '!setmod <name>', '!unset <name>')
# !setmod <command>
if (args.length is 1)
return @cmdUnset user, args, bot
else
@cmdUnset user, args, { say: -> 0 }
cmd = (args.splice 0, 1)[0]
msg = args.join ' '
@setCommand cmd, msg, Sauce.Level.Mod
return bot.say @str('action-mod-set', cmd)
# !remotes - Shows remote fields
cmdRemotes: (user, args, bot) =>
bot.say "[Remotes] " + JSON.stringify(@remotes.get()).substring(0, 400)
# !setrem <key> <message> - Sets a remote
cmdSetRem: (user, args, bot) =>
unless args.length >= 2
return bot.say "Remotes set usage: !setrem <key> <message>"
key = (args.splice 0, 1)[0]
msg = args.join ' '
unless user.id?
return bot.say "Only moderators registered on the web interface may set remote commands."
@remotes.add key, {
value: msg
updatedby: user.id
updatetime: ~~(Date.now()/1000)
}
bot.say "Remote set."
setCommand: (cmd, msg, level) ->
# Make sure people don't accidentally set "!!ip" as a command
cmd = cmd.replace /^!/, ''
return unless cmd.length > 0
data =
message: msg
level : level
@commands.add cmd, data
@addTrigger cmd
removeCommand: (cmd) ->
@commands.remove cmd
@delTrigger cmd
clearCommands: ->
@commands.clear()
for cmd, _ of @triggers
@delTrigger cmd
exports.New = (channel) ->
new Commands channel