added coinflip command and also did some refactorig

This commit is contained in:
Joonas 2023-02-12 02:25:12 +02:00
parent d8c2513954
commit 0b1d52e418
1 changed files with 94 additions and 20 deletions

View File

@ -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) { if (user && msg.senderUsername !== user.name) {
const changeName = await prisma.user.update({ const changeName = await prisma.user.update({
where: { where: {
@ -52,11 +60,9 @@ const command = {
}); });
if (!changeName) { if (!changeName) {
await client.say( throw new Error(
msg.channelName, `Tried to change username from ${user.name} to ${msg.senderUsername} but failed`
`${msg.displayName}, detected name change but something went wrong changing it`
); );
return;
} }
} }
@ -100,14 +106,6 @@ const command = {
} }
case "cdr": { case "cdr": {
if (!user) {
await client.say(
msg.channelName,
`${msg.displayName}, run '% megis start' first`
);
return;
}
if (user.megis.cdrActive) { if (user.megis.cdrActive) {
await client.say( await client.say(
msg.channelName, msg.channelName,
@ -169,6 +167,90 @@ const command = {
break; 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": { case "lb": {
const allMegisers = await prisma.user.findMany({ const allMegisers = await prisma.user.findMany({
take: 3, take: 3,
@ -196,14 +278,6 @@ const command = {
} }
default: { default: {
if (!user) {
await client.say(
msg.channelName,
`${msg.displayName}, run '% megis start' first`
);
return;
}
if (!user.megis.cdrActive) { if (!user.megis.cdrActive) {
const timeMs = now; const timeMs = now;
if (timeMs < user.megis.megisCd) { if (timeMs < user.megis.megisCd) {