-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.js
816 lines (706 loc) · 19.4 KB
/
Bot.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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
import nmv, {UUID} from "@caspertech/node-metaverse";
import chalk from "chalk";
import * as Discord from "discord.js";
import {debug} from "./index.js";
import {doPrint, log} from "./Utils.js";
const slPerms = nmv.BotOptionFlags.LiteObjectStore | nmv.BotOptionFlags.StoreMyAttachmentsOnly;
const mentionRegex = /@(\S+)/ig;
const emptyRegex = /^\s*$/ig;
const discordIgnore = ["888420645283692576"];
const slIgnore = ["71a34da3-268c-4d04-8dc9-63a69cb91bb1", "6279bf98-c639-448a-96bb-00f0a7c42801"];
export const BotTypes = {
SL : {
header: "SL",
colour: chalk.blue
},
DISCORD: {
header: "Discord",
colour: chalk.yellow
}
};
/*
Class representing a general bot
@description This is intentionally not exported. You should only ever instantiate subclasses.
*/
class Bot
{
/*
The bot object itself
@type {Object}
@description This should only be set in subclasses
*/
bot;
/*
The bot object's type
@type {BotTypes}
@description This should only be set in subclasses
*/
botType;
/*
The destination bot
@type {Bot}
@description This is managed in the superclass (Bot) but is also used in subclasses
*/
dest;
/*
Whether or not the bot is connected
@type {boolean}
@description This should be managed by subclasses
*/
connected;
/*
Formats a message
@param {string} sender - The name of the sender
@param {string} msg - The message sent
*/
formatMessage(sender, msg)
{
if(msg.match(emptyRegex))
{
return null;
}
return `<${sender}> ${msg}`;
}
/*
Formats a system message
@param {string} event - The event giving rise to this message
@param {string} sender - The name of the sender
@param {string} msg - The message sent
*/
formatSystemMessage(event, sender, msg)
{
return `[${event}] [${sender}] ${msg}`;
}
/*
Prints a message
@param {string} txt - The text to pront
@param {Object} [obj] - An optional object to print
*/
print(txt, obj = null)
{
doPrint(this.botType.header, this.botType.colour, txt, obj);
}
/*
Prints a debug message
@param {string} txt - The text to pront
@param {Object} [obj] - An optional object to print
*/
printDebug(txt, obj = null)
{
if(debug)
{
doPrint(this.botType.header + " - Debug", this.botType.colour, txt, obj);
}
}
/*
Sets the destination bot for messages originating from this bot
@param {Bot} dest - The bot to send messages to
*/
setDest(dest)
{
this.dest = dest;
}
}
/*
Class representing a Second Life bot
@extends Bot
*/
export class SLBot
extends Bot
{
/*
The bot type
@type {BotTypes}
*/
botType = BotTypes.SL;
/*
The Second Life First Name
@type {string}
*/
firstName;
/*
The groups this bot should manage
@type {string[]}
*/
groups;
/*
This bot's UUID
@type {string}
*/
id;
/*
The Second Life Last Name
@type {string}
*/
lastName;
/*
The Second Life Password
@type {string}
*/
password;
/*
Construct an SLBot instance
@constructor
@param {string} firstName - The Second Life First Name
@param {string} lastName - The Second Life Last Name
@param {string} password - The Second Life Password
@param {string[]} groups - The UUIDs for the groups this bot will manage
*/
constructor(firstName, lastName, password, groups)
{
super();
this.firstName = firstName;
if(lastName && !lastName.match(/^\s*$/i))
{
this.lastName = lastName;
}
else
{
this.lastName = "Resident";
}
this.password = password;
this.groups = groups;
this.print("First name: " + this.firstName);
this.print("Last name: " + this.lastName);
this.print("Groups: ", this.groups);
}
/*
Connects the bot to Second Life
@returns {Promise<void>}
*/
async connect()
{
const loginParameters = new nmv.LoginParameters();
loginParameters.firstName = this.firstName;
loginParameters.lastName = this.lastName;
loginParameters.password = this.password;
if( !this.bot )
{
this.bot = new nmv.Bot(loginParameters, slPerms);
}
this.print("Logging in");
await this.bot.login();
this.print("Logged in");
await this.bot.connectToSim();
this.print("Waiting for event queue");
await this.bot.waitForEventQueue();
this.print("Connected to sim");
this.connected = true;
}
/*
Disconnects the bot
@returns {Promise<void>}
*/
async exit()
{
this.print("Disconnecting from " + this.botType.header);
try
{
await this.bot.close();
}
catch(error)
{
this.print(`<${chalk.red("Error")}> Problem exiting ${this.botType.header} - `, error);
}
this.connected = false;
}
/*
Finds the index of a given group UUID
@param {string} groupId - The group UUID
@returns {integer} - The index
*/
findIndex(groupId)
{
for(let x = 0; x < this.groups.length; x++)
{
if(this.groups[x] === groupId)
{
return x;
}
}
return -1;
}
/*
Starts this bot listening for messages on Second Life
@returns {Promise<void>}
*/
async listen()
{
this.id = UUID.getString(this.bot.agentID());
this.bot.clientEvents.onGroupChat.subscribe(this.onGroupChat.bind(this));
this.bot.clientEvents.onGroupChatSessionJoin.subscribe((event) =>
{
if(event.success)
{
console.log(
"We have joined a chat session! Group ID: " +
event.sessionID);
}
else
{
console.log(
"We have FAILED to join a chat session! Group ID: " +
event.sessionID);
}
});
for(const group of this.groups)
{
this.printDebug("Attempting to start group chat session for " + group);
await this.bot.clientCommands.comms.startGroupChatSession(group, "");
}
this.print("Listening");
}
/*
Group chat event callback
@description This promise can be safely ignored due to async functionality
@param {object} event - The event
@returns {void}
*/
onGroupChat(event)
{
this.printDebug("Message sent: ", event);
const sender = event.fromName;
const senderId = UUID.getString(event.from);
this.printDebug("Message author id: " + UUID.getString(event.from));
this.printDebug("My id: " + UUID.getString(this.bot.agentID()));
if(senderId === this.id || slIgnore.find((ele) => ele === senderId))
{
this.printDebug("This was me or an ignore; ignoring");
return;
}
const text = event.message;
const groupId = UUID.getString(event.groupID);
const index = this.findIndex(groupId);
if(index < 0)
{
this.printDebug("Invalid index: " + index);
return;
}
this.printDebug("Sending to Discord");
this.dest.sendMessage(index, this.formatMessage(sender, text));
}
/*
Sends a message to Second Life
@param {integer} index - The index of the group to send to
@param {string} msg - The message to send
@returns {void}
*/
sendMessage(index, msg)
{
if(index < 0 || index >= this.groups.length || !msg)
{
this.printDebug("Invalid param -- " + index + ", '" + msg + "'");
return;
}
log(msg);
this.printDebug("Attempting message -- " + index + ", '" + msg + "' to " + this.groups[index]);
this.printDebug("Comms: ", this.bot.clientCommands.comms.sendGroupMessage);
//this.printDebug("Commands: ",this.bot.clientCommands);
//this.printDebug("Bot: ", this.bot );
// Promise ignored; async is desired here
this.bot.clientCommands.comms.sendGroupMessage(this.groups[index], msg);
}
}
/*
Class representing a Discord bot
@extends Bot
*/
export class DiscordBot
extends Bot
{
/*
The bot's type
@type {BotTypes}
*/
botType = BotTypes.DISCORD;
/*
The list of channel snowflake IDs this bot will manage
@type {string[]}
*/
channelSnowflakes;
/*
The list of channels this bot is managing
@type {Channel[]}
*/
channels;
/*
The guild this bot is managing
@type {Guild}
*/
guild;
/*
The guild snowflake id of the guild this bot will manage
@type {string}
*/
guildSnowflake;
/*
The secret token to connect to Discord
@type {string}
*/
token;
/*
Constructs a Discord bot
@constructor
@param {string} token - The secret token
@param {string} guildSnowflake - The snowflake id of the guild to manage
@param {string[]} channelSnowflakes - The snowflake ids of the channels to manage
*/
constructor(token, guildSnowflake, channelSnowflakes)
{
super();
this.token = token;
this.guildSnowflake = guildSnowflake;
this.channelSnowflakes = channelSnowflakes;
this.print("Guild: " + this.guildSnowflake);
this.print("Channels: " + this.channelSnowflakes);
}
/*
Connect this bot to Discord
@returns {Promise<void>}
*/
async connect()
{
const Intents = Discord.GatewayIntentBits;
if( !this.bot )
{
this.bot = new Discord.Client({
intents: [Intents.Guilds, Intents.GuildMembers, Intents.GuildBans,
Intents.GuildEmojisAndStickers, Intents.GuildIntegrations,
Intents.GuildWebhooks, Intents.GuildInvites,
Intents.GuildVoiceStates, Intents.GuildPresences,
Intents.GuildMessages, Intents.GuildMessageReactions,
Intents.GuildMessageTyping, Intents.DirectMessages,
Intents.DirectMessageReactions, Intents.DirectMessageTyping,
Intents.MessageContent]
});
}
this.print("Logging in");
await this.bot.login(this.token);
if( !this.guild )
{
this.guild = await this.bot.guilds.fetch(this.guildSnowflake);
}
if( !this.channels )
{
this.channels = [];
for(const channel of this.channelSnowflakes)
{
this.channels.push(await this.guild.channels.fetch(channel));
}
}
this.connected = true;
}
/*
Disconnects this bot from Discord
@returns {Promise<void>}
*/
async exit()
{
this.print("Disconnecting from " + this.botType.header);
try
{
await this.bot.destroy();
}
catch(error)
{
this.print(`<${chalk.red("Error")}> Problem exiting ${this.botType.header} - `, error);
}
this.connected = false;
}
/*
Finds the index of the given snowflake id
@param {string} channelSnowflake - The snowflake to find the index of
@returns {integer} - The index of the given channel
*/
findIndex(channelSnowflake)
{
for(let x = 0; x < this.channelSnowflakes.length; x++)
{
if(this.channelSnowflakes[x] === channelSnowflake)
{
return x;
}
}
return -1;
}
/*
Gets the display name of a given user
@param {string} id - The user's snowflake id
@returns {Promise<string>}
*/
async getDisplayName(id)
{
this.printDebug("Fetching display name for id " + id);
return (await this.guild.members.fetch(id)).displayName;
}
/*
Starts this bot listening
@description All promises returned here are ignored because this code needs to be asynchronous
@returns {void}
*/
listen()
{
this.bot.on("messageCreate", this.onMessageCreate.bind(this)); // msg
this.bot.on("messageDelete", this.onMessageDelete.bind(this)); // msg
this.bot.on("messageDeleteBulk", this.onMessageDeleteBulk.bind(this)); // msg[], channel
this.bot.on("messageUpdate", this.onMessageUpdate.bind(this)); // old, new
this.bot.on("messageReactionAdd", this.onReactionAdd.bind(this)); // reaction, user
this.bot.on("messageReactionRemove", this.onReactionRemove.bind(this)); // reaction, user
this.bot.on("messageReactionRemoveAll", this.onReactionRemoveAll.bind(this)); // msg, reaction[]
this.bot.on("messageReactionRemoveEmoji", this.onRemoveEmoji.bind(this)); // reaction
this.print("Listening");
}
/*
Called when a message is created/sent
@description This promise can be safely ignored due to async functionality
@param {Message} msg - The message sent
@returns {Promise<void>}
*/
async onMessageCreate(msg)
{
this.printDebug("Message sent: ", msg);
this.printDebug("Message author id: " + msg.author.id);
this.printDebug("My id: " + this.bot.user.id);
if(!this.validMessage(msg))
{
this.printDebug("Invalid message; ignored");
return;
}
const text = msg.content;
const sender = await this.getDisplayName(msg.author.id);
const index = this.findIndex(msg.channelId);
if(index < 0)
{
this.printDebug("Invalid index");
return;
}
this.printDebug("Sending to SL");
this.dest.sendMessage(index, this.formatMessage(sender, text));
}
/*
Called when a message is deleted
@description This promise can be safely ignored due to async functionality
@param {Message} msg - The message deleted
@returns {Promise<void>}
*/
async onMessageDelete(msg)
{
this.printDebug("Message deleted: ", msg);
if(!this.validMessage(msg))
{
return;
}
const text = msg.content;
const sender = await this.getDisplayName(msg.author.id);
const index = this.findIndex(msg.channelId);
if(index < 0)
{
this.printDebug("Invalid index");
return;
}
this.dest.sendMessage(index, this.formatSystemMessage("Deleted", sender, text));
}
/*
Called when multiple messages are deleted
@description Channel goes unused here because each msg has its own channel
@param {Message[]} msgs - The messages
@param [Channel] channel - The channel the messages were deleted from
@returns {void}
*/
onMessageDeleteBulk(msgs, channel)
{
for(const msg of msgs)
{
this.onMessageDelete(msg);
}
}
/*
Called when a message is updated
@description This promise can be safely ignored due to async functionality
@param {Message} oldMessage - The old message
@param {Message} newMessage - The new message
@returns {Promise<void>}
*/
async onMessageUpdate(oldMessage, newMessage)
{
this.printDebug("Message updated from: ", oldMessage);
this.printDebug("Message updated to: ", newMessage);
if(!this.validMessage(oldMessage) || !this.validMessage(newMessage))
{
this.printDebug("Invalid message; ignoring");
return;
}
const oldText = oldMessage.content;
const newText = newMessage.content;
const sender = await this.getDisplayName(oldMessage.author.id);
const index = this.findIndex(oldMessage.channelId);
if(index < 0)
{
this.printDebug("Invalid index");
return;
}
this.printDebug("Sending to SL");
this.dest.sendMessage(index, this.formatSystemMessage("Changed", sender, `{${oldText}} -> {${newText}}`));
}
/*
Called when a reaction is added
@description The user is ignored because it's present in the message
@description This promise can be safely ignored due to async functionality
@param {Reaction} reaction - The reaction added
@param {User} [user] - The user who added the reaction
@returns {Promise<void>}
*/
async onReactionAdd(reaction, user)
{
this.printDebug("Reaction added: ", reaction);
if(!this.validMessage(reaction.message))
{
return;
}
const msg = reaction.message;
const text = msg.content;
const sender = await this.getDisplayName(msg.author.id);
const emoji = reaction.emoji.name;
this.printDebug("Emoji: ", reaction.emoji);
const index = this.findIndex(msg.channelId);
if(index < 0)
{
this.printDebug("Invalid index");
return;
}
this.dest.sendMessage(index, this.formatMessage(sender, `:${emoji}: @ {${text}}`));
}
/*
Called when a reaction is removed
@description The user is ignored because it's present in the message
@description This promise can be safely ignored due to async functionality
@param {Reaction} reaction - The reaction removed
@param {User} [user] - The user who added the reaction
@returns {Promise<void>}
*/
async onReactionRemove(reaction, user)
{
this.printDebug("Reaction removed: ", reaction);
if(!this.validMessage(reaction.message))
{
return;
}
const msg = reaction.message;
const text = msg.content;
const sender = await this.getDisplayName(msg.author.id);
const emoji = reaction.emoji.name;
const index = this.findIndex(msg.channelId);
if(index < 0)
{
this.printDebug("Invalid index");
return;
}
this.dest.sendMessage(index, this.formatMessage(sender, `Removed :${emoji}: @ {${text}}`));
}
/*
Called when all reactions are removed
@param {Message} message - The message from which the reactions were deleted
@param {Reaction[]} reactions - The reactions deleted
@returns {void}
*/
onReactionRemoveAll(message, reactions)
{
for(const reaction of reactions)
{
this.onReactionRemove(reaction);
}
}
/*
Called when a bot removes an emoji
@description This promise can be safely ignored due to async functionality
@param {Reaction} reaction - The emoji removed
@returns {Promise<void>}
*/
async onRemoveEmoji(reaction)
{
this.printDebug("Emoji removed: ", reaction);
if(!this.validMessage(reaction.message))
{
return;
}
const msg = reaction.message;
const text = msg.content;
const sender = await this.getDisplayName(msg.author.id);
const emoji = reaction.emoji.name;
const index = this.findIndex(msg.channelId);
if(index < 0)
{
this.printDebug("Invalid index");
return;
}
this.dest.sendMessage(index, this.formatMessage(sender, `Removed :${emoji}: @ {${text}}`));
}
/*
Convert mentions of usernames to actual Discord mentions
@param {string} msg - The message to process
@returns {Promise<string>}
*/
async processMentions(msg)
{
let matches = msg.matchAll(mentionRegex);
for( const match of matches )
{
this.printDebug("Found mention: "+ match[0] + " (" + match[1] + ")" );
const memberId = (await this.guild.members.fetch({
query: match[1],
limit: 1
})).keys().next().value;
if(memberId)
{
this.printDebug(match[0] + " -> " + `<@${memberId}> :`, memberId);
msg = msg.replaceAll(match[0], `<@${memberId}>`);
}
}
return msg;
}
/*
Sends a message to Discord
@param {integer} index - The index of the channel to send the message to
@param {string} msg - The message to send
@returns {void}
*/
sendMessage(index, msg)
{
if(index < 0 || index >= this.channels.length || !msg)
{
this.printDebug("Invalid message on sending");
return;
}
this.print(msg);
log(msg);
// Mentions need to be processed before the message is sent
// But the message does not need to be sent before this method is called again
// Thus, process the promise from processMentions but ignore the one from send
this.processMentions(msg).then((function(msg2)
{
this.printDebug("Index: " + index);
this.printDebug("Channel: ", this.channels[index]);
// Promise ignored; async is desired here
this.channels[index].send(msg2);
}).bind(this));
}
/*
Decides whether a given message is valid or not
@param {Message} msg - The message
@returns {boolean}
*/
validMessage(msg)
{
if( msg.author.id === this.bot.user.id )
{
this.printDebug("Invalid message - from me");
return false;
}
if( discordIgnore.find((id) => id === msg.author.id) )
{
this.printDebug( "Invalid message - ignored id");
return false;
}
return true;
}
}