twitch_bot/commands/megis.js

85 lines
1.9 KiB
JavaScript

const prisma = require("../clients/prisma");
const timeout = 1800000;
const megisTypes = {
0: "bcaa megis PoroSad",
7: "lemon sugar free megis BroBalt",
15: "sugar free megis! PogChamp",
30: "normal megis!!! AngelThump",
};
const command = {
name: "megis",
desc: "economy command: get megis once per hour",
restricted: false,
mod: false,
run: async (client, msg) => {
const megis =
Object.keys(megisTypes)[
Math.floor(Math.random() * Object.keys(megisTypes).length)
];
const user = await prisma.user.findUnique({
where: {
userId: msg.senderUserID,
},
include: {
megis: true,
},
});
if (!user) {
const createUser = await prisma.user.create({
data: {
userId: msg.senderUserID,
megis: {
create: {
megis: 0,
},
},
},
});
}
const timeMs = new Date().getTime();
const timeoutMs = new Date(user.megis.updatedAt).getTime() + timeout;
if (user) {
if (timeMs < timeoutMs) {
await client.say(
msg.channelName,
`${
msg.displayName
}, remember kids, only one megis per hour! (on timeout ${Math.floor(
Math.abs(timeoutMs - timeMs) / 1000 / 60
)} minutes 🕒 ) `
);
return;
}
}
const updateUser = await prisma.user.update({
where: {
userId: msg.senderUserID,
},
data: {
megis: {
update: {
megis: {
increment: parseInt(megis),
},
},
},
},
include: {
megis: true,
},
});
await client.say(
msg.channelName,
`${msg.displayName}, +${megis}! ${megisTypes[megis]} Total megis: ${updateUser.megis.megis}`
);
},
};
module.exports = { command };