add cdr functionality to megis command

This commit is contained in:
Joonas 2023-02-09 14:57:25 +02:00
parent 5635adf9a8
commit 86ade2b2f3
4 changed files with 133 additions and 15 deletions

View File

@ -7,6 +7,22 @@ const megisTypes = {
30: "normal megis!!! AngelThump",
};
function secondsToHMS(secs) {
function z(n) {
return (n < 10 ? "0" : "") + n;
}
var sign = secs < 0 ? "-" : "";
secs = Math.abs(secs);
return (
sign +
z((secs / 3600) | 0) +
":" +
z(((secs % 3600) / 60) | 0) +
":" +
Math.floor(z(secs % 60))
);
}
// this command does things that should be done seperately,
// probably will extract adding the user to the database to
// another command when i start doing another command that also needs that functionality
@ -15,8 +31,9 @@ const command = {
desc: "economy command: get megis once per hour",
restricted: false,
mod: false,
run: async (client, msg) => {
const user = await prisma.user.findUnique({
run: async (client, msg, splitted) => {
const now = new Date().getTime();
let user = await prisma.user.findUnique({
where: {
userId: msg.senderUserID,
},
@ -26,18 +43,22 @@ const command = {
});
if (!user) {
const createUser = await prisma.user.create({
user = await prisma.user.create({
data: {
userId: msg.senderUserID,
megis: {
create: {
megis: 0,
cdrActive: false,
},
},
},
include: {
megis: true,
},
});
if (!createUser) {
if (!user) {
await client.say(
msg.channelName,
`${msg.displayName}, something went wrong with adding you to the database`
@ -47,18 +68,83 @@ const command = {
}
}
if (user) {
const timeMs = new Date().getTime();
const timeoutMs = new Date(user.megis.updatedAt).getTime() + timeout;
if (timeMs < timeoutMs) {
if (splitted[2] === "cdr") {
if (user.megis.cdrActive) {
await client.say(
msg.channelName,
`${msg.displayName}, Two cans per day... (on timeout for ${Math.floor(
Math.abs(timeoutMs - timeMs) / 1000 / 60
)} minutes 🕒 ) `
`${msg.displayName}, you already have a cdr active`
);
return;
}
if (now < user.megis.cdrCd) {
await client.say(
msg.channelName,
`${msg.displayName}, cdr still on cooldown for ${secondsToHMS(
(user.megis.cdrCd - now) / 1000
)} 🕒`
);
return;
}
if (user.megis.megis < 15) {
await client.say(
msg.channelName,
`${msg.displayName}, you have don't have enough megis to buy a cdr! You have ${user.megis.megis} `
);
return;
}
const cdr = await prisma.user.update({
where: {
userId: msg.senderUserID,
},
data: {
megis: {
update: {
megis: {
decrement: 15,
},
cdrActive: true,
cdrCd: String(now + 3600000),
},
},
},
});
if (!cdr) {
await client.say(
msg.channelName,
`${msg.displayName}, something went wrong with resetting your cdr`
);
return;
}
await client.say(
msg.channelName,
`${msg.displayName}, no sleep tonight... (-15 megis: cooldown reset)`
);
return;
}
if (user) {
if (!user.megis.cdrActive) {
const timeMs = now;
if (timeMs < user.megis.megisCd) {
await client.say(
msg.channelName,
`${
msg.displayName
}, Two cans per day... (on timeout for ${secondsToHMS(
(user.megis.megisCd - now) / 1000
)} 🕒 ) `
);
return;
}
}
}
const megis =
@ -75,6 +161,8 @@ const command = {
megis: {
increment: parseInt(megis),
},
megisCd: String(now + 1800000),
cdrActive: false,
},
},
},

View File

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Megis" ADD COLUMN "cdr" BOOLEAN;

View File

@ -0,0 +1,25 @@
/*
Warnings:
- You are about to drop the column `cdr` on the `Megis` table. All the data in the column will be lost.
*/
-- RedefineTables
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Megis" (
"id" TEXT NOT NULL PRIMARY KEY,
"userId" TEXT NOT NULL,
"megis" INTEGER NOT NULL,
"cdrActive" BOOLEAN,
"megisCd" TEXT,
"cdrCd" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
CONSTRAINT "Megis_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_Megis" ("createdAt", "id", "megis", "updatedAt", "userId") SELECT "createdAt", "id", "megis", "updatedAt", "userId" FROM "Megis";
DROP TABLE "Megis";
ALTER TABLE "new_Megis" RENAME TO "Megis";
CREATE UNIQUE INDEX "Megis_userId_key" ON "Megis"("userId");
PRAGMA foreign_key_check;
PRAGMA foreign_keys=ON;

View File

@ -20,10 +20,13 @@ model User {
}
model Megis {
id String @id @default(uuid())
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String @unique
megis Int
id String @id @default(uuid())
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String @unique
megis Int
cdrActive Boolean?
megisCd String?
cdrCd String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt