From 0b1d52e4181c429967bff686aa1fc02d400f6199 Mon Sep 17 00:00:00 2001 From: Joonas Date: Sun, 12 Feb 2023 02:25:12 +0200 Subject: [PATCH] added coinflip command and also did some refactorig --- commands/megis.js | 114 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 94 insertions(+), 20 deletions(-) diff --git a/commands/megis.js b/commands/megis.js index f5ea69a..f90ac99 100644 --- a/commands/megis.js +++ b/commands/megis.js @@ -41,6 +41,14 @@ const command = { }, }); + if (!user) { + await client.say( + msg.channelName, + `${msg.displayName}, run '% megis start' first` + ); + return; + } + if (user && msg.senderUsername !== user.name) { const changeName = await prisma.user.update({ where: { @@ -52,11 +60,9 @@ const command = { }); if (!changeName) { - await client.say( - msg.channelName, - `${msg.displayName}, detected name change but something went wrong changing it` + throw new Error( + `Tried to change username from ${user.name} to ${msg.senderUsername} but failed` ); - return; } } @@ -100,14 +106,6 @@ const command = { } case "cdr": { - if (!user) { - await client.say( - msg.channelName, - `${msg.displayName}, run '% megis start' first` - ); - return; - } - if (user.megis.cdrActive) { await client.say( msg.channelName, @@ -169,6 +167,90 @@ const command = { break; } + case "cf": { + if ( + (isNaN(splitted[3]) && splitted[3] !== "all") || + splitted[3] === 0 + ) { + await client.say( + msg.channelName, + `${msg.displayName}, arg needs to be a valid number or 'all'` + ); + + return; + } + + if (splitted[3] !== "all") { + if (parseInt(splitted[3]) > user.megis.megis) { + await client.say( + msg.channelName, + `${msg.displayName}, you don't have that much megis!` + ); + return; + } + } + + const takeBet = await prisma.user.update({ + where: { + userId: msg.senderUserID, + }, + data: { + megis: { + update: { + megis: { + decrement: + splitted[3] === "all" + ? parseInt(user.megis.megis) + : parseInt(splitted[3]), + }, + }, + }, + }, + include: { + megis: true, + }, + }); + + const flip = Math.floor(Math.random() * 2); + let result; + + if (flip === 1) { + result = await prisma.user.update({ + where: { + userId: msg.senderUserID, + }, + data: { + megis: { + update: { + megis: { + increment: + splitted[3] === "all" + ? user.megis.megis * 3 + : splitted[3] * 3, + }, + }, + }, + }, + include: { + megis: true, + }, + }); + } + + await client.say( + msg.channelName, + result + ? `${msg.displayName}, you won! +${ + splitted[3] === "all" ? user.megis.megis * 2 : splitted[3] * 2 + } Current megis: ${result.megis.megis}` + : `${msg.displayName}, you lost! -${ + splitted[3] === "all" ? user.megis.megis : splitted[3] + } Current megis: ${takeBet.megis.megis}` + ); + + break; + } + case "lb": { const allMegisers = await prisma.user.findMany({ take: 3, @@ -196,14 +278,6 @@ const command = { } default: { - if (!user) { - await client.say( - msg.channelName, - `${msg.displayName}, run '% megis start' first` - ); - return; - } - if (!user.megis.cdrActive) { const timeMs = now; if (timeMs < user.megis.megisCd) {