feat: Reminders #44

Merged
Anri merged 54 commits from feat/reminders into main 2023-01-17 12:15:15 +01:00
3 changed files with 82 additions and 10 deletions
Showing only changes of commit a52644e380 - Show all commits

View file

@ -48,5 +48,10 @@
"c_reminder10": "L'utilisateur n'a aucun rappel en attente ou page n°",
"c_reminder11": "vide",
"c_reminder12": "Précédent",
"c_reminder13": "Suivant"
"c_reminder13": "Suivant",
"c_reminder14": "Message envoyé en DM car le salon n'est plus disponible.",
"c_reminder15": "Message envoyé en DM car vous avez quitté",
"c_reminder16": "Message envoyé en DM car le serveur Discord n'est plus disponible.",
"c_reminder17": "Message d'il y a",
"c_reminder18": "Pas de message"
}

View file

@ -77,3 +77,27 @@ export const userWithNickname = (member: GuildMember) => {
return member.user.tag;
}
};
/**
* Move the text into backtick text, preserving mentions and links
* @param text Text
* @returns Formatted text
*/
export const cleanCodeBlock = (text: string) => {
text = `\`${text.trim()}\``;
// Keep mentions
text = text.replace(/(<@\d+>)/g, function(mention: string) {
return `\`${mention}\``;
});
// Keep links
text = text.replace(/(http[s]?:\/\/(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)/g, function(url: string) {
return `\`${url}\``;
});
// Fix issues
text = text.replace('``', '');
return text;
};

View file

@ -1,5 +1,6 @@
import { Client, Colors, EmbedBuilder, User } from 'discord.js';
import { getLocale } from './locales';
import { cleanCodeBlock } from './misc';
import { showDate, strToSeconds, timeDeltaToString } from './time';
/**
@ -113,23 +114,64 @@ export const deleteReminder = (client: Client, createdAt: string, userId: string
};
export const sendReminder = (client: Client, info: infoReminder, option: OptionReminder) => {
const loc = getLocale(client, info.locale);
// Send the message in the appropriate channel
if (option == OptionReminder.DirectMessage) {
// TODO: Embed
let message: string;
if (info.message === null) {
message = loc.get('c_reminder18');
} else {
message = cleanCodeBlock(info.message);
}
const embed = new EmbedBuilder()
.setColor('Random')
.setDescription(message)
.setTimestamp(info.createdAt);
let channelOk = false;
if (info.channelId !== null) {
if (client.channels.cache.get(info.channelId) !== undefined) {
channelOk = true;
} else {
embed.setFooter({ text: loc.get('c_reminder14') });
}
}
let guildOk = false;
if (info.guildId !== null) {
const guild = client.guilds.cache.get(info.guildId);
if (guild !== undefined) {
if (guild.members.cache.get(info.userId) !== undefined) {
guildOk = true;
} else {
embed.setFooter({ text: `${loc.get('c_reminder15')} ${guild.name}.` });
}
} else {
embed.setFooter({ text: loc.get('c_reminder16') });
}
}
if (option == OptionReminder.DirectMessage || !channelOk || !guildOk) {
// Direct message
const user = client.users.cache.get(info.userId);
// TODO: Locales
user?.send(`Here your reminder: ${info.message} :)`);
if (user !== undefined) {
user.send({ embeds: [embed] });
}
} 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
let content = `<@${info.userId}>`;
embed.setFooter({ text: `${loc.get('c_reminder17')} ${timeDeltaToString(info.createdAt)}` });
// TODO: Test if a message exist
// TODO: Locales
// TODO: Embed
channel.send(`Here your reminder: ${info.message} :)`);
// Mention everybody if needed
if (option == OptionReminder.Mention) {
(info.message?.match(/<@\d+>/g) ?? []).forEach(mention => {
content += ' ' + mention;
});
}
channel.send({ content, embeds: [embed] });
}
});
}
@ -286,6 +328,7 @@ export const embedListReminders = async (client: Client, user: User, guildId: st
if (pageMax <= 1) {
page = 1;
}
// TODO: Use Random color or force a color from args
const embed = new EmbedBuilder()
.setColor(Colors.DarkGrey)
.setDescription(`${loc.get('c_reminder5')} ${user}${loc.get('c_reminder6')} ${page}/${pageMax}`)