defer the response to prevent a fun little race condition

This commit is contained in:
Elijah R 2024-06-04 01:16:47 -04:00
parent 20292a7160
commit be525180c3

View file

@ -24,6 +24,7 @@ if (!config.token) {
client.on('interactionCreate', async i => { client.on('interactionCreate', async i => {
if (i instanceof CommandInteraction) { if (i instanceof CommandInteraction) {
await i.deferReply();
switch (i.commandName) { switch (i.commandName) {
case "poll": case "poll":
const rcp_polls = JSON.parse(fs.readFileSync('data/rcp_polls.json', "utf-8")); const rcp_polls = JSON.parse(fs.readFileSync('data/rcp_polls.json', "utf-8"));
@ -31,7 +32,7 @@ if (!config.token) {
Object.keys(rcp_polls.approval.polls).forEach(poll => { Object.keys(rcp_polls.approval.polls).forEach(poll => {
polls += `:red_circle: **${rcp_polls.approval.polls[poll].pollster}** - (**${rcp_polls.approval.polls[poll].date}**), ${rcp_polls.approval.polls[poll].sample}, **${rcp_polls.approval.polls[poll].approve}**% approve, **${rcp_polls.approval.polls[poll].disapprove}**% disapprove (spread: **${rcp_polls.approval.polls[poll].spread}**)\n`; polls += `:red_circle: **${rcp_polls.approval.polls[poll].pollster}** - (**${rcp_polls.approval.polls[poll].date}**), ${rcp_polls.approval.polls[poll].sample}, **${rcp_polls.approval.polls[poll].approve}**% approve, **${rcp_polls.approval.polls[poll].disapprove}**% disapprove (spread: **${rcp_polls.approval.polls[poll].spread}**)\n`;
}); });
await i.reply(`Biden Approval Rating\n\n${polls}`); await i.editReply(`Biden Approval Rating\n\n${polls}`);
break; break;
case "simulate": case "simulate":
var electionname = (i.options as CommandInteractionOptionResolver).getSubcommand(); var electionname = (i.options as CommandInteractionOptionResolver).getSubcommand();
@ -56,11 +57,11 @@ if (!config.token) {
var color = (i.options as CommandInteractionOptionResolver).getString("with_color") || "#bfab22"; var color = (i.options as CommandInteractionOptionResolver).getString("with_color") || "#bfab22";
var odds = (i.options as CommandInteractionOptionResolver).getNumber("with_odds") || 0.33; var odds = (i.options as CommandInteractionOptionResolver).getNumber("with_odds") || 0.33;
if (!/^#[0-9A-Fa-f]{3,6}$/.test(color)) { if (!/^#[0-9A-Fa-f]{3,6}$/.test(color)) {
await i.reply("Please provide a valid hex color code"); await i.editReply("Please provide a valid hex color code");
return; return;
} }
if (election.candidates.some(c => c.party === party || c.name === newcandidate)) { if (election.candidates.some(c => c.party === party || c.name === newcandidate)) {
await i.reply("A candidate with that name or party already exists"); await i.editReply("A candidate with that name or party already exists");
return return
} }
election.candidates.push({ election.candidates.push({
@ -84,7 +85,7 @@ if (!config.token) {
})) }))
.setImage("attachment://election.png") .setImage("attachment://election.png")
.setTimestamp(); .setTimestamp();
await i.reply({embeds: [embed], files: [{attachment: result.png, name: "election.png"}]}); await i.editReply({embeds: [embed], files: [{attachment: result.png, name: "election.png"}]});
} }
} }
}); });