112 lines
2.7 KiB
TypeScript
112 lines
2.7 KiB
TypeScript
import Command from "./command";
|
|
|
|
import db from "../things/db";
|
|
import bot from "../things/bot";
|
|
|
|
import { Constants, InteractionDataOptionsString } from "eris";
|
|
import parse from "parse-duration";
|
|
import logger from "../things/logger";
|
|
|
|
import {
|
|
Optional,
|
|
unravelOption,
|
|
unravelOptionalOption
|
|
} from "../utils/options";
|
|
|
|
const remind: Command = {
|
|
name: "remind",
|
|
description: "Reminds you to do something.",
|
|
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) => {
|
|
// i love abusing types
|
|
const options = interaction.data.options!;
|
|
const whenOption = unravelOption<InteractionDataOptionsString>(
|
|
"when",
|
|
options
|
|
);
|
|
const whatOption = unravelOptionalOption<
|
|
Optional<InteractionDataOptionsString>
|
|
>("what", options);
|
|
|
|
const when = parse(whenOption.value);
|
|
const what = whatOption?.value;
|
|
|
|
if (when === null) {
|
|
await interaction.createMessage({
|
|
content:
|
|
"I didn't understand that time. Try something like `3h` or `2 days`.",
|
|
flags: Constants.MessageFlags.EPHEMERAL
|
|
});
|
|
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({
|
|
content: `Reminder set <t:${Math.floor(sendAt.getTime() / 1000)}:R>.`,
|
|
flags: Constants.MessageFlags.EPHEMERAL
|
|
});
|
|
}
|
|
};
|
|
|
|
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;
|