Skip to content

Commit

Permalink
fix formatting issues + add recurring reminders
Browse files Browse the repository at this point in the history
  • Loading branch information
wdhdev committed Dec 3, 2024
1 parent 441910a commit 85cab30
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/commands/reminders/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ const command: Command = {
.addFields(
{ name: "Reason", value: reminder.reason },
{ name: "Set", value: `<t:${reminder.reminder_set.toString().slice(0, -3)}:f>`, inline: true },
{ name: "Due", value: `<t:${(reminder.reminder_set + reminder.delay).toString().slice(0, -3)}:R>`, inline: true }
{ name: "Due", value: `<t:${(Number(reminder.reminder_set) + reminder.delay).toString().slice(0, -3)}:R>`, inline: true },
{ name: "Recurring", value: reminder?.recurring ? emoji.tick : emoji.cross, inline: true }
)

await interaction.editReply({ embeds: [info] });
Expand Down
6 changes: 3 additions & 3 deletions src/commands/reminders/reminders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ const command: Command = {
return;
}

// Sort reminders by due date
reminders = reminders.sort((a: any, b: any) => a.due - b.due);
// Sort reminders by when they were set
reminders = reminders.sort((a: any, b: any) => Number(a.reminder_set) - Number(b.reminder_set));

const list = new Discord.EmbedBuilder()
.setColor(client.config.embeds.default as ColorResolvable)
.setTitle("Your Reminders")
.setDescription(cap(reminders.map(r => `\`${r.reminder_id}\` (<t:${(r.reminder_set + r.delay).toString().slice(0, -3)}:R>):\n*${!fullReasons ? cap(r.reason, 100): r.reason}*`).join("\n"), 4000))
.setDescription(cap(reminders.map(r => `\`${r.reminder_id}\` (<t:${(Number(r.reminder_set) + r.delay).toString().slice(0, -3)}:R>):\n*${!fullReasons ? cap(r.reason, 100): r.reason}*`).join("\n"), 4000))

await interaction.editReply({ embeds: [list] });
} catch(err) {
Expand Down
26 changes: 21 additions & 5 deletions src/commands/reminders/remindme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const command: Command = {
type: 3,
name: "reason",
description: "The reason for the reminder.",
max_length: 1000,
max_length: 512,
required: true
},

Expand All @@ -32,6 +32,13 @@ const command: Command = {
name: "send_in_channel",
description: "Send the reminder in this channel, instead of in a direct message.",
required: false
},

{
type: 5,
name: "recurring",
description: "Set the reminder to automatically repeat.",
required: false
}
],
default_member_permissions: null,
Expand All @@ -45,6 +52,7 @@ const command: Command = {
let time: number | string = interaction.options.get("time")?.value as string;
const reason = interaction.options.get("reason")?.value as string;
const sendInChannel = interaction.options.get("send_in_channel")?.value || false as boolean;
const recurring = interaction.options.get("recurring")?.value || false as boolean;

const reminders = await Reminder.find({ user: interaction.user.id });

Expand Down Expand Up @@ -102,20 +110,28 @@ const command: Command = {
channel: interaction.channel?.id ? interaction.channel?.id : null,
delay: time,
reason: reason,
send_in_channel: sendInChannel
send_in_channel: sendInChannel,
recurring
}).save()

