feat: Reminders #44
5 changed files with 113 additions and 50 deletions
|
@ -142,7 +142,6 @@ export default {
|
|||
const filename = getFilename(__filename);
|
||||
const loc = getLocale(client, interaction.locale);
|
||||
|
||||
/* Votre code ici */
|
||||
const subcommand = interaction.options.getSubcommand();
|
||||
switch (subcommand) {
|
||||
// New reminder
|
||||
|
@ -154,7 +153,7 @@ export default {
|
|||
if (time != null) {
|
||||
// Use the cli because we already have enough data
|
||||
return newReminder(client, time, {
|
||||
locale: loc,
|
||||
locale: interaction.locale,
|
||||
message: interaction.options.getString(loc_default?.get('c_reminder_sub1_opt2_name') as string),
|
||||
createdAt: interaction.createdAt.getTime(),
|
||||
channelId: interaction.channelId,
|
||||
|
|
|
@ -1,9 +1,60 @@
|
|||
import { Client } from 'discord.js';
|
||||
import { deleteReminder, infoReminder, OptionReminder, sendReminder } from '../../utils/reminder';
|
||||
|
||||
export const once = true;
|
||||
|
||||
type dbReminder = {
|
||||
id: number,
|
||||
data: string,
|
||||
expiration_date: number,
|
||||
option_id: number,
|
||||
channel_id: number,
|
||||
creation_date: number,
|
||||
user_id: number,
|
||||
guild_id: number,
|
||||
locale: string
|
||||
}
|
||||
|
||||
/** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-ready */
|
||||
export default async () => {
|
||||
export default async (client: Client) => {
|
||||
console.log('Connected to Discord!');
|
||||
|
||||
// Restart all the timeout about reminders here
|
||||
// TODO: Reminders timeout
|
||||
new Promise((ok, ko) => {
|
||||
// Fetch all reminders
|
||||
client.db.all('SELECT * FROM reminder', [], (err, row) => {
|
||||
if (err) {
|
||||
ko(err);
|
||||
}
|
||||
|
||||
// Send all the current reminders
|
||||
ok(row);
|
||||
});
|
||||
}).then((data) => {
|
||||
const now = Date.now();
|
||||
(data as dbReminder[]).forEach((element) => {
|
||||
if (element.expiration_date <= now) {
|
||||
// Reminder expired
|
||||
deleteReminder(client, element.creation_date, `${element.user_id}`).then((res) => {
|
||||
if (res != true) {
|
||||
throw res;
|
||||
}
|
||||
});
|
||||
const info = {
|
||||
locale: element.locale,
|
||||
message: element.data,
|
||||
createdAt: element.creation_date,
|
||||
channelId: `${element.channel_id}`,
|
||||
userId: `${element.user_id}`,
|
||||
guildId: `${element.guild_id}`,
|
||||
} as infoReminder;
|
||||
sendReminder(client, info, element.option_id as OptionReminder);
|
||||
} else {
|
||||
// Restart timeout
|
||||
}
|
||||
|
||||
});
|
||||
}).catch(err => {
|
||||
throw err;
|
||||
});
|
||||
};
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import { Client, ModalSubmitInteraction } from 'discord.js';
|
||||
import { getLocale } from '../../utils/locales';
|
||||
import { getFilename } from '../../utils/misc';
|
||||
import { newReminder } from '../../utils/reminder';
|
||||
|
||||
|
@ -9,7 +8,7 @@ export default {
|
|||
},
|
||||
interaction: async (interaction: ModalSubmitInteraction, client: Client) =>
|
||||
newReminder(client, interaction.fields.fields.get('reminderGUI-time')?.value as string, {
|
||||
locale: getLocale(client, interaction.locale),
|
||||
locale: interaction.locale,
|
||||
message: interaction.fields.fields.get('reminderGUI-message')?.value ?? null,
|
||||
createdAt: interaction.createdAt.getTime(),
|
||||
channelId: interaction.channelId,
|
||||
|
|
|
@ -66,6 +66,6 @@ const initDatabase = (db: Database) => {
|
|||
creation_date INTEGER, \
|
||||
user_id INTEGER, \
|
||||
guild_id INTEGER, \
|
||||
timeout_id INTEGER \
|
||||
locale TEXT \
|
||||
);');
|
||||
};
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import { Client } from 'discord.js';
|
||||
import { getLocale } from './locales';
|
||||
import { strToSeconds } from './time';
|
||||
|
||||
/**
|
||||
* Option possible for reminders
|
||||
*/
|
||||
enum OptionReminder {
|
||||
export enum OptionReminder {
|
||||
/** No parameters */
|
||||
Nothing,
|
||||
/** @ */
|
||||
|
@ -16,8 +17,8 @@ enum OptionReminder {
|
|||
/**
|
||||
* Store data about the remidner
|
||||
*/
|
||||
type infoReminder = {
|
||||
locale: Map<string, Map<string, string>>,
|
||||
export type infoReminder = {
|
||||
locale: string,
|
||||
message: string | null,
|
||||
createdAt: number,
|
||||
channelId: string | null,
|
||||
|
@ -51,11 +52,11 @@ export const newReminder = async (client: Client, time: string, info: infoRemind
|
|||
new Promise((ok, ko) => {
|
||||
const data = splitTime(time);
|
||||
const timeout = strToSeconds(data.time);
|
||||
const timeoutId = setTimeoutReminder(client, info, data.option, timeout);
|
||||
setTimeoutReminder(client, info, data.option, timeout);
|
||||
|
||||
// Add the remind to the db
|
||||
client.db.run('INSERT INTO reminder ( \
|
||||
data, expiration_date, option_id, channel_id, creation_date, user_id, guild_id, timeout_id \
|
||||
data, expiration_date, option_id, channel_id, creation_date, user_id, guild_id, locale \
|
||||
) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? );', [
|
||||
info.message,
|
||||
info.createdAt + timeout,
|
||||
|
@ -64,32 +65,29 @@ export const newReminder = async (client: Client, time: string, info: infoRemind
|
|||
info.createdAt,
|
||||
info.userId,
|
||||
info.guildId,
|
||||
timeoutId], (err) => {
|
||||
info.locale], (err) => {
|
||||
if (err) {
|
||||
ko(err);
|
||||
}
|
||||
|
||||
// Send confirmation to user
|
||||
ok(`${info.locale.get('c_reminder1')} ${data.time}.`);
|
||||
const loc = getLocale(client, info.locale);
|
||||
ok(`${loc.get('c_reminder1')} ${data.time}.`);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Create a timeout for a reminder
|
||||
* Delete 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
|
||||
* @param createdAt Creation of the reminder
|
||||
* @param userId User ID who created the reminder
|
||||
* @returns what the SQlite request sended
|
||||
*/
|
||||
const setTimeoutReminder = (client: Client, info: infoReminder, option: OptionReminder, timeout: number) => {
|
||||
// TODO: Reminders timeout
|
||||
return Number(setTimeout(() => {
|
||||
export const deleteReminder = (client: Client, createdAt: number, userId: string) => {
|
||||
// Delete the reminder for the database
|
||||
new Promise((ok, ko) => {
|
||||
return 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) => {
|
||||
client.db.run('DELETE FROM reminder WHERE creation_date = ? AND user_id = ?', [createdAt, userId], (err) => {
|
||||
if (err) {
|
||||
ko(err);
|
||||
}
|
||||
|
@ -97,11 +95,10 @@ const setTimeoutReminder = (client: Client, info: infoReminder, option: OptionRe
|
|||
// Send confirmation to user
|
||||
ok(true);
|
||||
});
|
||||
}).then((val) => {
|
||||
if (val != true) {
|
||||
throw val;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const sendReminder = (client: Client, info: infoReminder, option: OptionReminder) => {
|
||||
// Send the message in the appropriate channel
|
||||
if (option == OptionReminder.DirectMessage) {
|
||||
// Direct message
|
||||
|
@ -116,12 +113,29 @@ const setTimeoutReminder = (client: Client, info: infoReminder, option: OptionRe
|
|||
// everyone who are in the message
|
||||
|
||||
// TODO: Test if a message exist
|
||||
console.log(typeof info.message, info.message);
|
||||
// TODO: Locales
|
||||
// TODO: Embed
|
||||
channel.send(`Here your reminder: ${info.message} :)`);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}, timeout * 1000));
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
const setTimeoutReminder = (client: Client, info: infoReminder, option: OptionReminder, timeout: number) => {
|
||||
setTimeout(() => {
|
||||
deleteReminder(client, info.createdAt, info.userId).then((val) => {
|
||||
if (val != true) {
|
||||
throw val;
|
||||
}
|
||||
|
||||
sendReminder(client, info, option);
|
||||
});
|
||||
}, timeout * 1000);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue