feat: Reminders #44

Merged
Anri merged 54 commits from feat/reminders into main 2023-01-17 12:15:15 +01:00
4 changed files with 29 additions and 21 deletions
Showing only changes of commit 024beb00b2 - Show all commits

View file

@ -148,10 +148,13 @@ export default {
// New reminder // New reminder
case loc_default?.get(`c_${filename}_sub1_name`) case loc_default?.get(`c_${filename}_sub1_name`)
?.toLowerCase() ?? '': { ?.toLowerCase() ?? '': {
// If time is already renseigned
const time = interaction.options.getString(loc_default?.get(`c_${filename}_sub1_opt1_name`) as string); const time = interaction.options.getString(loc_default?.get(`c_${filename}_sub1_opt1_name`) as string);
if (time != null) { if (time != null) {
// Cli // Use the cli because we already have enough data
return newReminder(client, time, { return newReminder(client, time, {
locale: loc,
message: interaction.options.getString(loc_default?.get('c_reminder_sub1_opt2_name') as string), message: interaction.options.getString(loc_default?.get('c_reminder_sub1_opt2_name') as string),
createdAt: interaction.createdAt.getTime(), createdAt: interaction.createdAt.getTime(),
channelId: interaction.channelId, channelId: interaction.channelId,
@ -162,7 +165,7 @@ export default {
ephemeral: true, ephemeral: true,
})); }));
} else { } else {
// GUI // Show modal to user to get at least the time
const modal = new ModalBuilder() const modal = new ModalBuilder()
.setCustomId('reminderGUI') .setCustomId('reminderGUI')
.setTitle(loc?.get('c_reminder_name').capitalize()); .setTitle(loc?.get('c_reminder_name').capitalize());
@ -181,7 +184,10 @@ export default {
.setPlaceholder(loc?.get('c_reminder_sub1_opt2_desc')) .setPlaceholder(loc?.get('c_reminder_sub1_opt2_desc'))
.setRequired(false); .setRequired(false);
modal.addComponents(new ActionRowBuilder<ModalActionRowComponentBuilder>().addComponents(timeGUI), new ActionRowBuilder<ModalActionRowComponentBuilder>().addComponents(messageGUI)); modal.addComponents(
new ActionRowBuilder<ModalActionRowComponentBuilder>().addComponents(timeGUI),
new ActionRowBuilder<ModalActionRowComponentBuilder>().addComponents(messageGUI)
);
return interaction.showModal(modal); return interaction.showModal(modal);
} }
@ -196,7 +202,8 @@ export default {
// Quand aucun utilisateur est spécifié, il faut affiché ses // Quand aucun utilisateur est spécifié, il faut affiché ses
// rappels, sinon affiché les rappels de la personne qui fait // rappels, sinon affiché les rappels de la personne qui fait
// la commande. // la commande.
break;
return interaction.reply('TODO');
} }
// Delete a reminder // Delete a reminder
case loc_default?.get(`c_${filename}_sub3_name`) case loc_default?.get(`c_${filename}_sub3_name`)
@ -204,7 +211,8 @@ export default {
// TODO: Message simple qui indique que l'on a supprimé // TODO: Message simple qui indique que l'on a supprimé
// le reminder. Penser à check l'appartenance du reminder. // le reminder. Penser à check l'appartenance du reminder.
// On donne l'ID du reminder en option. // On donne l'ID du reminder en option.
break;
return interaction.reply('TODO');
} }
default: { default: {
console.error(`${__filename}: unknown subcommand (${subcommand})`); console.error(`${__filename}: unknown subcommand (${subcommand})`);

View file

@ -32,5 +32,6 @@
"c_reminder_sub3_name": "efface", "c_reminder_sub3_name": "efface",
"c_reminder_sub3_desc": "Supprime un rappel", "c_reminder_sub3_desc": "Supprime un rappel",
"c_reminder_sub3_opt1_name": "ID", "c_reminder_sub3_opt1_name": "ID",
"c_reminder_sub3_opt1_desc": "Rappel à supprimé" "c_reminder_sub3_opt1_desc": "Rappel à supprimé",
"c_reminder1": "Un rappel a été configuré pour dans"
} }

View file

@ -1,4 +1,5 @@
import { Client, ModalSubmitInteraction } from 'discord.js'; import { Client, ModalSubmitInteraction } from 'discord.js';
import { getLocale } from '../../utils/locales';
import { getFilename } from '../../utils/misc'; import { getFilename } from '../../utils/misc';
import { newReminder } from '../../utils/reminder'; import { newReminder } from '../../utils/reminder';
@ -6,8 +7,9 @@ export default {
data: { data: {
name: getFilename(__filename), name: getFilename(__filename),
}, },
interaction: async (interaction: ModalSubmitInteraction, client: Client) => { interaction: async (interaction: ModalSubmitInteraction, client: Client) =>
return newReminder(client, interaction.fields.fields.get('reminderGUI-time')?.value as string, { newReminder(client, interaction.fields.fields.get('reminderGUI-time')?.value as string, {
locale: getLocale(client, interaction.locale),
message: interaction.fields.fields.get('reminderGUI-message')?.value ?? null, message: interaction.fields.fields.get('reminderGUI-message')?.value ?? null,
createdAt: interaction.createdAt.getTime(), createdAt: interaction.createdAt.getTime(),
channelId: interaction.channelId, channelId: interaction.channelId,
@ -16,6 +18,5 @@ export default {
}).then((msg) => interaction.reply({ }).then((msg) => interaction.reply({
content: msg as string, content: msg as string,
ephemeral: true, ephemeral: true,
})); })),
},
}; };

View file

@ -1,13 +1,14 @@
import { Client } from 'discord.js'; import { Client } from 'discord.js';
import { strToSeconds } from './time'; import { strToSeconds } from './time';
export enum OptionReminder { enum OptionReminder {
Nothing, Nothing,
Mention, Mention,
DirectMessage, DirectMessage,
} }
export type infoReminder = { type infoReminder = {
locale: Map<string, Map<string, string>>,
message: string | null, message: string | null,
createdAt: number, createdAt: number,
channelId: string | null, channelId: string | null,
@ -25,12 +26,11 @@ const splitTime = (time: string) => {
return { time: time, option: OptionReminder.Nothing }; return { time: time, option: OptionReminder.Nothing };
}; };
export const newReminder = async (client: Client, time: string, info: infoReminder) => { export const newReminder = async (client: Client, time: string, info: infoReminder) =>
new Promise((ok, ko) => {
const data = splitTime(time); const data = splitTime(time);
// Add the remind to the db // Add the remind to the db
return new Promise((ok, ko) => { client.db.run('INSERT INTO reminder ( \
const res = 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 \
) VALUES ( ?, ?, ?, ?, ?, ?, ?);', [ ) VALUES ( ?, ?, ?, ?, ?, ?, ?);', [
info.message, info.message,
@ -45,8 +45,6 @@ export const newReminder = async (client: Client, time: string, info: infoRemind
} }
// Send confirmation to user // Send confirmation to user
// TODO: local ok(`${info.locale.get('c_reminder1')} ${data.time}.`);
ok(`ratio: ${info.message}, ${data.time}=${strToSeconds(data.time)}, ${data.option}, ${res}`);
}); });
}); });
};