Skip to content

Upgrade discord.js to version 12 #555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ class Bot {
this.attachListeners();
}

disconnect() {
this.ircClient.disconnect();
this.discord.destroy();
Object.values(this.webhooks).forEach(x => x.client.destroy());
}

attachListeners() {
this.discord.on('ready', () => {
logger.info('Connected to Discord');
Expand Down Expand Up @@ -258,7 +264,7 @@ class Bot {

static getDiscordNicknameOnServer(user, guild) {
if (guild) {
const userDetails = guild.members.get(user.id);
const userDetails = guild.members.cache.get(user.id);
if (userDetails) {
return userDetails.nickname || user.username;
}
Expand All @@ -277,12 +283,12 @@ class Bot {
return text
.replace(/\n|\r\n|\r/g, ' ')
.replace(/<#(\d+)>/g, (match, channelId) => {
const channel = this.discord.channels.get(channelId);
const channel = this.discord.channels.cache.get(channelId);
if (channel) return `#${channel.name}`;
return '#deleted-channel';
})
.replace(/<@&(\d+)>/g, (match, roleId) => {
const role = message.guild.roles.get(roleId);
const role = message.guild.roles.cache.get(roleId);
if (role) return `@${role.name}`;
return '@deleted-role';
})
Expand Down Expand Up @@ -391,10 +397,10 @@ class Bot {
// #channel -> channel before retrieving and select only text channels:
let discordChannel = null;

if (this.discord.channels.has(discordChannelName)) {
discordChannel = this.discord.channels.get(discordChannelName);
if (this.discord.channels.cache.has(discordChannelName)) {
discordChannel = this.discord.channels.cache.get(discordChannelName);
} else if (discordChannelName.startsWith('#')) {
discordChannel = this.discord.channels
discordChannel = this.discord.channels.cache
.filter(c => c.type === 'text')
.find(c => c.name === discordChannelName.slice(1));
}
Expand All @@ -417,7 +423,7 @@ class Bot {
}

getDiscordAvatar(nick, channel) {
const guildMembers = this.findDiscordChannel(channel).guild.members;
const guildMembers = this.findDiscordChannel(channel).guild.members.cache;
const findByNicknameOrUsername = caseSensitive =>
(member) => {
if (caseSensitive) {
Expand All @@ -438,8 +444,8 @@ class Bot {

// No matching user or more than one => default avatar
if (users && users.size === 1) {
const url = users.first().user.avatarURL;
if (url) return url.replace(/\?size=\d{1,}$/, '?size=128');
const url = users.first().user.avatarURL({ size: 128, format: 'png' });
if (url) return url;
}

// If there isn't a URL format, don't send an avatar at all
Expand Down Expand Up @@ -498,7 +504,7 @@ class Bot {
// @username#1234 => mention
// skips usernames including spaces for ease (they cannot include hashes)
// checks case insensitively as Discord does
const user = guild.members.find(x =>
const user = guild.members.cache.find(x =>
Bot.caseComp(x.user.username, username)
&& x.user.discriminator === discriminator);
if (user) return user;
Expand All @@ -510,16 +516,16 @@ class Bot {
// this preliminary stuff is ultimately unnecessary
// but might save time over later more complicated calculations
// @nickname => mention, case insensitively
const nickUser = guild.members.find(x =>
x.nickname !== null && Bot.caseComp(x.nickname, reference));
const nickUser = guild.members.cache.find(x =>
x.nickname && Bot.caseComp(x.nickname, reference));
if (nickUser) return nickUser;

// @username => mention, case insensitively
const user = guild.members.find(x => Bot.caseComp(x.user.username, reference));
const user = guild.members.cache.find(x => Bot.caseComp(x.user.username, reference));
if (user) return user;

// @role => mention, case insensitively
const role = guild.roles.find(x => x.mentionable && Bot.caseComp(x.name, reference));
const role = guild.roles.cache.find(x => x.mentionable && Bot.caseComp(x.name, reference));
if (role) return role;

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

// check users by username and nickname
guild.members.forEach((member) => {
guild.members.cache.forEach((member) => {
checkMatch(member.user.username, member);
if (bestMatch === member || member.nickname === null) return;
if (bestMatch === member || !member.nickname) return;
checkMatch(member.nickname, member);
});
// check mentionable roles by visible name
guild.roles.forEach((member) => {
guild.roles.cache.forEach((member) => {
if (!member.mentionable) return;
checkMatch(member.name, member);
});
Expand All @@ -561,7 +567,7 @@ class Bot {
return match;
}).replace(/:(\w+):/g, (match, ident) => {
// :emoji: => mention, case sensitively
const emoji = guild.emojis.find(x => x.name === ident && x.requiresColons);
const emoji = guild.emojis.cache.find(x => x.name === ident && x.requiresColons);
if (emoji) return emoji;

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

// discord matches channel names case insensitively
const chan = guild.channels.find(x => Bot.caseComp(x.name, channelName));
const chan = guild.channels.cache.find(x => Bot.caseComp(x.name, channelName));
return chan || match;
});

Expand All @@ -586,11 +592,10 @@ class Bot {
}
const avatarURL = this.getDiscordAvatar(author, channel);
const username = _.padEnd(author.substring(0, USERNAME_MAX_LENGTH), USERNAME_MIN_LENGTH, '_');
webhook.client.sendMessage(withMentions, {
webhook.client.send(withMentions, {
username,
text,
avatarURL,
disableEveryone: !canPingEveryone,
disableMentions: canPingEveryone ? 'none' : 'everyone',
}).catch(logger.error);
return;
}
Expand Down
104 changes: 61 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"license": "MIT",
"dependencies": {
"commander": "^6.0.0",
"discord.js": "11.5.1",
"discord.js": "12.2.0",
"irc-colors": "1.5.0",
"irc-formatting": "1.0.0-rc3",
"irc-upd": "0.11.0",
Expand Down
Loading