add cdr functionality to megis command
This commit is contained in:
parent
5635adf9a8
commit
86ade2b2f3
|
@ -7,6 +7,22 @@ const megisTypes = {
|
||||||
30: "normal megis!!! AngelThump",
|
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,
|
// this command does things that should be done seperately,
|
||||||
// probably will extract adding the user to the database to
|
// probably will extract adding the user to the database to
|
||||||
// another command when i start doing another command that also needs that functionality
|
// 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",
|
desc: "economy command: get megis once per hour",
|
||||||
restricted: false,
|
restricted: false,
|
||||||
mod: false,
|
mod: false,
|
||||||
run: async (client, msg) => {
|
run: async (client, msg, splitted) => {
|
||||||
const user = await prisma.user.findUnique({
|
const now = new Date().getTime();
|
||||||
|
let user = await prisma.user.findUnique({
|
||||||
where: {
|
where: {
|
||||||
userId: msg.senderUserID,
|
userId: msg.senderUserID,
|
||||||
},
|
},
|
||||||
|
@ -26,18 +43,22 @@ const command = {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
const createUser = await prisma.user.create({
|
user = await prisma.user.create({
|
||||||
data: {
|
data: {
|
||||||
userId: msg.senderUserID,
|
userId: msg.senderUserID,
|
||||||
megis: {
|
megis: {
|
||||||
create: {
|
create: {
|
||||||
megis: 0,
|
megis: 0,
|
||||||
|
cdrActive: false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
include: {
|
||||||
|
megis: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!createUser) {
|
if (!user) {
|
||||||
await client.say(
|
await client.say(
|
||||||
msg.channelName,
|
msg.channelName,
|
||||||
`${msg.displayName}, something went wrong with adding you to the database`
|
`${msg.displayName}, something went wrong with adding you to the database`
|
||||||
|
@ -47,18 +68,83 @@ const command = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (user) {
|
if (splitted[2] === "cdr") {
|
||||||
const timeMs = new Date().getTime();
|
if (user.megis.cdrActive) {
|
||||||
const timeoutMs = new Date(user.megis.updatedAt).getTime() + timeout;
|
|
||||||
if (timeMs < timeoutMs) {
|
|
||||||
await client.say(
|
await client.say(
|
||||||
msg.channelName,
|
msg.channelName,
|
||||||
`${msg.displayName}, Two cans per day... (on timeout for ${Math.floor(
|
`${msg.displayName}, you already have a cdr active`
|
||||||
Math.abs(timeoutMs - timeMs) / 1000 / 60
|
|
||||||
)} minutes 🕒 ) `
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return;
|
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 =
|
const megis =
|
||||||
|
@ -75,6 +161,8 @@ const command = {
|
||||||
megis: {
|
megis: {
|
||||||
increment: parseInt(megis),
|
increment: parseInt(megis),
|
||||||
},
|
},
|
||||||
|
megisCd: String(now + 1800000),
|
||||||
|
cdrActive: false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
2
prisma/migrations/20230209113847_cdr/migration.sql
Normal file
2
prisma/migrations/20230209113847_cdr/migration.sql
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "Megis" ADD COLUMN "cdr" BOOLEAN;
|
25
prisma/migrations/20230209121730_cdrstuff/migration.sql
Normal file
25
prisma/migrations/20230209121730_cdrstuff/migration.sql
Normal 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;
|
|
@ -20,10 +20,13 @@ model User {
|
||||||
}
|
}
|
||||||
|
|
||||||
model Megis {
|
model Megis {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||||
userId String @unique
|
userId String @unique
|
||||||
megis Int
|
megis Int
|
||||||
|
cdrActive Boolean?
|
||||||
|
megisCd String?
|
||||||
|
cdrCd String?
|
||||||
|
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
|
|
Loading…
Reference in a new issue