-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
786 lines (689 loc) · 25.1 KB
/
index.js
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
var Discord = require('discord.io');
var fs = require('fs');
var wait = require('wait-sync');
const auth = require("./auth.json");
const support = require("./support.js");
const codes = require("./errorcodes.js");
const moderator = require("./funsucker.js");
var pbimg = require("./pblimg.js");
var userauth = require("./roles.js");
const appstore = require("./appsearch.js");
const config = require("./config.json");
const ai = require("./ai.js");
//Automatically start bot on launch (true normally, false for debug)
var autoStartBot = true;
var debugCommandsEnabled = true;
var verboseLogging = true;
var disabled = false
version = 0.1;
//Dynamic configuration. Loaded/saved to disk. Don't edit here
settings = {
mytag: "",
serverID: "",
usersICareAbout: {}
}
// Utility functions
utils = {
dayOfWeek: function() {
return new Date().getDay();
},
day: function() {
var d = new Date().getDay();
return env.days[d];
},
debugLog: debugLog,
sendMessage: botReply,
deleteMessage: deleteMessage,
getServerTrustedID: userauth.getTrustedServerName,
getRndInteger: getRndInteger,
isUserMuted: isUserMuted,
logMutedUserMessageDeleted: logMutedUserMessageDeleted,
toggleMute: toggleMute,
setUserMute: setUserMute,
userTagToID: userTagToID,
getUserAge: getUserAge
}
function resolveServerIDToTrustedServer(id) {
var sname = null
var p = config.trustedServers.forEach(elm => {
if (id == elm.id) {
sname = elm.name
}
});
return sname
}
function validateConfig() {
}
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
function userTagToID(tag) {
//E.g. <@!316611120519577602>
try {
return tag.match(/\d+/)[0];
} catch (e) {
return "";
}
}
function debugLog(text) {
if (verboseLogging) {
console.log(text)
}
}
function generateHelpEmbed() {
var e = {
color: parseInt("ff4700", 16),
thumbnail: {
url: "https://willow.systems/pebble/bot/bot.png",
height: 80,
width: 80
},
title: "Hi, I'm Anne",
description: "I do a few things round here to help out. Perhaps the most useful is answer your questions. Start the question with a question mark (mad, I know) and I'll do my best to answer it.",
fields: [
{"name": "Asking me a question", value: "Start a question with a question mark, and I'll try to find support for your issue. For example: `? Where can I find the android app?`"},
{"name": "Pebble Images", value: "If you write a message with a Pebble resource in it (i.e. `system://images/emoji_11`), I'll post that image. For a list of all available, see https://willow.systems/pebble/bot/images/"},
{"name": "Error codes", value: "If you write a message with a Pebble error code in it (i.e. `FE504504`), I'll explain how to fix it."},
{"name": "Appstore Embed", value: "If you paste a Pebble store link (and nothing else), I'll pull some of the info"},
{"name": "Other Commands", value: "I also have a few commands that are useful. These start with a leading period:"},
{"name": ".support [topic]", "value": "Show a support topic. If you don't know what you're looking for, try asking me a question first."},
// {"name": ".support list", "value": "List all support topics."},
{"name": ".store [search term]", "value": "I'll search the store with your term and return the top result."},
{"name": ".app [search term], .face [search term]", "value": "The same as .store, but filtered to apps or faces only."},
{"name": ".help", "value":"Show this message"},
{"name": ".ignore", "value":"Toggle whether I should ignore you or not"}
]
}
return e
}
function loadSaveData() {
try {
var data = fs.readFileSync(__dirname + "/brain.json");
data = JSON.parse(data);
if (data.usersICareAbout == null) { data.usersICareAbout = {} }
if (data.seenusers == null) { data.seenusers = []}
if (data.mutedUsers == null) { data.mutedUsers = []}
settings = data
} catch (e) {
console.log("Failed to start: " + e)
process.exit();
}
}
function save() {
fs.writeFile(__dirname + "/brain.json", JSON.stringify(settings), "UTF-8", function() {
//debugLog("Settings persisted to disk.");
});
}
function amIIgnoringUser(userID) {
userID = userTagToID(userID)
if (settings.usersICareAbout.hasOwnProperty(userID)) {
if (settings.usersICareAbout[userID].hasOwnProperty("ignore")) {
return settings.usersICareAbout[userID].ignore
} else {
return false
}
} else {
return false
}
}
function amIOnlyDMingUser(userID) {
userID = userTagToID(userID)
if (settings.usersICareAbout.hasOwnProperty(userID)) {
if (settings.usersICareAbout[userID].hasOwnProperty("forcedm")) {
return settings.usersICareAbout[userID].forcedm
} else {
return false
}
} else {
return false
}
}
function isUserMuted(userID) {
if (settings.usersICareAbout.hasOwnProperty(userID) && settings.usersICareAbout[userID].hasOwnProperty("muteUntil")) {
var then = new Date(settings.usersICareAbout[userID].muteUntil).getTime();
var now = new Date().getTime()
return (then > now)
} else {
return false
}
}
function toggleIgnore(userID, channelID) {
userID = userTagToID(userID)
if (amIIgnoringUser(userID)) {
settings.usersICareAbout[userID].ignore = false
botReply(" ", channelID, userID, {
color: parseInt("ff4700", 16),
thumbnail: {
url: "https://willow.systems/pebble/bot/unmute.png",
height: 80,
width: 80
},
title: "I'm no longer ignoring you",
description: "We have so much to catch up on!"
});
} else {
if (! settings.usersICareAbout.hasOwnProperty(userID)) {
settings.usersICareAbout[userID] = {};
}
settings.usersICareAbout[userID].ignore = true
botReply(" ", channelID, userID, {
color: parseInt("ff4700", 16),
thumbnail: {
url: "https://willow.systems/pebble/bot/mute.png",
height: 80,
width: 80
},
title: "I'm now ignoring you",
description: "Just write `.ignore` again if you ever change your mind"
});
}
save()
}
function toggleForceDM(userID, adminID) {
userID = userTagToID(userID)
if (amIOnlyDMingUser(userID)) {
settings.usersICareAbout[userID].forcedm = false
moderator.notifyOfForceDM(userID, adminID, false)
} else {
if (! settings.usersICareAbout.hasOwnProperty(userID)) {
settings.usersICareAbout[userID] = {};
}
settings.usersICareAbout[userID].forcedm = true
moderator.notifyOfForceDM(userID, adminID, true)
}
save()
}
function toggleMute(userID, adminID) {
userID = userTagToID(userID)
if (isUserMuted(userID)) {
setUserMute(userID, new Date(0))
moderator.notifyOfMute(userID, adminID, false)
} else {
setUserMute(userID, new Date("2121-01-01"))
moderator.notifyOfMute(userID, adminID, true)
}
save()
}
function setUserMute(userID, endDate) {
if (endDate.getTime() < new Date().getTime()) {
//We're disabling mute
//Remove role
removeUserFromRole(userID, config.rateLimit.timeoutRoleID[userauth.getTrustedServerName()])
for (var i=0;i<settings.mutedUsers.length;i++) {
if (settings.mutedUsers[i].id == userID) {
settings.mutedUsers.splice(i,1);
}
}
} else {
//Enable mute, add role
addUserToRole(userID, config.rateLimit.timeoutRoleID[userauth.getTrustedServerName()])
settings.mutedUsers.push({
id: userID,
end: endDate
});
}
if (! settings.usersICareAbout.hasOwnProperty(userID)) {
settings.usersICareAbout[userID] = {};
}
settings.usersICareAbout[userID].muteUntil = endDate.getTime()
}
function unmuteExpiredMutes() {
for (var i=0;i<settings.mutedUsers.length;i++) {
var then = new Date(settings.mutedUsers[i].end).getTime()
var now = new Date().getTime()
if (then < now) {
setUserMute(settings.mutedUsers[i].id, new Date(0))
}
}
}
function logMutedUserMessageDeleted(userID) {
if (! settings.usersICareAbout.hasOwnProperty(userID)) {
settings.usersICareAbout[userID] = {}
}
if (! settings.usersICareAbout.hasOwnProperty("mutedMsgCount")) {
settings.usersICareAbout.mutedMsgCount = 0
}
settings.usersICareAbout.mutedMsgCount++
}
function deleteMessage(cid, mid, callback) {
console.log("Channel: " + cid.toString())
console.log("Message: " + mid.toString())
bot.deleteMessage({channelID: cid.toString(), messageID: mid.toString()}, function(error) {
if (error != null) {
console.log("Error deleting a message")
console.log(error)
} else {
console.log("Message deleted")
callback()
}
});
}
function recordSeenUser(userID) {
userID = userTagToID(userID)
if (! settings.seenusers.includes(userID)) {
settings.seenusers.push(userID)
}
}
function haveISeenUserBefore(userID) {
userID = userTagToID(userID)
return settings.seenusers.includes(userID)
}
function getUserAge(userID) {
if (settings.usersICareAbout.hasOwnProperty(userID) && settings.usersICareAbout[userID].hasOwnProperty("joined")) {
var birthDate = new Date(settings.usersICareAbout[userID].joined).getTime()
var now = new Date().getTime()
var ago = (now - birthDate) / 1000 / 3600 / 24
console.log(userID + " is " + ago + " days old")
return ago
} else {
return 0
}
}
function addUserToRole(userID, roleID) {
bot.addToRole({
serverID: settings.serverID,
userID: userID,
roleID: roleID
}, (error, response) => {
if (error != null) {
console.log("Add role error: " + JSON.stringify(error))
}
})
}
function removeUserFromRole(userID, roleID) {
bot.removeFromRole({
serverID: settings.serverID,
userID: userID,
roleID: roleID
}, (error, response) => {
if (error != null) {
console.log("Add role error: " + JSON.stringify(error))
}
})
}
//Load data from FILE
loadSaveData()
//Initialise the icon replier
pbimg.init(__dirname + "/icons")
//Validate configuration
validateConfig();
//Bot functions. Magic happens in bot.on('message')
bot = new Discord.Client({
token: auth.token,
autorun: autoStartBot
});
function botReply(msg, channelID, userID, msgEmbed) {
if (msg == null || msg == "") { return }
if (amIOnlyDMingUser(userID)) {
//DM user
channelID = userID
}
if (msg.includes("~")) {
msg = msg.split("~");
if (msg[0] == "file") {
console.log("SEND FILE")
bot.uploadFile({
to: channelID,
file: msg[1]
});
}
} else {
if (msg.length > 1999) {
//Msg is too long, cut to truncate
msg = msg.substring(0, 1995);
//Now we need to cut off the last word, so we don't send a malformed emoji
var split = (msg.includes(" ")) ? "\ " : "\<";
msg = msg.substring(0, msg.lastIndexOf(split)) + "..."
}
if (msgEmbed != null) {
bot.sendMessage({ to: channelID, message: msg, embed: msgEmbed});
} else {
bot.sendMessage({ to: channelID, message: msg });
}
}
}
bot.on('ready', function (evt) {
//Get the first server address. This bot is not designed to service multiple servers on the same runtime (though it probably could)
try {
var serverID = evt.d.guilds[0].id
settings.serverID = serverID
} catch (e) {
console.log("Failed to get server ID: " + e);
var serverID = "Unknown"
}
console.log('Connected to discord. Server ID: ' + serverID);
console.log('Authenticated as: ' + bot.username + ' - (' + bot.id + ')');
if (resolveServerIDToTrustedServer(serverID) != null) {
console.log("Server recognised as " + resolveServerIDToTrustedServer(serverID))
} else {
console.log("Warning: Server is not recognised. Connected to unknown server. Authentication roles and commanded dependant on them are not available.")
}
settings.mytag = "<@" + bot.id + ">"
//Load permission info for this server from config.json
userauth.readConfig(serverID, config)
//Map the current server roles to IDs
userauth.setupRoles(bot.servers[serverID]);
//Save once every 15 mins
setInterval(save, 900000);
//Check for users to unmute once a minute
setInterval(unmuteExpiredMutes, 60000)
//Setup wolfram
ai.init(auth.wolfram)
console.log("Initalisation complete. Ready to serve " + resolveServerIDToTrustedServer(serverID) + "!")
});
bot.on('disconnect', function(errMsg, code) {
console.log('Disconnected from discord [' + code + ']. Reason: ' + errMsg);
console.log('Reconnecting');
bot.connect();
});
bot.on('message', function (user, userID, channelID, message, evt) {
//Ignore ourselves
if (userTagToID(userID) == userTagToID(settings.mytag)) {
// debugLog("Ignoring message as it's from myself")
return;
}
//Check we've not been disabled
if (disabled && ! message.includes("$on")) {
return
}
var messageID = evt.d.id
//Get roles
var roles = []
if (evt.d.hasOwnProperty("member")) {
if (evt.d.member.hasOwnProperty("roles")) {
//DMs don't have roles
roles = evt.d.member.roles
}
//Store info for $joined
if (! settings.usersICareAbout.hasOwnProperty(userID)) { settings.usersICareAbout[userID] = {} }
settings.usersICareAbout[userID].joined = (evt.d.member.hasOwnProperty("joined_at")) ? evt.d.member.joined_at : "Unknown"
//Store some basic metadata for fun
//Inc messagecount
settings.usersICareAbout[userID].msgcount = (settings.usersICareAbout[userID].hasOwnProperty("msgcount")) ? settings.usersICareAbout[userID].msgcount + 1 : 1;
if (! settings.usersICareAbout[userID].hasOwnProperty("wordcount")) {
settings.usersICareAbout[userID].wordcount = {
pebble: 0,
yes: 0,
no: 0
}
}
settings.usersICareAbout[userID].wordcount.pebble += (message.match(/pebble/g) || []).length
settings.usersICareAbout[userID].wordcount.yes += (message.match(/\byes\b/g) || []).length
settings.usersICareAbout[userID].wordcount.no += (message.match(/\bno\b/g) || []).length
}
//Moderate message (delete if user muted)
moderator.scan(message, messageID, userID, channelID)
//Ignore user?
if (amIIgnoringUser(userID) && message.indexOf(".ignore") == -1) {
debugLog("Ignoring user " + userID)
return
}
//Remove punctuation we don't care about
message = message.replace(/[\(\)\[\]\{\}\!]/g,"");
appstore.detectAutoEmbed(message, function(res) {
if (res.s) {
botReply(" ", channelID, userID, {
color: parseInt(res.category_color, 16),
thumbnail: {
url: res.img,
height: 144,
width: 168
},
title: res.title,
fields: [
{name: "Category", value: res.category},
{name: "Description", value: res.description},
{name: "Author", value: res.author},
{name: "Hearts", value: res.hearts}
],
footer: {
"text": "(Click the title to go to the store page)"
},
url: "https://apps.rebble.io/en_US/application/" + res.id
});
}
});
//Wolfram
// var mytag = "<@" + bot.id + ">"
// var msg = message.replace("<@!", "<@");
// if (msg.substring(0, mytag.length) == mytag) {
// if (userauth.hasPermission(roles, "useAdminCommands")) {
// msg = msg.replace(mytag, "");
// ai.answer(msg, function(response) {
// botReply(response, channelID, userID)
// })
// }
// }
// Commands
if (message.substr(0,1) == ".") {
var args = message.substr(1).split(' ');
var cmd = args[0]
args = args.splice(1);
// This is the core logic, where we manage commands
if (cmd == "help") {
botReply(" ", channelID, userID, generateHelpEmbed());
} else if (cmd == "stats") {
var countStart = new Date("2021-03-06T12:27:02.898Z");
var ago = new Date() - countStart
ago = Math.floor(ago / 1000 / 3600 / 24)
botReply("Since I started counting " + ago + " days ago, you've sent " + settings.usersICareAbout[userID].msgcount + " messages. You've said 'pebble' " + settings.usersICareAbout[userID].wordcount.pebble + " times, 'yes' " + settings.usersICareAbout[userID].wordcount.yes + " times and 'no' " + settings.usersICareAbout[userID].wordcount.no + " times.", channelID, userID);
return
} else if (cmd == "permissions") {
var response = "Permission bits for <@" + userID + ">:\nuseAdminCommands: " + userauth.hasPermission(roles, "useAdminCommands");
response += "\nuseIntrospection: " + userauth.hasPermission(roles, "useIntrospection");
botReply(response, channelID, userID);
} else if (cmd == "support") {
var s = support.showTopic(args.join(" "));
if (s.file) {
console.log("Support topic has a file!: " + s.file)
botReply("file~files/" + s.file, channelID, userID);
s.file = null
}
botReply(s.msg, channelID, userID, s.embed);
} else if (cmd == "ignore") {
toggleIgnore(userID, channelID);
} else if (cmd == "app" || cmd == "face" || cmd == "store") {
var filter = {
app: "watchapp",
face: "watchface",
store: "watchface,watchapp"
}[cmd]
appstore.search(args.join(" "), filter, function(res) {
if (res.s) {
botReply(" ", channelID, userID, {
color: parseInt(res.category_color, 16),
thumbnail: {
url: res.img,
height: 144,
width: 168
},
title: res.title,
fields: [
{name: "Category", value: res.category},
{name: "Description", value: res.description},
{name: "Author", value: res.author},
{name: "Hearts", value: res.hearts},
],
footer: {
"text": "(Click the title to go to the store page)"
},
url: "https://apps.rebble.io/en_US/application/" + res.id
});
} else {
botReply("No results", channelID, userID);
}
}, function() {
botReply("Something went wrong", channelID, userID);
});
}
}
// Admin commands
if (message.substr(0,1) == "$" && /on|off|help|save|joined|forcedm|say|toggleMute|muteState.*/.test(message)) {
if (! userauth.hasPermission(roles, "useAdminCommands")) {
botReply("You do not have permission to do that", channelID, userID)
return
}
var args = message.substr(1).split(' ');
var cmd = args[0]
args = args.splice(1);
if (cmd == "off") {
if (userauth.hasPermission(roles, "switchOff")) {
disabled = true
botReply("Entering sleep mode. I'll now only respond to the Wake command: `$on`.", channelID, userID);
} else {
botReply("You do not have permission to do that", channelID, userID)
return
}
} else if (cmd == "on") {
if (userauth.hasPermission(roles, "switchOff")) {
disabled = false
botReply("I'm awake!", channelID, userID);
} else {
botReply("You do not have permission to do that", channelID, userID)
return
}
} else if (cmd == "joined") {
var usr = userTagToID(args[0])
if (usr == null || usr == "") {
botReply("Missing argument. Usage: $joined @user", channelID, userID);
} else {
if (settings.usersICareAbout.hasOwnProperty(usr) && settings.usersICareAbout[usr].hasOwnProperty("joined")) {
botReply("User join date: **" + settings.usersICareAbout[usr].joined.toString().split(".")[0].replace("T", " ") + "**", channelID, userID);
} else {
botReply("Unknown user or data not yet gathered.", channelID, userID);
}
}
} else if (cmd == "forcedm") {
var usr = userTagToID(args[0])
if (usr == null || usr == "") {
botReply("Missing argument. Usage: $forcedm @user", channelID, userID);
} else {
toggleForceDM(usr, userID);
}
} else if (cmd == "toggleMute") {
var usr = userTagToID(args[0])
if (usr == null || usr == "") {
botReply("Missing argument. Usage: $toggleMute @user (@ or ID)", channelID, userID);
} else {
toggleMute(usr, userID);
}
} else if (cmd == "muteState") {
var usr = userTagToID(args[0])
if (usr == null || usr == "") {
botReply("Missing argument. Usage: $muteState @user (@ or ID)", channelID, userID);
} else {
var out = (isUserMuted(usr)) ? "User is muted. " : "User is not muted. "
if (settings.usersICareAbout.hasOwnProperty(usr) && settings.usersICareAbout[usr].hasOwnProperty("muteUntil") && isUserMuted(usr)) {
out += " Mute expires " + new Date(settings.usersICareAbout[usr].muteUntil).toISOString() + ". "
}
if (settings.usersICareAbout.hasOwnProperty(usr) && settings.usersICareAbout[usr].hasOwnProperty("mutedMsgCount")) {
out += ` While muted they have tried to send ${settings.usersICareAbout[usr].mutedMsgCount} messages.`
}
botReply(out, channelID, userID);
}
} else if (cmd == "save") {
save();
botReply("It is now safe to turn off your computer", channelID, userID);
} else if (cmd == "say") {
var channel = args[0]
args.splice(0,1);
botReply(args.join(" "), channel, bot.id)
} else if (cmd == "help") {
botReply('Adminstrator Commands:\n\
$off - Suspend Anne\n\
$on - Reanimate Anne\n\
$joined @user - Show the server join date for user\n\
$forcedm @user - Toggle forceDM mode for @user. If enabled bot will only DM @user in response to messages\n\
$say <channelID> <msg> - Puppeteer Anne\n\
$toggleMute <userID> - Toggle muting a userID\n\
$muteState <userID> - Get the muted state for a userID\n\
$save - Save brain to disk\n\
$help - Show this dialogue', channelID, userID);
}
}
//Remove . as it's not a command
message = message.replace(/\./g,"");
// Pebble image sending
var img = pbimg.match(message)
if (img != false) {
botReply(img,channelID, userID);
return
}
//help keyword detection
var is_keyword_search = false
var mytag = "<@" + bot.id + ">"
var msg = message.replace("<@!", "<@");
//Match on @Anne Droid <question>
if (msg.substring(0, mytag.length) == mytag) {
message = message.replace(mytag, "");
message = message.replace(/[<>@!]/g,"");
is_keyword_search = true
}
//Legacy ? <question>
if (message.substr(0,1) == "?" && message.replace(/[\?\ ]/g,"").length > 1) {
is_keyword_search = true
}
if (is_keyword_search) {
var supportResponse = support.handleNLQuery(message)
if (supportResponse.file) {
console.log("Support topic has a file!: " + supportResponse.file)
botReply("file~files/" + supportResponse.file, channelID, userID);
supportResponse.file = null
}
botReply(supportResponse.msg, channelID, userID, supportResponse.embed);
recordSeenUser(userID);
return
} else {
//Proactive help
//This is where if we see a new user ask a question in #rebble-help, we introduce outselves
if (userauth.getProactiveChannels(config).includes(channelID.toString()) && ! haveISeenUserBefore(userID)) {
botReply(" ", userID, userID, {
color: parseInt("ff4700", 16),
thumbnail: {
url: "https://willow.systems/pebble/bot/bot.png",
height: 80,
width: 80
},
title: "Hi, @" + user,
description: "I've not seen you around here before, welcome to the Rebble Alliance discord server. Here are a few useful tips:",
fields: [
{name: "Need help?", value: "If you have a question and nobody has answered yet, try asking me! **Start your question with a leading '?'** and I'll try to answer it. E.g. `? How do I setup rebble?`" },
{name: "Still confused?", value: "You can find a channel guide and role explanations in <#221397928592277504>, there are plenty of humans here who are super helpful." }
],
});
recordSeenUser(userID);
}
}
//Error code detection
var errorCodeFix = codes.matchCode(message);
if (errorCodeFix != null) {
botReply(" ", channelID, userID, errorCodeFix)
}
//Fun
var binary = RegExp('^[01 ]+$')
if (binary.test(message) && message.length > 5) {
botReply('01001100 01101111 01101110 01100111 00100000 01001100 01101001 01110110 01100101 00100000 01010000 01100101 01100010 01100010 01101100 01100101 00100001', channelID, userID)
}
recordSeenUser(userID);
});
bot.on('any', function(event) {
//Catch special events
//Eventually populate this from roles, but for now I cba
var protectedUsernames = ["katharineberry","bradtgmurray","kat","liammcloughlin","meiguro","orviwan","crc32","ishotjr","josuha","will","will0","tation","astosia","stefanogerli","tertty","annedroid","sphinxy","itsthered","itsthered1","udsx"];
if (event.t == "GUILD_MEMBER_ADD") {
//New user has joined the server
var newuser = event.d.user.username;
newuser = newuser.toLowerCase().replace(/[!?.'")>\]]/g,"")
for (var i = 0; i < protectedUsernames.length; i++) {
if (newuser == protectedUsernames[i]) {
moderator.warnPotentialImpersonate(event.d.user.id)
break
}
}
} else if (event.t == "GUILD_BAN_ADD") {
//User yeeted
moderator.notifyOfBan(event.d.user.username)
} else {
//console.log(event)
}
});