Skip to content
Navigation Menu
{{ message }}
forked from PhantomBot/PhantomBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquoteSystem.js
More file actions
398 lines (348 loc) · 15.3 KB
/
Copy pathquoteSystem.js
File metadata and controls
398 lines (348 loc) · 15.3 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
* Copyright (C) 2016-2026 phantombot.github.io/PhantomBot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* quoteSystem.js
*
* Have the bot remember the most epic/derpy oneliners
*/
(function() {
var quoteMode = $.getSetIniDbBoolean('settings', 'quoteMode', true),
isDeleting = false;
/**
* @function updateQuote
* @param {Number} quoteid
* @param {Array} quote data
*/
function updateQuote(quoteid, quote) {
// Specify String() for objects as they were being treated as an object rather than a String on stringify().
quote[0] = String(quote[0]).replace(/"/g, '\'\'');
quote[1] = String(quote[1]).replace(/"/g, '\'\'');
quote[3] = String(quote[3]).replace(/"/g, '\'\'');
$.inidb.set('quotes', quoteid, JSON.stringify([String(quote[0]), String(quote[1]), String(quote[2]), String(quote[3])]));
$.panelsocketserver.sendJSONToAll(JSON.stringify({"query_id": "quote_update", "results": "update"}));
}
/**
* @function saveQuote
* @param {string} username
* @param {string} quote
* @returns {Number}
*/
function saveQuote(username, quote) {
var newKey = $.inidb.GetKeyList('quotes', '').length,
game = (!$.javaString($.getGame($.channelName)).isBlank() ? $.getGame($.channelName) : "Some Game");
if ($.inidb.exists('quotes', newKey)) {
newKey++;
}
quote = String(quote).replace(/"/g, '\'\'');
$.inidb.set('quotes', newKey, JSON.stringify([username, quote, $.systemTime(), game + '']));
$.panelsocketserver.sendJSONToAll(JSON.stringify({"query_id": "quote_update", "results": "update"}));
return newKey;
}
/**
* @function deleteQuote
* @param {Number} quoteId
* @returns {Number}
*/
function deleteQuote(quoteId) {
var quoteKeys,
quotes = [],
i;
if (isDeleting) {
return -1;
}
if ($.inidb.exists('quotes', quoteId)) {
isDeleting = true;
$.inidb.del('quotes', quoteId);
quoteKeys = $.inidb.GetKeyList('quotes', '');
for (i in quoteKeys) {
quotes.push($.getIniDbString('quotes', quoteKeys[i]));
$.inidb.del('quotes', quoteKeys[i]);
}
for (i in quotes) {
$.inidb.set('quotes', i, quotes[i]);
}
isDeleting = false;
$.panelsocketserver.sendJSONToAll(JSON.stringify({"query_id": "quote_update", "results": "update"}));
return (quotes.length ? quotes.length : 0);
}
return -1;
}
/**
* @function getQuote
* @param quoteId id or search query
* @returns {Array}
*/
function getQuote(quoteId) {
let quote;
if (!quoteId) {
quoteId = $.rand($.inidb.GetKeyList('quotes', '').length);
} else if (isNaN(quoteId)) {
quoteId = String(quoteId).toLowerCase();
var quotes = $.inidb.GetKeyValueList('quotes', '');
var ids = [];
for (var i = 0; i < quotes.length; i++) {
if (String(quotes[i].getValue()).toLowerCase().indexOf(quoteId) >= 0) {
ids.push(quotes[i].getKey());
}
}
quoteId = ids.length > 0 ? $.randElement(ids) : $.rand(quotes.length);
}
quote = $.optIniDbString('quotes', quoteId);
if (quote.isPresent()) {
quote = JSON.parse(quote.get());
quote.push(quoteId);
return quote;
}
return [];
}
/**
* @event command
* @param event
*/
$.bind('command', function(event) {
var sender = event.getSender(),
command = event.getCommand(),
args = event.getArgs(),
quote,
quoteStr;
/**
* @commandpath editquote [id] [user|game|quote] [text] - Edit quotes.
*/
if ($.equalsIgnoreCase(command, "editquote")) {
if (args.length < 3) {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.edit.usage'));
return;
}
quote = getQuote(args[0]);
if (quote.length > 0) {
if ($.equalsIgnoreCase(args[1], "user")) {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.edit.user.success', args[0], args[2]));
quote[0] = args[2];
updateQuote(args[0], quote);
} else if ($.equalsIgnoreCase(args[1], "game")) {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.edit.game.success', args[0], args.splice(2).join(' ')));
quote[3] = args.splice(2).join(' ');
updateQuote(args[0], quote);
} else if ($.equalsIgnoreCase(args[1], "quote")) {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.edit.quote.success', args[0], args.splice(2).join(' ')));
quote[1] = args.splice(2).join(' ');
updateQuote(args[0], quote);
} else {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.edit.usage'));
}
return;
}
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.edit.404'));
$.log.event(sender + ' edited quote #' + quote);
return;
}
/**
* @commandpath quotemodetoggle - toggle between !addquote function modes
*/
if ($.equalsIgnoreCase(command, 'quotemodetoggle')) {
if (quoteMode) {
quoteMode = false;
$.inidb.set('settings', 'quoteMode', 'false');
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.add.usage2'));
return;
}
quoteMode = true;
$.inidb.set('settings', 'quoteMode', 'true');
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.add.usage1'));
return;
}
/**
* @commandpath addquote [quote text] - Save a quote
*/
if ($.equalsIgnoreCase(command, 'addquote')) {
if (quoteMode) {
if (args.length < 1) {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.add.usage1'));
return;
}
quote = args.splice(0).join(' ');
$.say($.lang.get('quotesystem.add.success', $.viewer.getByLogin(sender).name(), saveQuote(String($.viewer.getByLogin(sender).name()), quote)));
$.log.event(sender + ' added a quote "' + quote + '".');
return;
}
if (args.length < 2) {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.add.usage2'));
return;
}
var useTwitchNames = $.getIniDbBoolean('settings', 'quoteTwitchNamesToggle', true);
var target = useTwitchNames ? args[0].toLowerCase() : args[0].substring(0, 1).toUpperCase() + args[0].substring(1).toLowerCase();
if (useTwitchNames && !$.user.isKnown(target)) {
$.say($.whisperPrefix(sender) + $.lang.get('common.user.404', target));
return;
}
quote = args.splice(1).join(' ');
var username = useTwitchNames ? $.viewer.getByLogin(target).name() : target;
$.say($.lang.get('quotesystem.add.success', username, saveQuote(String(username), quote)));
$.log.event(sender + ' added a quote "' + quote + '".');
return;
}
/**
* USED BY THE PANEL
*/
if ($.equalsIgnoreCase(command, 'addquotesilent')) {
if (!$.isBot(sender)) {
return;
}
if (args.length < 1) {
return;
}
quote = args.splice(0).join(' ');
saveQuote(String($.viewer.getByLogin(sender).name()), quote);
return;
}
/**
* @commandpath delquote [quoteId] - Delete a quote
*/
if ($.equalsIgnoreCase(command, 'delquote')) {
if (!args[0] || isNaN(args[0])) {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.del.usage'));
return;
}
var newCount;
if ((newCount = deleteQuote(args[0])) >= 0) {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.del.success', args[0], newCount));
$.log.event(sender + ' removed quote with id: ' + args[0]);
return;
}
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.del.404', args[0]));
return;
}
/**
* USED BY THE PANEL
*/
if ($.equalsIgnoreCase(command, 'delquotesilent')) {
if (!$.isBot(sender)) {
return;
}
if (deleteQuote(args[0]) >= 0) {
$.log.event(sender + ' removed quote with id: ' + args[0]);
}
return;
}
/**
* @commandpath quote [quoteId] - Announce a quote by its Id, omit the id parameter to get a random quote
*/
if ($.equalsIgnoreCase(command, 'quote')) {
quote = getQuote(args[0]);
if (quote.length > 0) {
let user = $.viewer.getByLogin(quote[0]);
if (user == null) {
user = quote[0];
} else {
user = user.name();
}
quoteStr = $.getIniDbString('settings', 'quoteMessage', $.lang.get('quotesystem.get.success'));
quoteStr = quoteStr.replace('(id)', (quote.length === 5 ? quote[4].toString() : quote[3].toString()))
.replace('(quote)', quote[1])
.replace('(userrank)', $.resolveRank(quote[0]))
.replace('(user)', user)
.replace('(game)', (quote.length === 5 ? quote[3] : "Some Game"))
.replace('(date)', $.getLocalTimeString($.getSetIniDbString('settings', 'quoteDateFormat', 'dd-MM-yyyy'), parseInt(quote[2])));
$.say(quoteStr);
} else {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.get.404', (typeof args[0] !== 'undefined' ? args[0] : '')));
}
return;
}
/**
* @commandpath quotemessage [message] - Sets the quote string with tags: (id) (quote) (user) (userrank) (game) (date)
*/
if ($.equalsIgnoreCase(command, 'quotemessage')) {
if (!args[0]) {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.quotemessage.usage'));
return;
}
quoteStr = args.splice(0).join(' ');
$.inidb.set('settings', 'quoteMessage', quoteStr);
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.quotemessage.success'));
$.log.event(sender + ' changed the quote message to: ' + quoteStr);
return;
}
/**
* @commandpath quotedateformat [format] - Sets the date format for the (date) tag in a quote
*/
if ($.equalsIgnoreCase(command, 'quotedateformat')) {
if (!args[0]) {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.quotedateformat.usage', $.getSetIniDbString('settings', 'quoteDateFormat', 'dd-MM-yyyy')));
return;
}
quoteStr = args.splice(0).join(' ');
$.inidb.set('settings', 'quoteDateFormat', quoteStr);
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.quotedateformat.success'));
$.log.event(sender + ' changed the quote date format to: ' + quoteStr);
return;
}
/**
* @commandpath searchquote [string] - Searches the quotes for a string and returns a list of IDs
*/
if ($.equalsIgnoreCase(command, 'searchquote')) {
if (!args[0]) {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.searchquote.usage'));
return;
}
var searchString = args.join(' ');
if (searchString.length < 1) {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.searchquote.usage'));
return;
}
var matchingKeys = $.inidb.searchByValue('quotes', searchString);
if (matchingKeys.length === 0) {
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.searchquote.404'));
return;
}
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.searchquote.found', matchingKeys.join(', ')));
return;
}
/**
* @commandpath quotetwitchnamestoggle - Toggles on and off if quote names need to have been seen in chat before
*/
if ($.equalsIgnoreCase(command, 'quotetwitchnamestoggle')) {
var useTwitchNames = $.getIniDbBoolean('settings', 'quoteTwitchNamesToggle');
if (useTwitchNames) {
useTwitchNames = false;
$.inidb.set('settings', 'quoteTwitchNamesToggle', 'false');
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.twitchnames-disabled'));
return;
}
useTwitchNames = true;
$.inidb.set('settings', 'quoteTwitchNamesToggle', 'true');
$.say($.whisperPrefix(sender) + $.lang.get('quotesystem.twitchnames-enabled'));
return;
}
});
/**
* @event initReady
*/
$.bind('initReady', function() {
$.registerChatCommand('./systems/quoteSystem.js', 'quotemodetoggle', $.PERMISSION.Mod);
$.registerChatCommand('./systems/quoteSystem.js', 'searchquote', $.PERMISSION.Viewer);
$.registerChatCommand('./systems/quoteSystem.js', 'addquote', $.PERMISSION.Mod);
$.registerChatCommand('./systems/quoteSystem.js', 'addquotesilent', $.PERMISSION.Admin);
$.registerChatCommand('./systems/quoteSystem.js', 'delquote', $.PERMISSION.Mod);
$.registerChatCommand('./systems/quoteSystem.js', 'delquotesilent', $.PERMISSION.Admin);
$.registerChatCommand('./systems/quoteSystem.js', 'editquote', $.PERMISSION.Mod);
$.registerChatCommand('./systems/quoteSystem.js', 'quote');
$.registerChatCommand('./systems/quoteSystem.js', 'quotemessage', $.PERMISSION.Admin);
$.registerChatCommand('./systems/quoteSystem.js', 'quotedateformat', $.PERMISSION.Admin);
$.registerChatCommand('./systems/quoteSystem.js', 'quotetwitchnamestoggle', $.PERMISSION.Admin);
});
})();
You can’t perform that action at this time.
