103 lines
2.5 KiB
TypeScript
103 lines
2.5 KiB
TypeScript
import Command from "./command";
|
|
|
|
import db from "../things/db";
|
|
import bot from "../things/bot";
|
|
|
|
import { Constants } from "eris";
|
|
import parse from "parse-duration";
|
|
import logger from "../things/logger";
|
|
|
|
const remind: Command = {
|
|
name: "remind",
|
|
description: "Reminds you to do something.",
|
|
draft: false,
|
|
options: [
|
|
{
|
|
name: "when",
|
|
description: "When to remind you.",
|
|
required: true,
|
|
type: Constants.ApplicationCommandOptionTypes.STRING
|
|
},
|
|
{
|
|
name: "what",
|
|
description: "What to remind you about.",
|
|
type: Constants.ApplicationCommandOptionTypes.STRING
|
|
}
|
|
],
|
|
command: async (interaction) => {
|
|
const whenOption = interaction.data.options?.find((x) => x.name === "when");
|
|
const whatOption = interaction.data.options?.find((x) => x.name === "what");
|
|
|
|
let what = null;
|
|
if (whatOption?.type === Constants.ApplicationCommandOptionTypes.STRING)
|
|
what = whatOption.value;
|
|
|
|
// fucking ts
|
|
if (whenOption?.type != Constants.ApplicationCommandOptionTypes.STRING)
|
|
return;
|
|
|
|
const when = parse(whenOption.value);
|
|
if (when === null) {
|
|
await interaction.createMessage(
|
|
"I didn't understand that time. Try something like `3h` or `2 days`."
|
|
);
|
|
return;
|
|
}
|
|
|
|
const sendAt = new Date(Date.now() + when);
|
|
|
|
await db.reminder.create({
|
|
data: {
|
|
user: (interaction.member || interaction.user)!.id,
|
|
createdAt: new Date(),
|
|
sendAt,
|
|
message: what
|
|
}
|
|
});
|
|
|
|
await interaction.createMessage(
|
|
`Reminder set <t:${Math.floor(sendAt.getTime() / 1000)}:R>.`
|
|
);
|
|
}
|
|
};
|
|
|
|
async function checkDB() {
|
|
const reminders = await db.reminder.findMany({
|
|
where: {
|
|
sendAt: {
|
|
lte: new Date()
|
|
}
|
|
}
|
|
});
|
|
|
|
for (const reminder of reminders) {
|
|
try {
|
|
let user = await bot.users.find((x) => x.id === reminder.user);
|
|
if (!user) user = await bot.getRESTUser(reminder.user);
|
|
if (!user) throw new Error("User not found");
|
|
|
|
const dmChannel = await user.getDMChannel();
|
|
|
|
await dmChannel.createMessage(
|
|
`You asked me to remind you about ${
|
|
reminder.message ?? "something"
|
|
} <t:${Math.floor(reminder.createdAt.getTime() / 1000)}:R>.`
|
|
);
|
|
} catch (err) {
|
|
logger.error(
|
|
{ err, id: reminder.user },
|
|
"Couldn't send reminder to user"
|
|
);
|
|
}
|
|
|
|
await db.reminder.delete({
|
|
where: {
|
|
id: reminder.id
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
setInterval(checkDB, 1000 * 5);
|
|
|
|
export default remind;
|