Browse Source

Defer all replies before responding to prevent timeouts

pull/28/head
Abheek Dhawan 3 years ago
parent
commit
583eb6f86f
Signed by: abheekd GPG Key ID: 7BE81B8C14475B67
  1. 8
      commands/about.js
  2. 4
      commands/help.js
  3. 10
      commands/rounds.js
  4. 6
      commands/top.js
  5. 6
      commands/train.js
  6. 1
      events/interactionCreate.js

8
commands/about.js

@ -28,6 +28,8 @@ module.exports = {
return subcommand; return subcommand;
}), }),
async execute(interaction) { async execute(interaction) {
await interaction.deferReply();
const client = interaction.client; const client = interaction.client;
const action = interaction.options.getSubcommand(); const action = interaction.options.getSubcommand();
if (action === 'contributors') { if (action === 'contributors') {
@ -37,7 +39,7 @@ module.exports = {
.setTimestamp() .setTimestamp()
.setColor('#ffffff'); .setColor('#ffffff');
interaction.reply({ embeds: [contributorEmbed] }); interaction.followUp({ embeds: [contributorEmbed] });
} }
else if (action === 'changelog') { else if (action === 'changelog') {
const gitRepoLocation = __dirname; 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`); 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') { else if (action === 'bot') {
await client.guilds.fetch(); await client.guilds.fetch();
@ -70,7 +72,7 @@ module.exports = {
.addField('Training Users', `${trainingDocuments}`, true) .addField('Training Users', `${trainingDocuments}`, true)
.setTimestamp(); .setTimestamp();
interaction.reply({ embeds: [aboutBotEmbed] }); interaction.followUp({ embeds: [aboutBotEmbed] });
} }
}, },
}; };

4
commands/help.js

@ -6,9 +6,11 @@ module.exports = {
.setName('help') .setName('help')
.setDescription('Replies with a help message explaining what the bot can do'), .setDescription('Replies with a help message explaining what the bot can do'),
async execute(interaction) { async execute(interaction) {
await interaction.deferReply();
const helpEmbed = new MessageEmbed() 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.') .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'); .setColor('#ffffff');
interaction.reply({ embeds: [helpEmbed] }); interaction.followUp({ embeds: [helpEmbed] });
}, },
}; };

10
commands/rounds.js

@ -29,6 +29,8 @@ module.exports = {
return subcommand; return subcommand;
}), }),
async execute(interaction) { async execute(interaction) {
await interaction.deferReply();
const action = interaction.options.getSubcommand(); const action = interaction.options.getSubcommand();
if (action === 'generate') { if (action === 'generate') {
let i; let i;
@ -70,7 +72,7 @@ module.exports = {
log({ logger: 'rounds', content: `Saving round to DB failed: ${err}`, level: 'error' }); log({ logger: 'rounds', content: `Saving round to DB failed: ${err}`, level: 'error' });
return; 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 roundsList = await generatedRound.find({ requestedBy: interaction.user.id }).sort({ timestamp: -1 });
let finalMessage = ''; let finalMessage = '';
if (!roundsList) { if (!roundsList) {
interaction.reply('You haven\'t requested any roundsList!'); interaction.followUp('You haven\'t requested any roundsList!');
return; return;
} }
@ -96,7 +98,7 @@ module.exports = {
.setDescription(finalMessage) .setDescription(finalMessage)
.setTimestamp(); .setTimestamp();
interaction.reply({ interaction.followUp({
embeds: [roundsListEmbed], embeds: [roundsListEmbed],
ephemeral: true, ephemeral: true,
}); });
@ -105,7 +107,7 @@ module.exports = {
const totalCount = await generatedRound.countDocuments({}); const totalCount = await generatedRound.countDocuments({});
const userCount = await generatedRound.countDocuments({ requestedBy: interaction.user.id }); 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}`);
} }
}, },
}; };

6
commands/top.js

@ -9,6 +9,8 @@ module.exports = {
.setName('top') .setName('top')
.setDescription('Lists top ten scores across servers (server specific leaderboard WIP)'), .setDescription('Lists top ten scores across servers (server specific leaderboard WIP)'),
async execute(interaction) { async execute(interaction) {
await interaction.deferReply();
let messageContent = ''; let messageContent = '';
userScore userScore
.find({}) .find({})
@ -20,7 +22,7 @@ module.exports = {
} }
if (obj.length < 10) { if (obj.length < 10) {
// Need at least 10 scores for top 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!`, `There are only ${obj.length} users, we need at least 10!`,
); );
} }
@ -32,7 +34,7 @@ module.exports = {
.setDescription(messageContent) .setDescription(messageContent)
.setColor('#ffffff'); .setColor('#ffffff');
interaction.reply({ embeds: [leaderboardEmbed] }); interaction.followUp({ embeds: [leaderboardEmbed] });
}); });
}, },
}; };

6
commands/train.js

@ -29,6 +29,8 @@ module.exports = {
return option; return option;
}), }),
async execute(interaction) { async execute(interaction) {
await interaction.deferReply();
const subject = interaction.options.get('subject') ? interaction.options.get('subject').value : null; const subject = interaction.options.get('subject') ? interaction.options.get('subject').value : null;
const authorId = interaction.user.id; const authorId = interaction.user.id;
let score; let score;
@ -81,7 +83,7 @@ module.exports = {
categoryArray = ['ENERGY']; categoryArray = ['ENERGY'];
break; break;
default: default:
interaction.reply( interaction.followUp(
new MessageEmbed() new MessageEmbed()
.setDescription('<:red_x:816791117671825409> Not a valid subject!') .setDescription('<:red_x:816791117671825409> Not a valid subject!')
.setColor('#ffffff'), .setColor('#ffffff'),
@ -96,7 +98,7 @@ module.exports = {
const tossupQuestion = data.tossup_question; const tossupQuestion = data.tossup_question;
const tossupAnswer = data.tossup_answer; const tossupAnswer = data.tossup_answer;
const messageFilter = message => message.author.id === interaction.author.id; 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(() => { .then(() => {
interaction.channel.awaitMessages({ interaction.channel.awaitMessages({
messageFilter, messageFilter,

1
events/interactionCreate.js

@ -8,7 +8,6 @@ module.exports = {
if (!interaction.isCommand()) return; if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName); const command = client.commands.get(interaction.commandName);
if (!command) return; if (!command) return;
try { try {

Loading…
Cancel
Save