* add comments

* add timeout when creating a reminder
This commit is contained in:
Mylloon 2022-11-06 18:25:21 +01:00
parent 024beb00b2
commit d42fd9072b
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 86 additions and 5 deletions

View file

@ -3,4 +3,7 @@ export const once = true;
/** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-ready */ /** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-ready */
export default async () => { export default async () => {
console.log('Connected to Discord!'); console.log('Connected to Discord!');
// Restart all the timeout about reminders here
// TODO: Reminders timeout
}; };

View file

@ -58,6 +58,7 @@ const initDatabase = (db: Database) => {
channel_id INTEGER, \ channel_id INTEGER, \
creation_date INTEGER, \ creation_date INTEGER, \
user_id INTEGER, \ user_id INTEGER, \
guild_id INTEGER \ guild_id INTEGER, \
timeout_id INTEGER \
);'); );');
}; };

View file

@ -1,12 +1,21 @@
import { Client } from 'discord.js'; import { Client } from 'discord.js';
import { strToSeconds } from './time'; import { strToSeconds } from './time';
/**
* Option possible for reminders
*/
enum OptionReminder { enum OptionReminder {
/** No parameters */
Nothing, Nothing,
/** @ */
Mention, Mention,
/** p */
DirectMessage, DirectMessage,
} }
/**
* Store data about the remidner
*/
type infoReminder = { type infoReminder = {
locale: Map<string, Map<string, string>>, locale: Map<string, Map<string, string>>,
message: string | null, message: string | null,
@ -16,6 +25,11 @@ type infoReminder = {
guildId: string | null guildId: string | null
} }
/**
* Split the time and the extra args `p` and `@`
* @param time raw text from user
* @returns An object with the time and the option
*/
const splitTime = (time: string) => { const splitTime = (time: string) => {
if (time?.endsWith('@')) { if (time?.endsWith('@')) {
return { time: time.slice(0, -1), option: OptionReminder.Mention }; return { time: time.slice(0, -1), option: OptionReminder.Mention };
@ -26,20 +40,31 @@ const splitTime = (time: string) => {
return { time: time, option: OptionReminder.Nothing }; return { time: time, option: OptionReminder.Nothing };
}; };
/**
* Create a new reminder
* @param client Client
* @param time raw text from user about the time wanted
* @param info data about the context of the reminder
* @returns Promise resolution of the sql request
*/
export const newReminder = async (client: Client, time: string, info: infoReminder) => export const newReminder = async (client: Client, time: string, info: infoReminder) =>
new Promise((ok, ko) => { new Promise((ok, ko) => {
const data = splitTime(time); const data = splitTime(time);
const timeout = strToSeconds(data.time);
const timeoutId = setTimeoutReminder(client, info, data.option, timeout);
// Add the remind to the db // Add the remind to the db
client.db.run('INSERT INTO reminder ( \ client.db.run('INSERT INTO reminder ( \
data, expiration_date, option_id, channel_id, creation_date, user_id, guild_id \ data, expiration_date, option_id, channel_id, creation_date, user_id, guild_id, timeout_id \
) VALUES ( ?, ?, ?, ?, ?, ?, ?);', [ ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? );', [
info.message, info.message,
info.createdAt + strToSeconds(data.time), info.createdAt + timeout,
data.option.valueOf(), data.option.valueOf(),
info.channelId, info.channelId,
info.createdAt, info.createdAt,
info.userId, info.userId,
info.guildId], (err) => { info.guildId,
timeoutId], (err) => {
if (err) { if (err) {
ko(err); ko(err);
} }
@ -48,3 +73,55 @@ export const newReminder = async (client: Client, time: string, info: infoRemind
ok(`${info.locale.get('c_reminder1')} ${data.time}.`); ok(`${info.locale.get('c_reminder1')} ${data.time}.`);
}); });
}); });
/**
* Create a timeout for a reminder
* @param client Client
* @param info info about the reminder
* @param option option used for this reminder (aka location of the response)
* @param timeout Amout of time before the reminder ends
* @returns timeout ID
*/
const setTimeoutReminder = (client: Client, info: infoReminder, option: OptionReminder, timeout: number) => {
// TODO: Reminders timeout
return Number(setTimeout(() => {
// Delete the reminder for the database
new Promise((ok, ko) => {
// Add the remind to the db
client.db.run('DELETE FROM reminder WHERE creation_date = ? AND user_id = ?', [info.createdAt, info.userId], (err) => {
if (err) {
ko(err);
}
// Send confirmation to user
ok(true);
});
}).then((val) => {
if (val != true) {
throw val;
}
// Send the message in the appropriate channel
if (option == OptionReminder.DirectMessage) {
// Direct message
const user = client.users.cache.get(info.userId);
// TODO: Locales
user?.send(`Here your reminder: ${info.message} :)`);
} else {
// Channel
client.channels.fetch(info.channelId ?? '').then((channel) => {
if (channel?.isTextBased()) {
// TODO: Check if option == OptionReminder.Mention and mention
// everyone who are in the message
// TODO: Test if a message exist
console.log(typeof info.message, info.message);
// TODO: Locales
channel.send(`Here your reminder: ${info.message} :)`);
}
});
}
});
}, timeout * 1000));
};