Skip to content

Commit f047ce0

Browse files
authored
Upgrade discord.js to 12.2.0 (#555)
* Upgrade discord.js to 12.2.0 * Use cache accessors in Discord client * Replace sendMessage with send * Use avatarURL method * Update test stubs for discord.js 12 * Disconnect clients at end of tests * Fix disabling webhook mentions when bot lacks permissions
1 parent ee2d70f commit f047ce0

8 files changed

+139
-104
lines changed

lib/bot.js

+27-22
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ class Bot {
133133
this.attachListeners();
134134
}
135135

136+
disconnect() {
137+
this.ircClient.disconnect();
138+
this.discord.destroy();
139+
Object.values(this.webhooks).forEach(x => x.client.destroy());
140+
}
141+
136142
attachListeners() {
137143
this.discord.on('ready', () => {
138144
logger.info('Connected to Discord');
@@ -258,7 +264,7 @@ class Bot {
258264

259265
static getDiscordNicknameOnServer(user, guild) {
260266
if (guild) {
261-
const userDetails = guild.members.get(user.id);
267+
const userDetails = guild.members.cache.get(user.id);
262268
if (userDetails) {
263269
return userDetails.nickname || user.username;
264270
}
@@ -277,12 +283,12 @@ class Bot {
277283
return text
278284
.replace(/\n|\r\n|\r/g, ' ')
279285
.replace(/<#(\d+)>/g, (match, channelId) => {
280-
const channel = this.discord.channels.get(channelId);
286+
const channel = this.discord.channels.cache.get(channelId);
281287
if (channel) return `#${channel.name}`;
282288
return '#deleted-channel';
283289
})
284290
.replace(/<@&(\d+)>/g, (match, roleId) => {
285-
const role = message.guild.roles.get(roleId);
291+
const role = message.guild.roles.cache.get(roleId);
286292
if (role) return `@${role.name}`;
287293
return '@deleted-role';
288294
})
@@ -391,10 +397,10 @@ class Bot {
391397
// #channel -> channel before retrieving and select only text channels:
392398
let discordChannel = null;
393399

394-
if (this.discord.channels.has(discordChannelName)) {
395-
discordChannel = this.discord.channels.get(discordChannelName);
400+
if (this.discord.channels.cache.has(discordChannelName)) {
401+
discordChannel = this.discord.channels.cache.get(discordChannelName);
396402
} else if (discordChannelName.startsWith('#')) {
397-
discordChannel = this.discord.channels
403+
discordChannel = this.discord.channels.cache
398404
.filter(c => c.type === 'text')
399405
.find(c => c.name === discordChannelName.slice(1));
400406
}
@@ -417,7 +423,7 @@ class Bot {
417423
}
418424

419425
getDiscordAvatar(nick, channel) {
420-
const guildMembers = this.findDiscordChannel(channel).guild.members;
426+
const guildMembers = this.findDiscordChannel(channel).guild.members.cache;
421427
const findByNicknameOrUsername = caseSensitive =>
422428
(member) => {
423429
if (caseSensitive) {
@@ -438,8 +444,8 @@ class Bot {
438444

439445
// No matching user or more than one => default avatar
440446
if (users && users.size === 1) {
441-
const url = users.first().user.avatarURL;
442-
if (url) return url.replace(/\?size=\d{1,}$/, '?size=128');
447+
const url = users.first().user.avatarURL({ size: 128, format: 'png' });
448+
if (url) return url;
443449
}
444450

445451
// If there isn't a URL format, don't send an avatar at all
@@ -498,7 +504,7 @@ class Bot {
498504
// @username#1234 => mention
499505
// skips usernames including spaces for ease (they cannot include hashes)
500506
// checks case insensitively as Discord does
501-
const user = guild.members.find(x =>
507+
const user = guild.members.cache.find(x =>
502508
Bot.caseComp(x.user.username, username)
503509
&& x.user.discriminator === discriminator);
504510
if (user) return user;
@@ -510,16 +516,16 @@ class Bot {
510516
// this preliminary stuff is ultimately unnecessary
511517
// but might save time over later more complicated calculations
512518
// @nickname => mention, case insensitively
513-
const nickUser = guild.members.find(x =>
514-
x.nickname !== null && Bot.caseComp(x.nickname, reference));
519+
const nickUser = guild.members.cache.find(x =>
520+
x.nickname && Bot.caseComp(x.nickname, reference));
515521
if (nickUser) return nickUser;
516522

517523
// @username => mention, case insensitively
518-
const user = guild.members.find(x => Bot.caseComp(x.user.username, reference));
524+
const user = guild.members.cache.find(x => Bot.caseComp(x.user.username, reference));
519525
if (user) return user;
520526

521527
// @role => mention, case insensitively
522-
const role = guild.roles.find(x => x.mentionable && Bot.caseComp(x.name, reference));
528+
const role = guild.roles.cache.find(x => x.mentionable && Bot.caseComp(x.name, reference));
523529
if (role) return role;
524530

525531
// No match found checking the whole word. Check for partial matches now instead.
@@ -544,13 +550,13 @@ class Bot {
544550
};
545551

546552
// check users by username and nickname
547-
guild.members.forEach((member) => {
553+
guild.members.cache.forEach((member) => {
548554
checkMatch(member.user.username, member);
549-
if (bestMatch === member || member.nickname === null) return;
555+
if (bestMatch === member || !member.nickname) return;
550556
checkMatch(member.nickname, member);
551557
});
552558
// check mentionable roles by visible name
553-
guild.roles.forEach((member) => {
559+
guild.roles.cache.forEach((member) => {
554560
if (!member.mentionable) return;
555561
checkMatch(member.name, member);
556562
});
@@ -561,7 +567,7 @@ class Bot {
561567
return match;
562568
}).replace(/:(\w+):/g, (match, ident) => {
563569
// :emoji: => mention, case sensitively
564-
const emoji = guild.emojis.find(x => x.name === ident && x.requiresColons);
570+
const emoji = guild.emojis.cache.find(x => x.name === ident && x.requiresColons);
565571
if (emoji) return emoji;
566572

567573
return match;
@@ -571,7 +577,7 @@ class Bot {
571577
// but these seem likely to be common around channel references)
572578

573579
// discord matches channel names case insensitively
574-
const chan = guild.channels.find(x => Bot.caseComp(x.name, channelName));
580+
const chan = guild.channels.cache.find(x => Bot.caseComp(x.name, channelName));
575581
return chan || match;
576582
});
577583

@@ -586,11 +592,10 @@ class Bot {
586592
}
587593
const avatarURL = this.getDiscordAvatar(author, channel);
588594
const username = _.padEnd(author.substring(0, USERNAME_MAX_LENGTH), USERNAME_MIN_LENGTH, '_');
589-
webhook.client.sendMessage(withMentions, {
595+
webhook.client.send(withMentions, {
590596
username,
591-
text,
592597
avatarURL,
593-
disableEveryone: !canPingEveryone,
598+
disableMentions: canPingEveryone ? 'none' : 'everyone',
594599
}).catch(logger.error);
595600
return;
596601
}

package-lock.json

+61-43
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"license": "MIT",
3838
"dependencies": {
3939
"commander": "^6.0.0",
40-
"discord.js": "11.5.1",
40+
"discord.js": "12.2.0",
4141
"irc-colors": "1.5.0",
4242
"irc-formatting": "1.0.0-rc3",
4343
"irc-upd": "0.11.0",

0 commit comments

Comments
 (0)