if(time < client.config.reminders.timeTillSet) {
client.reminders.set(`${interaction.user.id}-${reminder.reminder_id}`, setTimeout(async () => {
client.reminders.delete(`${interaction.user.id}-${reminder.reminder_id}`);
await Reminder.findOneAndDelete({ reminder_id: reminder.reminder_id, user: interaction.user.id });

if(reminder?.recurring) {
reminder.reminder_set = Date.now().toString();
await reminder.save();
} else {
await reminder.deleteOne();
}

const embed = new Discord.EmbedBuilder()
.setColor(client.config.embeds.default as ColorResolvable)
.setTitle("Reminder")
.setDescription(reason)
.addFields (
{ name: "Set", value: `<t:${reminder.reminder_set.toString().slice(0, -3)}:f> (<t:${reminder.reminder_set.toString().slice(0, -3)}:R>)` }
{ name: "Set", value: `<t:${reminder.reminder_set.toString().slice(0, -3)}:f> (<t:${reminder.reminder_set.toString().slice(0, -3)}:R>)` },
{ name: "Recurring", value: reminder?.recurring ? emoji.tick : emoji.cross }
)
.setFooter({ text: `ID: ${reminder.reminder_id}` })
.setTimestamp()
Expand Down Expand Up @@ -149,7 +165,7 @@ const command: Command = {

const reminderSet = new Discord.EmbedBuilder()
.setColor(client.config.embeds.default as ColorResolvable)
.setDescription(`${emoji.tick} Your reminder has been set for <t:${(reminder.reminder_set + reminder.delay).toString().slice(0, -3)}:f> with ID \`${reminder.reminder_id}\`!`)
.setDescription(`${emoji.tick} Your reminder has been set for <t:${(Number(reminder.reminder_set) + reminder.delay).toString().slice(0, -3)}:f> with ID \`${reminder.reminder_id}\`${reminder?.recurring ? " and will recur until cancelled." : "."}`)

await interaction.editReply({ embeds: [reminderSet] });
} catch(err) {
Expand Down
22 changes: 13 additions & 9 deletions src/events/client/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const event: Event = {
await globalCommands(client);

// Manage timeouts
async function manageExistingTimeouts() {
async function manageExistingReminders() {
let reminders = await Reminder.find({});
const dueReminders = reminders.filter(reminder => (reminder.reminder_set + reminder.delay) <= Date.now().toString());
const dueReminders = reminders.filter(reminder => (Number(reminder.reminder_set) + reminder.delay) <= Date.now());

for(const reminder of dueReminders) {
await reminder.deleteOne();
Expand All @@ -33,37 +33,41 @@ const event: Event = {
.setDescription(reminder.reason)
.addFields (
{ name: "Set", value: `<t:${reminder.reminder_set.toString().slice(0, -3)}:f>`, inline: true },
{ name: "Overdue Since", value: `<t:${(reminder.reminder_set + reminder.delay).toString().slice(0, -3)}:R>`, inline: true }
{ name: "Overdue Since", value: `<t:${(Number(reminder.reminder_set) + reminder.delay).toString().slice(0, -3)}:R>`, inline: true }
)
.setFooter({ text: `ID: ${reminder.reminder_id}` })
.setTimestamp()

const recurring = new EmbedBuilder()
.setColor(client.config.embeds.default as ColorResolvable)
.setDescription("This reminder was a recurring reminder, however since it was sent overdue, it has been reset, you will need to set it again if you want it to continue.")

if(reminder?.send_in_channel && reminder.channel) {
try {
const channel = client.channels.cache.get(reminder.channel) as TextChannel;

if(!channel) throw "Channel not found.";

await channel.send({ content: `<@${reminder.user}>`, embeds: [embed] });
await channel.send({ content: `<@${reminder.user}>`, embeds: reminder?.recurring ? [embed, recurring] : [embed] });
} catch {
try {
const user = client.users.cache.get(reminder.user);

await user?.send({ embeds: [embed] });
await user?.send({ embeds: reminder?.recurring ? [embed, recurring] : [embed] });
} catch {}
}
} else {
try {
const user = client.users.cache.get(reminder.user);

await user?.send({ embeds: [embed] });
await user?.send({ embeds: reminder?.recurring ? [embed, recurring] : [embed] });
} catch {
try {
const channel = client.channels.cache.get(reminder.channel) as TextChannel;

if(!channel) return;

await channel.send({ content: `<@${reminder.user}>`, embeds: [embed] });
await channel.send({ content: `<@${reminder.user}>`, embeds: reminder?.recurring ? [embed, recurring] : [embed] });
} catch {}
}
}
Expand All @@ -74,11 +78,11 @@ const event: Event = {
}
}

manageExistingTimeouts().then(async () => {
manageExistingReminders().then(async () => {
setInterval(async () => {
const reminders = await Reminder.find({});

if(reminders.length === 0) return;
if(!reminders.length) return;

for(const reminder of reminders) {
if(client.reminders.get(`${reminder.user}-${reminder.reminder_id}`)) continue;
Expand Down
7 changes: 6 additions & 1 deletion src/models/Reminder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface Reminder extends Document {
reminder_set: string;
reason: string;
send_in_channel?: boolean;
recurring?: boolean;
}

const schema = new Schema<Reminder>(
Expand All @@ -50,7 +51,7 @@ const schema = new Schema<Reminder>(
},
channel: {
type: String,
required: false,
required: true,
set: (value: string) => encrypt(value),
get: (value: string) => decrypt(value)
},
Expand All @@ -75,6 +76,10 @@ const schema = new Schema<Reminder>(
send_in_channel: {
type: Boolean,
default: false
},
recurring: {
type: Boolean,
default: false
}
},
{ timestamps: true }
Expand Down
4 changes: 2 additions & 2 deletions src/util/setReminder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ExtendedClient from "../classes/ExtendedClient";
import { Reminder } from "../models/Reminder";

export default async function (reminder: Reminder, client: ExtendedClient): Promise<Boolean> {
const delay = Number(reminder.reminder_set + reminder.delay) - Date.now();
const delay = Number(Number(reminder.reminder_set) + reminder.delay) - Date.now();

if(delay > client.config.reminders.timeTillSet) return false;

Expand All @@ -23,7 +23,7 @@ export default async function (reminder: Reminder, client: ExtendedClient): Prom
.setFooter({ text: `ID: ${reminder.reminder_id}` })
.setTimestamp()

if(reminder?.send_in_channel && reminder?.channel) {
if(reminder?.send_in_channel && reminder.channel) {
try {
const channel = client.channels.cache.get(reminder.channel) as TextChannel;

Expand Down

0 comments on commit 85cab30

Please sign in to comment.