eco-bet: use askYesOrNo to clean up

This commit is contained in:
フズキ 2021-01-26 19:37:31 +01:00
parent 499aea9e66
commit 055a57e928
No known key found for this signature in database
GPG key ID: AD7750AB4625F1DD

View file

@ -75,17 +75,13 @@ export const BetCommand = new Command({
// else if (duration >= {threshold}) // else if (duration >= {threshold})
// return channel.send("Too long idk"); // return channel.send("Too long idk");
// [Potential pertinence to use the ask later on?]
// Ask target whether or not they want to take the bet. // Ask target whether or not they want to take the bet.
const msg = await channel.send(`<@${target.id}>, do you want to take this bet of ${$(amount).pluralise("Mon", "s")}`); const takeBet = await askYesOrNo(
await msg.react("✅"); await channel.send(`<@${target.id}>, do you want to take this bet of ${$(amount).pluralise("Mon", "s")}`),
await msg.react("⛔"); target.id
);
// Wait for a reaction. if (takeBet) {
msg.awaitReactions(
async (reaction, user) => {
// If target accepts: set bet up.
if (user.id === target.id && reaction.emoji.name === "✅") {
// [ISSUE: volatile storage] // [ISSUE: volatile storage]
// Remove amount money from both parts to avoid duplication of money. // Remove amount money from both parts to avoid duplication of money.
sender.money -= amount; sender.money -= amount;
@ -101,19 +97,19 @@ export const BetCommand = new Command({
// When bet is over, give a vote to ask people their thoughts. // When bet is over, give a vote to ask people their thoughts.
const voteMsg = await channel.send(`VOTE: do you think that <@${target.id}> has won the bet?`); const voteMsg = await channel.send(`VOTE: do you think that <@${target.id}> has won the bet?`);
await voteMsg.react("✅"); await voteMsg.react("✅");
await voteMsg.react(""); await voteMsg.react("");
// Filter reactions to only collect the pertinent ones. // Filter reactions to only collect the pertinent ones.
voteMsg.awaitReactions( voteMsg.awaitReactions(
(reaction, user) => { (reaction, user) => {
return ["✅", ""].includes(reaction.emoji.name); return ["✅", ""].includes(reaction.emoji.name);
}, },
// [Pertinence to make configurable on the fly.] // [Pertinence to make configurable on the fly.]
{ time: parseDuration("2m") } { time: parseDuration("2m") }
).then(reactions => { ).then(reactions => {
// Count votes // Count votes
const ok = reactions.filter(reaction => reaction.emoji.name === "✅").size; const ok = reactions.filter(reaction => reaction.emoji.name === "✅").size;
const no = reactions.filter(reaction => reaction.emoji.name === "").size; const no = reactions.filter(reaction => reaction.emoji.name === "").size;
if (ok > no) { if (ok > no) {
receiver.money += amount * 2; receiver.money += amount * 2;
@ -132,16 +128,8 @@ export const BetCommand = new Command({
}); });
}, duration); }, duration);
} }
// If target refuses: notify and stop. else
else if (user.id === target.id && reaction.emoji.name === "⛔")
await channel.send(`<@${target.id}> has rejected your bet, <@${author.id}>`); await channel.send(`<@${target.id}> has rejected your bet, <@${author.id}>`);
return false;
},
// [Lesser pertinence to make configurable on the fly.]
// Wait for a minute, and delete the asking message after that.
{ time: 60000 }
).then(() => msg.delete());
} }
} }
}) })