Files
blitzcrank/commands/calendar/remind.ts
Drew Malzahn e5e38e6100 WIP: Basic event loop, database code
The monster is starting to resemble a working program
2025-06-30 00:28:47 -04:00

106 lines
2.9 KiB
TypeScript

// import type { ChatInputCommandInteraction } from "discord.js";
import type { Client, ChatInputCommandInteraction } from "discord.js";
import { InteractionResponse, SlashCommandBuilder } from "discord.js";
import * as sequelize from "sequelize";
import * as chrono from "chrono-node";
import { sql } from "../../database";
export default function (settings: { client: Client }) {
const client = settings.client;
const Reminders = sql.define("reminders", {
id: {
type: sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
userId: sequelize.INTEGER,
text: sequelize.TEXT,
trigger: sequelize.DATE,
isValid: sequelize.BOOLEAN,
requestChannel: sequelize.BIGINT,
requestMessage: sequelize.BIGINT,
});
/**
* Create an infinitely looping function which tries to determine what
* reminders will be triggering soon, and creating a timer process for
* them.
*/
async function reminderLoop() {
console.log("Reminder loop");
const results = await Reminders.findAll({
// TODO: This means that there's less than or equal to one minute left before
// the reminder goes off.
where: sequelize.literal(
"(julianday('now') - julianday(trigger)) <= 1.0 AND (julianday('now') - julianday(trigger)) > 0",
),
});
for (const rmd of results) {
setTimeout(async () => {
const channel = client.channels.cache.get("1234"); // TODO
if (channel?.isSendable()) {
await channel.send(`Reminder: ${rmd.get("text")}`);
}
});
}
setTimeout(reminderLoop, 60 * 1000); // Loop after 60 seconds
}
async function execute(interaction: ChatInputCommandInteraction) {
try {
const when = chrono.parseDate(
interaction.options.getString("when") ?? "now",
);
if (!when) {
return interaction.reply(
`Sorry, I don't understand '${when}' as a date`,
);
}
const reminder = await Reminders.create({
userId: interaction.user.id,
text: interaction.options.getString("what"),
trigger: when, // TODO
requestMessage: interaction.id,
requestChannel: interaction.channelId,
isValid: true,
});
console.info(`Created Reminder(${reminder.get("id")})`);
return interaction.reply(
`Ok, I'll remind you at ${when?.toDateString()}`,
);
} catch (_error) {
return interaction.reply(
"Something went wrong with adding your reminder.",
);
}
}
return {
Reminders: Reminders,
// reminderLoop: reminderLoop,
data: new SlashCommandBuilder()
.setName("remind")
.setDescription("Remind me to do something")
.addStringOption((option) =>
option
.setName("when")
.setDescription("Short description of when you want the reminder")
.setRequired(true),
)
.addStringOption((option) =>
option
.setName("what")
.setDescription(
"Short description of what you want to be reminded of",
)
.setRequired(true),
),
execute: execute,
initialize: async () => {
await Reminders.sync();
reminderLoop();
}
};
}