From 583eb6f86ff659262e06b6f3544294345a7e91ec Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sat, 19 Mar 2022 23:31:45 -0500 Subject: [PATCH] Defer all replies before responding to prevent timeouts --- commands/about.js | 8 +++++--- commands/help.js | 4 +++- commands/rounds.js | 10 ++++++---- commands/top.js | 6 ++++-- commands/train.js | 6 ++++-- events/interactionCreate.js | 1 - 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/commands/about.js b/commands/about.js index ccf1856..dfede13 100644 --- a/commands/about.js +++ b/commands/about.js @@ -28,6 +28,8 @@ module.exports = { return subcommand; }), async execute(interaction) { + await interaction.deferReply(); + const client = interaction.client; const action = interaction.options.getSubcommand(); if (action === 'contributors') { @@ -37,7 +39,7 @@ module.exports = { .setTimestamp() .setColor('#ffffff'); - interaction.reply({ embeds: [contributorEmbed] }); + interaction.followUp({ embeds: [contributorEmbed] }); } else if (action === 'changelog') { const gitRepoLocation = __dirname; @@ -58,7 +60,7 @@ module.exports = { changelogEmbed.addField(commit.abbrevHash, `> \`Hash:\`${commit.hash}\n> \`Subject:\`${commit.subject}\n> \`Author:\`${commit.authorName}\n> \`Date:\`${commit.authorDateRel}\n> \`Link\`: [GitHub](https://github.com/ADawesomeguy/AwesomeSciBo/commit/${commit.hash})\n`); }); - interaction.reply({ embeds: [changelogEmbed] }); + interaction.followUp({ embeds: [changelogEmbed] }); } else if (action === 'bot') { await client.guilds.fetch(); @@ -70,7 +72,7 @@ module.exports = { .addField('Training Users', `${trainingDocuments}`, true) .setTimestamp(); - interaction.reply({ embeds: [aboutBotEmbed] }); + interaction.followUp({ embeds: [aboutBotEmbed] }); } }, }; diff --git a/commands/help.js b/commands/help.js index 154795e..85c86ae 100644 --- a/commands/help.js +++ b/commands/help.js @@ -6,9 +6,11 @@ module.exports = { .setName('help') .setDescription('Replies with a help message explaining what the bot can do'), async execute(interaction) { + await interaction.deferReply(); + const helpEmbed = new MessageEmbed() .setDescription('AwesomeSciBo has migrated to using slash commands! You can take a look at the different commands by typing `/` and clicking on the AwesomeSciBo icon.') .setColor('#ffffff'); - interaction.reply({ embeds: [helpEmbed] }); + interaction.followUp({ embeds: [helpEmbed] }); }, }; diff --git a/commands/rounds.js b/commands/rounds.js index be18c2a..ba7e08b 100644 --- a/commands/rounds.js +++ b/commands/rounds.js @@ -29,6 +29,8 @@ module.exports = { return subcommand; }), async execute(interaction) { + await interaction.deferReply(); + const action = interaction.options.getSubcommand(); if (action === 'generate') { let i; @@ -70,7 +72,7 @@ module.exports = { log({ logger: 'rounds', content: `Saving round to DB failed: ${err}`, level: 'error' }); return; } - interaction.reply(`Here's your round: https://api.adawesome.tech/round/${round._id.toString()}`, { ephemeral: true }); + interaction.followUp(`Here's your round: https://api.adawesome.tech/round/${round._id.toString()}`, { ephemeral: true }); }); }); } @@ -78,7 +80,7 @@ module.exports = { let roundsList = await generatedRound.find({ requestedBy: interaction.user.id }).sort({ timestamp: -1 }); let finalMessage = ''; if (!roundsList) { - interaction.reply('You haven\'t requested any roundsList!'); + interaction.followUp('You haven\'t requested any roundsList!'); return; } @@ -96,7 +98,7 @@ module.exports = { .setDescription(finalMessage) .setTimestamp(); - interaction.reply({ + interaction.followUp({ embeds: [roundsListEmbed], ephemeral: true, }); @@ -105,7 +107,7 @@ module.exports = { const totalCount = await generatedRound.countDocuments({}); const userCount = await generatedRound.countDocuments({ requestedBy: interaction.user.id }); - interaction.reply(`Total Hits: ${totalCount}\nYour Hits: ${userCount}`); + interaction.followUp(`Total Hits: ${totalCount}\nYour Hits: ${userCount}`); } }, }; diff --git a/commands/top.js b/commands/top.js index f08413e..cd0ada7 100644 --- a/commands/top.js +++ b/commands/top.js @@ -9,6 +9,8 @@ module.exports = { .setName('top') .setDescription('Lists top ten scores across servers (server specific leaderboard WIP)'), async execute(interaction) { + await interaction.deferReply(); + let messageContent = ''; userScore .find({}) @@ -20,7 +22,7 @@ module.exports = { } if (obj.length < 10) { // Need at least 10 scores for top 10 - return interaction.reply( + return interaction.followUp( `There are only ${obj.length} users, we need at least 10!`, ); } @@ -32,7 +34,7 @@ module.exports = { .setDescription(messageContent) .setColor('#ffffff'); - interaction.reply({ embeds: [leaderboardEmbed] }); + interaction.followUp({ embeds: [leaderboardEmbed] }); }); }, }; diff --git a/commands/train.js b/commands/train.js index 98fb1f1..716c3d6 100644 --- a/commands/train.js +++ b/commands/train.js @@ -29,6 +29,8 @@ module.exports = { return option; }), async execute(interaction) { + await interaction.deferReply(); + const subject = interaction.options.get('subject') ? interaction.options.get('subject').value : null; const authorId = interaction.user.id; let score; @@ -81,7 +83,7 @@ module.exports = { categoryArray = ['ENERGY']; break; default: - interaction.reply( + interaction.followUp( new MessageEmbed() .setDescription('<:red_x:816791117671825409> Not a valid subject!') .setColor('#ffffff'), @@ -96,7 +98,7 @@ module.exports = { const tossupQuestion = data.tossup_question; const tossupAnswer = data.tossup_answer; const messageFilter = message => message.author.id === interaction.author.id; - interaction.reply({ content: decode(tossupQuestion) + `\n\n||Source: ${data.uri}||` }) + interaction.followUp({ content: decode(tossupQuestion) + `\n\n||Source: ${data.uri}||` }) .then(() => { interaction.channel.awaitMessages({ messageFilter, diff --git a/events/interactionCreate.js b/events/interactionCreate.js index 7751d79..750fad0 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -8,7 +8,6 @@ module.exports = { if (!interaction.isCommand()) return; const command = client.commands.get(interaction.commandName); - if (!command) return; try {