refactor: update to discord.js v13 (#203)

* build: bump djs version

* refactor(add): update

* refactor(blacklist): update

* refactor(close): update

* refactor(help): update

* refactor(new): update

* refactor(panel): update

* refactor(remove): update

* refactor(stats): update

* refactor(survey): update

* refactor(tag): update

* refactor(topic): update

* refactor(guild): update, now Structures doesn't exist

Also I don't know if this would work actually, let's see

* refactor(guild_member)

* refactor(index.js): add intents

I can't understand how you do the indentation, sorry if I made a mistake

* Update package.json

* fix(intents): add `DIRECT_MESSAGES` intents


- also fix code style and indentation

* style: fix properties order and indentation

* fix(guild)

* Update and rename message.js to messageCreate.js

* Update guild.js

* refactor(manager): fix

* Update manager.js

* fix(help)

* style: fix spacing

* fix: permission overwrites

* fix(new)

* fix(presence): rename activity to activities

* fix(presence): fix debug message

* fix: update channel types

* Update new.js

* fix(embeds): update footer function

* fix: broken code

* style: add new lines around embed blocks

* fix(messages): update remaining `send()` calls

* fix(messages): i missed one

* build: replace fastify logger with standard logger

* refactor: update message and reaction collectors

Co-authored-by: Isaac <git@eartharoid.me>
This commit is contained in:
Puneet Gopinath
2021-08-17 01:35:20 +05:30
committed by GitHub
parent 96d1ddc48c
commit 0f10908ee0
30 changed files with 749 additions and 619 deletions

View File

@@ -122,7 +122,7 @@ module.exports = class Command {
* @returns {Promise<Message>}
*/
async sendUsage(channel, alias) {
const settings = await channel.guild.getSettings();
const settings = await this.client.utils.getSettings(channel.guild);
if (!alias) alias = this.name;
const prefix = settings.command_prefix;
@@ -156,7 +156,7 @@ module.exports = class Command {
}
this.args.forEach(arg => addArgs(embed, arg));
return await channel.send(embed);
return await channel.send({ embeds: [embed] });
}

View File

@@ -72,7 +72,7 @@ module.exports = class CommandManager {
async handle(message) {
if (message.author.bot) return; // ignore self and other bots
const settings = await message.guild.getSettings();
const settings = await this.client.utils.getSettings(message.guild);
const i18n = this.client.i18n.getLocale(settings.locale);
const prefix = settings.command_prefix;
const escaped_prefix = prefix.toLowerCase().replace(/(?=\W)/g, '\\'); // (lazy) escape every character so it can be used in a RexExp
@@ -122,14 +122,16 @@ module.exports = class CommandManager {
if (!bot_permissions.has(required_bot_permissions)) {
const perms = required_bot_permissions.map(p => `\`${p}\``).join(', ');
if (bot_permissions.has(['EMBED_LINKS', 'SEND_MESSAGES'])) {
await message.channel.send(
new MessageEmbed()
.setColor('ORANGE')
.setTitle(i18n('bot.missing_permissions.title'))
.setDescription(i18n('bot.missing_permissions.description', perms))
);
await message.channel.send({
embeds: [
new MessageEmbed()
.setColor('ORANGE')
.setTitle(i18n('bot.missing_permissions.title'))
.setDescription(i18n('bot.missing_permissions.description', perms))
]
});
} else if (bot_permissions.has('SEND_MESSAGES')) {
await message.channel.send('⚠️ ' + i18n('bot.missing_permissions.description', perms));
await message.channel.send({ content: '⚠️ ' + i18n('bot.missing_permissions.description', perms) });
} else if (bot_permissions.has('ADD_REACTIONS')) {
await message.react('⚠️');
} else {
@@ -138,24 +140,28 @@ module.exports = class CommandManager {
return;
}
const missing_permissions = cmd.permissions instanceof Array && !message.member.hasPermission(cmd.permissions);
const missing_permissions = cmd.permissions instanceof Array && !message.member.permissions.has(cmd.permissions);
if (missing_permissions) {
const perms = cmd.permissions.map(p => `\`${p}\``).join(', ');
return await message.channel.send(
new MessageEmbed()
.setColor(settings.error_colour)
.setTitle(i18n('missing_permissions.title'))
.setDescription(i18n('missing_permissions.description', perms))
);
return await message.channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.error_colour)
.setTitle(i18n('missing_permissions.title'))
.setDescription(i18n('missing_permissions.description', perms))
]
});
}
if (cmd.staff_only && await message.member.isStaff() === false) {
return await message.channel.send(
new MessageEmbed()
.setColor(settings.error_colour)
.setTitle(i18n('staff_only.title'))
.setDescription(i18n('staff_only.description'))
);
if (cmd.staff_only && await this.client.utils.isStaff(message.member) === false) {
return await message.channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.error_colour)
.setTitle(i18n('staff_only.title'))
.setDescription(i18n('staff_only.description'))
]
});
}
let args = raw_args;
@@ -165,12 +171,14 @@ module.exports = class CommandManager {
args = parseArgs(cmd.args, { argv: argv(raw_args) });
} catch (error) {
const help_cmd = `${settings.command_prefix}${i18n('commands.help.name')} ${cmd_name}`;
return await message.channel.send(
new MessageEmbed()
.setColor(settings.error_colour)
.setTitle(i18n('cmd_usage.invalid_named_args.title'))
.setDescription(i18n('cmd_usage.invalid_named_args.description', error.message, help_cmd))
);
return await message.channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.error_colour)
.setTitle(i18n('cmd_usage.invalid_named_args.title'))
.setDescription(i18n('cmd_usage.invalid_named_args.description', error.message, help_cmd))
]
});
}
for (const arg of cmd.args) {
if (arg.required && args[arg.name] === undefined) {
@@ -191,13 +199,15 @@ module.exports = class CommandManager {
} catch (e) {
this.client.log.warn(`An error occurred whilst executing the ${cmd.name} command`);
this.client.log.error(e);
await message.channel.send(
new MessageEmbed()
.setColor('ORANGE')
.setTitle(i18n('command_execution_error.title'))
.setDescription(i18n('command_execution_error.description'))
); // hopefully no user will ever see this message
await message.channel.send({
embeds: [
new MessageEmbed()
.setColor('ORANGE')
.setTitle(i18n('command_execution_error.title'))
.setDescription(i18n('command_execution_error.description'))
]
}); // hopefully no user will ever see this message
}
}
};
};

