diceGame.js
Anonymous
JavaScript
09/25/2025 6:53 PM
4.3 KB
16
Indexable
Never
const fs = require("fs");
const path = require("path");
const leaderboardPath = path.join(__dirname, "diceGameLeaderboard.json");
let leaderboard = {};
// Load leaderboard
if (fs.existsSync(leaderboardPath)) {
leaderboard = JSON.parse(fs.readFileSync(leaderboardPath, "utf-8"));
}
// Game state
let rolls = {};
let gameActive = false;
function saveLeaderboard() {
fs.writeFileSync(leaderboardPath, JSON.stringify(leaderboard, null, 2));
}
function handleCommand(command, args, message, client, config) {
if (command === "diceroll" || command === "dice") {
const requiredPerm = config.permissions.diceroll;
if (
!config.superAdmins.includes(message.author.id) &&
!message.member.permissions.has(requiredPerm)
) {
return message.reply(
`⚠️ You don’t have permission to start a dice game.`
);
}
if (gameActive) {
return message.channel.send("⚠️ A dice game is already running!");
}
// Parse arguments
maxRoll = parseInt(args[0]) || 100;
let timeArg = args[1] || "30s";
const timeMatch = timeArg.match(/^(\d+)(s|m)?$/);
if (timeMatch) {
const value = parseInt(timeMatch[1]);
const unit = timeMatch[2] || "s";
roundDuration =
unit === "h"
? value * 3600000
: unit === "m"
? value * 60000
: value * 1000;
} else {
roundDuration = 30000;
}
rolls = {};
gameActive = true;
message.channel.send(
`🎲 Dice Roll Game started! Range: **1–${maxRoll}**. ` +
`Type \`!roll\` to roll. You have **${roundDuration / 1000} seconds**!`
);
gameTimer = setTimeout(() => {
endRound(message);
}, roundDuration);
}
// Separate roll command
if (command === "roll") {
if (!gameActive) {
return message.channel.send(
"❌ No dice game running right now! Type `!diceroll` to start one."
);
}
const userId = message.author.id;
if (rolls[userId]) {
return message.channel.send(`⚠️ <@${userId}>, you already rolled!`);
}
const roll = Math.floor(Math.random() * maxRoll) + 1;
rolls[userId] = roll;
leaderboard[userId] = leaderboard[userId] || {
highest: 0,
lowest: 1000,
totalRolls: 0,
};
leaderboard[userId].highest = Math.max(leaderboard[userId].highest, roll);
leaderboard[userId].lowest = Math.min(leaderboard[userId].lowest, roll);
leaderboard[userId].totalRolls += 1;
saveLeaderboard();
message.channel.send(`🎲 <@${userId}> rolled **${roll}**!`);
}
}
// 🔥 Function to finish round and check ties
async function endRound(message) {
gameActive = false;
if (Object.keys(rolls).length === 0) {
return message.channel.send("😅 Nobody rolled! No winner this round.");
}
let contenders = Object.keys(rolls);
let round = 1;
while (contenders.length > 1) {
await message.channel.send(
`⚔️ Tie-breaker Round ${round}! Players: ${contenders
.map((id) => `<@${id}>`)
.join(", ")}`
);
let roundRolls = {};
let highest = 0;
// Each player rolls with suspense
for (const userId of contenders) {
const reroll = Math.floor(Math.random() * maxRoll) + 1;
roundRolls[userId] = reroll;
await message.channel.send(`🎲 <@${userId}> rolls...`);
await wait(2000); // suspense delay (2s)
await message.channel.send(`➡️ **${reroll}**!`);
if (reroll > highest) highest = reroll;
await wait(1000); // pause before next player
}
// Narrow down to the survivors
contenders = Object.keys(roundRolls).filter(
(id) => roundRolls[id] === highest
);
if (contenders.length > 1) {
await message.channel.send(
`😮 Multiple players tied with **${highest}**! Moving to the next round...`
);
await wait(3000); // suspense before next round
}
round++;
}
// Final winner
await message.channel.send(
`🏆 <@${contenders[0]}> is the ultimate winner! 🎉`
);
}
// Simple helper to pause execution
function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
module.exports = { handleCommand };
Editor is loading...
Leave a Comment