Skip to content

Commit

Permalink
updated /roulette: added balance check, fixed reply action location
Browse files Browse the repository at this point in the history
  • Loading branch information
vb2007 committed Aug 5, 2024
1 parent a7a738a commit 75f899f
Showing 1 changed file with 61 additions and 51 deletions.
112 changes: 61 additions & 51 deletions commands/economy/roulette.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,75 +20,85 @@ module.exports = {
{ name: "Black", value: "black" },
{ name: "Green", value: "green" }
)
.setRequired(true)
)
.setRequired(true))
.setDMPermission(false),
async execute(interaction) {
if (!interaction.inGuild()) {
var replyContent = "You can only play roulette in a server.";
}
else {
const rouletteNumbers = Array.from({ length: 37}, (_, index) => index + 1); //37 = european roulette. this might be configureable later.
const interactionUserId = interaction.user.id;
const amount = interaction.options.getInteger("amount");

var query = await db.query("SELECT balance FROM economy WHERE userId = ?", [interactionUserId]);
var userBalance = query[0]?.balance;

function generateRandomOutcome() {
const randomIndex = Math.floor(Math.random() * rouletteNumbers.length);
const number = rouletteNumbers[randomIndex]
//green if number is 0, black if it's devideable by 2, red othervise
const color = number === 0 ? "green" : (number % 2 === 0 ? "black" : "red");
return { number, color };
if (userBalance < amount) {
var replyContent = `You can't play with that much money!\nYour current balance is only \`$${userBalance}\`.`;
}
else {
const rouletteNumbers = Array.from({ length: 37}, (_, index) => index + 1); //37 = european roulette. this might be configureable later.

var interactionUserId = interaction.user.id;
function generateRandomOutcome() {
const randomIndex = Math.floor(Math.random() * rouletteNumbers.length);
const number = rouletteNumbers[randomIndex]
//green if number is 0, black if it's devideable by 2, red othervise
const color = number === 0 ? "green" : (number % 2 === 0 ? "black" : "red");
return { number, color };
}

var amount = interaction.options.getInteger("amount");
var guessedColor = interaction.options.getString("color");
var guessedColor = interaction.options.getString("color");

var randomOutcome = generateRandomOutcome();
var randomOutcome = generateRandomOutcome();

var randomColor = randomOutcome.color;
var randomNumber = randomOutcome.number;
var randomColor = randomOutcome.color;
var randomNumber = randomOutcome.number;

switch (guessedColor) {
case randomColor:
await db.query("UPDATE economy SET balance = balance + ? WHERE userId = ?",
[
amount * 2,
interactionUserId
]
);
switch (guessedColor) {
case randomColor:
await db.query("UPDATE economy SET balance = balance + ? WHERE userId = ?",
[
amount * 2,
interactionUserId
]
);

var replyContent = `The ball landed on ${randomColor} ${randomNumber}.\nYour guess was ${randomColor} as well! :money_mouth:`;
break;
case !randomColor:
await db.query("UPDATE economy SET balance = balance - ? WHERE userId = ?",
[
amount,
interactionUserId
]
);
var replyContent = `The ball landed on ${randomColor} ${randomNumber}.\nYour guess was ${randomColor} as well! :money_mouth:`;
break;
case "red" || "black" || "green":
await db.query("UPDATE economy SET balance = balance - ? WHERE userId = ?",
[
amount,
interactionUserId
]
);

var replyContent = `The ball landed on ${randomColor} ${randomNumber}.\nYour guess was ${randomColor}.\nMaybe try your luck again. :upside_down:`;
break
default:
var replyContent = "The color you've chosen is invalid.\nPlease choose from *red*, *black* or *green*.";
}

var replyContent = `The ball landed on ${randomColor} ${randomNumber}.\nYour guess was ${randomColor}.\nMaybe try your luck again. :upside_down:`;
break
default:
var replyContent = "The color you've chosen is invalid.\nPlease choose from *red*, *black* or *green*.";

}
}

var embedReply = new EmbedBuilder({
color: 0x5F0FD6,
title: "Playing roulette.",
description: replyContent,
timestamp: new Date().toISOString(),
footer: {
text: `Requested by: ${interaction.user.username}`,
icon_url: interaction.user.displayAvatarURL({ dynamic: true })
}
});
var embedReply = new EmbedBuilder({
color: 0x5F0FD6,
title: "Playing roulette.",
description: replyContent,
timestamp: new Date().toISOString(),
footer: {
text: `Requested by: ${interaction.user.username}`,
icon_url: interaction.user.displayAvatarURL({ dynamic: true })
}
});

await interaction.reply({ embeds: [embedReply] })
await interaction.reply({ embeds: [embedReply] })

//logging
const response = JSON.stringify(embedReply.toJSON());
await logToFileAndDatabase(interaction, response);
}
//logging
const response = JSON.stringify(embedReply.toJSON());
await logToFileAndDatabase(interaction, response);
}
}

0 comments on commit 75f899f

Please sign in to comment.