View File

@@ -1,9 +0,0 @@
const fs = require('fs');
const { path } = require('../utils/fs');
module.exports = () => {
const files = fs.readdirSync(path('./src/structures'))
.filter(file => file.endsWith('.js'));
for (const file of files) require(`../structures/${file}`);
};

View File

@@ -2,7 +2,6 @@
const EventEmitter = require('events');
const TicketArchives = require('./archives');
const { MessageEmbed } = require('discord.js');
const { footer } = require('../../utils/discord');
/** Manages tickets */
module.exports = class TicketManager extends EventEmitter {
@@ -51,10 +50,10 @@ module.exports = class TicketManager extends EventEmitter {
parent: category_id,
reason: `${creator.user.tag} requested a new ticket channel`,
topic: `${creator}${topic.length > 0 ? ` | ${topic}` : ''}`,
type: 'text'
type: 'GUILD_TEXT'
});
t_channel.updateOverwrite(creator_id, {
t_channel.permissionOverwrites.edit(creator_id, {
ATTACH_FILES: true,
READ_MESSAGE_HISTORY: true,
SEND_MESSAGES: true,
@@ -71,7 +70,7 @@ module.exports = class TicketManager extends EventEmitter {
});
(async () => {
const settings = await guild.getSettings();
const settings = await this.client.utils.getSettings(guild);
const i18n = this.client.i18n.getLocale(settings.locale);
topic = t_row.topic
@@ -85,11 +84,11 @@ module.exports = class TicketManager extends EventEmitter {
? '@here'
: `<@&${id}>`);
await t_channel.send(mentions.join(', '));
await t_channel.send({ content: mentions.join(', ') });
}
if (cat_row.image) {
await t_channel.send(cat_row.image);
await t_channel.send({ content: cat_row.image });
}
const description = cat_row.opening_message
@@ -103,7 +102,10 @@ module.exports = class TicketManager extends EventEmitter {
if (topic) embed.addField(i18n('ticket.opening_message.fields.topic'), topic);
const sent = await t_channel.send(creator.user.toString(), embed);
const sent = await t_channel.send({
content: creator.user.toString(),
embeds: [embed]
});
await sent.pin({ reason: 'Ticket opening message' });
await t_row.update({ opening_message: sent.id });
@@ -128,17 +130,21 @@ module.exports = class TicketManager extends EventEmitter {
}
if (cat_row.require_topic && topic.length === 0) {
const collector_message = await t_channel.send(
new MessageEmbed()
.setColor(settings.colour)
.setTitle('⚠️ ' + i18n('commands.new.request_topic.title'))
.setDescription(i18n('commands.new.request_topic.description'))
.setFooter(footer(settings.footer, i18n('collector_expires_in', 120)), guild.iconURL())
);
const collector_message = await t_channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.colour)
.setTitle('⚠️ ' + i18n('commands.new.request_topic.title'))
.setDescription(i18n('commands.new.request_topic.description'))
.setFooter(this.client.utils.footer(settings.footer, i18n('collector_expires_in', 120)), guild.iconURL())
]
});
const collector_filter = message => message.author.id === t_row.creator;
const collector = t_channel.createMessageCollector(collector_filter, { time: 120000 });
const filter = message => message.author.id === t_row.creator;
const collector = t_channel.createMessageCollector({
filter,
time: 120000
});
collector.on('collect', async message => {
topic = message.content;
@@ -161,22 +167,26 @@ module.exports = class TicketManager extends EventEmitter {
.delete()
.catch(() => this.client.log.warn('Failed to delete topic collector message'));
if (cat_row.opening_questions) {
await t_channel.send(
new MessageEmbed()
.setColor(settings.colour)
.setDescription(i18n('ticket.questions', questions))
.setFooter(settings.footer, guild.iconURL())
);
await t_channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.colour)
.setDescription(i18n('ticket.questions', questions))
.setFooter(settings.footer, guild.iconURL())
]
});
}
});
} else {
if (cat_row.opening_questions) {
await t_channel.send(
new MessageEmbed()
.setColor(settings.colour)
.setDescription(i18n('ticket.questions', questions))
.setFooter(settings.footer, guild.iconURL())
);
await t_channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.colour)
.setDescription(i18n('ticket.questions', questions))
.setFooter(settings.footer, guild.iconURL())
]
});
}
}
})();
@@ -203,7 +213,7 @@ module.exports = class TicketManager extends EventEmitter {
this.emit('beforeClose', ticket_id);
const guild = this.client.guilds.cache.get(t_row.guild);
const settings = await guild.getSettings();
const settings = await this.client.utils.getSettings(guild);
const i18n = this.client.i18n.getLocale(settings.locale);
const channel = await this.client.channels.fetch(t_row.id);
@@ -224,14 +234,16 @@ module.exports = class TicketManager extends EventEmitter {
const description = reason
? i18n('ticket.closed_by_member_with_reason.description', closer.user.toString(), reason)
: i18n('ticket.closed_by_member.description', closer.user.toString());
await channel.send(
new MessageEmbed()
.setColor(settings.success_colour)
.setAuthor(closer.user.username, closer.user.displayAvatarURL())
.setTitle(i18n('ticket.closed.title'))
.setDescription(description)
.setFooter(settings.footer, guild.iconURL())
);
await channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.success_colour)
.setAuthor(closer.user.username, closer.user.displayAvatarURL())
.setTitle(i18n('ticket.closed.title'))
.setDescription(description)
.setFooter(settings.footer, guild.iconURL())
]
});
setTimeout(async () => {
await channel.delete(`Ticket channel closed by ${closer.user.tag}${reason ? `: "${reason}"` : ''}`);
@@ -242,13 +254,15 @@ module.exports = class TicketManager extends EventEmitter {
const description = reason
? i18n('ticket.closed_with_reason.description')
: i18n('ticket.closed.description');
await channel.send(
new MessageEmbed()
.setColor(settings.success_colour)
.setTitle(i18n('ticket.closed.title'))
.setDescription(description)
.setFooter(settings.footer, guild.iconURL())
);
await channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.success_colour)
.setTitle(i18n('ticket.closed.title'))
.setDescription(description)
.setFooter(settings.footer, guild.iconURL())
]
});
setTimeout(async () => {
await channel.delete(`Ticket channel closed${reason ? `: "${reason}"` : ''}`);
@@ -272,20 +286,25 @@ module.exports = class TicketManager extends EventEmitter {
});
if (survey) {
const r_collector_message = await channel.send(
creator.toString(),
new MessageEmbed()
.setColor(settings.colour)
.setTitle(i18n('ticket.survey.start.title'))
.setDescription(i18n('ticket.survey.start.description', creator.toString(), survey.questions.length))
.setFooter(i18n('collector_expires_in', 60))
);
const r_collector_message = await channel.send({
content: creator.toString(),
embeds: [
new MessageEmbed()
.setColor(settings.colour)
.setTitle(i18n('ticket.survey.start.title'))
.setDescription(i18n('ticket.survey.start.description', creator.toString(), survey.questions.length))
.setFooter(i18n('collector_expires_in', 60))
]
});
await r_collector_message.react('✅');
const collector_filter = (reaction, user) => user.id === creator.user.id && reaction.emoji.name === '✅';
const filter = (reaction, user) => user.id === creator.user.id && reaction.emoji.name === '✅';
const r_collector = r_collector_message.createReactionCollector(collector_filter, { time: 60000 });
const r_collector = r_collector_message.createReactionCollector({
filter,
time: 60000
});
r_collector.on('collect', async () => {
r_collector.stop();
@@ -293,17 +312,20 @@ module.exports = class TicketManager extends EventEmitter {
let answers = [];
let number = 1;
for (const question of survey.questions) {
await channel.send(
new MessageEmbed()
.setColor(settings.colour)
.setTitle(`${number++}/${survey.questions.length}`)
.setDescription(question)
.setFooter(i18n('collector_expires_in', 60))
);
await channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.colour)
.setTitle(`${number++}/${survey.questions.length}`)
.setDescription(question)
.setFooter(i18n('collector_expires_in', 60))
]
});
try {
const collected = await channel.awaitMessages(filter, {
const collected = await channel.awaitMessages({
errors: ['time'],
filter,
max: 1,
time: 60000
});
@@ -313,13 +335,15 @@ module.exports = class TicketManager extends EventEmitter {
}
}
await channel.send(
new MessageEmbed()
.setColor(settings.success_colour)
.setTitle(i18n('ticket.survey.complete.title'))
.setDescription(i18n('ticket.survey.complete.description'))
.setFooter(settings.footer, guild.iconURL())
);
await channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.success_colour)
.setTitle(i18n('ticket.survey.complete.title'))
.setDescription(i18n('ticket.survey.complete.description'))
.setFooter(settings.footer, guild.iconURL())
]
});
answers = answers.map(a => this.client.cryptr.encrypt(a));
await this.client.db.models.SurveyResponse.create({