twitch_bot/commands/megis.js

103 lines
2.4 KiB
JavaScript
Raw Normal View History

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",
};
2023-02-09 12:32:12 +01:00
// 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
const command = {
name: "megis",
desc: "economy command: get megis once per hour",
restricted: false,
mod: false,
run: async (client, msg) => {
2023-02-08 16:59:41 +01:00
const user = await prisma.user.findUnique({
where: {
userId: msg.senderUserID,
},
2023-02-08 16:59:41 +01:00
include: {
megis: true,
},
});
if (!user) {
const createUser = await prisma.user.create({
data: {
userId: msg.senderUserID,
megis: {
create: {
megis: 0,
},
},
},
2023-02-08 16:59:41 +01:00
});
if (!createUser) {
await client.say(
msg.channelName,
`${msg.displayName}, something went wrong with adding you to the database`
);
return;
}
2023-02-08 16:59:41 +01:00
}
if (user) {
const timeMs = new Date().getTime();
const timeoutMs = new Date(user.megis.updatedAt).getTime() + timeout;
if (timeMs < timeoutMs) {
2023-02-08 16:59:41 +01:00
await client.say(
msg.channelName,
`${msg.displayName}, Two cans per day... (on timeout for ${Math.floor(
Math.abs(timeoutMs - timeMs) / 1000 / 60
2023-02-08 16:59:41 +01:00
)} minutes 🕒 ) `
);
return;
}
}
const megis =
Object.keys(megisTypes)[
Math.floor(Math.random() * Object.keys(megisTypes).length)
];
2023-02-08 16:59:41 +01:00
const updateUser = await prisma.user.update({
where: {
userId: msg.senderUserID,
2023-02-08 16:59:41 +01:00
},
data: {
megis: {
2023-02-08 16:59:41 +01:00
update: {
megis: {
increment: parseInt(megis),
},
},
},
},
include: {
megis: true,
},
});
if (!updateUser) {
await client.say(
msg.channelName,
`${msg.displayName}, something went wrong`
);
return;
}
await client.say(
msg.channelName,
`${msg.displayName}, +${megis} megis! ${megisTypes[megis]} Total megis: ${updateUser.megis.megis}`
);
},
};
module.exports = { command };