I’m creating my very first Discord bot and I want it to pause and wait for my reply before proceeding, but I’m having trouble making it work. What steps should I follow to get my Discord bot to pause until it receives my message? Additionally, I’m aware that my usage of “throw new Error” may not be ideal; I am seeking an alternative way to halt execution.
Below is my attempt at the code:
const Discord = require('discord.js');
module.exports = class GameSetupCommand extends BaseCommand {
constructor() {
super('startgame', 'management', []);
}
async execute(client, message, args) {
if (!message.member.roles.cache.some((role) => role.name === "admin")) {
return message.channel.send("Only admins can initiate games!");
}
const gameAnnouncement = new Discord.MessageEmbed()
.setTitle('Upcoming Game Event!')
.setDescription('Insert game details here.')
.setColor("#FF0000")
.setTimestamp();
const availableMissionsList = "\`\`\`\n1 - 👑 Royal Challenge\n2 - 🚇 Urban Tactics\n3 - 🤬 Hamlet Havoc\n4 - 🥊 Bavarian Brawl\`\`\`";
const missionsArray = ['Royal Challenge', 'Urban Tactics', 'Hamlet Havoc', 'Bavarian Brawl'];
message.channel.send("Which mission would you like to select?");
message.channel.send(availableMissionsList);
message.channel.awaitMessages(m => m.author.id === message.author.id, {max: 1, time: 30000}).then(collected => {
const selectedMission = collected.first().content;
if (!missionsArray.includes(selectedMission)) {
message.channel.send("That’s not a valid selection!");
throw new Error("Invalid mission selected");
}
}).catch(() => {
message.channel.send('Time exceeded, operation canceled.');
throw new Error("No response received in time.");
});
message.channel.send("Would you like to use default classes? (yes/no)");
message.channel.awaitMessages(m => m.author.id === message.author.id, {max: 1, time: 30000}).then(collected => {
const response = collected.first().content;
if (response.toLowerCase() !== 'yes' && response.toLowerCase() !== 'no') {
message.channel.send("Please respond with either 'yes' or 'no'.");
throw new Error("Invalid response given");
}
}).catch(() => {
message.channel.send('Time exceeded, operation canceled.');
throw new Error("No response received in time.");
});
}
}