Skip to content

Commit

Permalink
send in channel option
Browse files Browse the repository at this point in the history
  • Loading branch information
wdhdev committed Jul 19, 2024
1 parent abc7ea1 commit 687490d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 22 deletions.
32 changes: 27 additions & 5 deletions src/commands/reminders/remindme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ const command: Command = {
description: "The reason for the reminder.",
max_length: 1000,
required: true
},

{
type: 5,
name: "send_in_channel",
description: "Send the reminder in this channel, instead of in a direct message.",
required: false
}
],
default_member_permissions: null,
Expand All @@ -38,6 +45,7 @@ const command: Command = {
try {
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 reminders = await Reminder.find({ user: interaction.user.id });

Expand Down Expand Up @@ -99,7 +107,8 @@ const command: Command = {
set: Date.now(),
due: Date.now() + time,
delay: time,
reason: reason
reason: reason,
send_in_channel: sendInChannel
}).save()

if(time < client.timeToSet) {
Expand All @@ -117,16 +126,29 @@ const command: Command = {
.setFooter({ text: `ID: ${reminder.reminder_id}` })
.setTimestamp()

try {
await interaction.user.send({ embeds: [embed] });
} catch {

if(sendInChannel) {
try {
const channel = client.channels.cache.get(interaction.channel.id) as TextChannel;

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

await channel.send({ content: `${interaction.user}`, embeds: [embed] });
} catch {
try {
await interaction.user.send({ embeds: [embed] });
} catch {}
}
} else {
try {
await interaction.user.send({ embeds: [embed] });
} catch {
const channel = client.channels.cache.get(interaction.channel.id) as TextChannel;

if(!channel) return;

await channel.send({ content: `${interaction.user}`, embeds: [embed] });
} catch {}
}
}
}, time))
}
Expand Down
33 changes: 24 additions & 9 deletions src/events/client/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import ExtendedClient from "../../classes/ExtendedClient";
import Discord from "discord.js";
import { exec } from "child_process";
import globalCommands from "../../scripts/global-commands";
import setReminder from "../../util/setReminder";

import Reminder from "../../models/Reminder";
import setReminder from "../../util/setReminder";

const event: Event = {
name: "ready",
Expand Down Expand Up @@ -35,7 +35,6 @@ const event: Event = {

async function manageExistingTimeouts() {
let reminders = await Reminder.find({});

const dueReminders = reminders.filter(reminder => reminder.due <= Date.now().toString());

for(const reminder of dueReminders) {
Expand All @@ -53,18 +52,34 @@ const event: Event = {
.setFooter({ text: `ID: ${reminder.reminder_id}` })
.setTimestamp()

try {
const user = client.users.cache.get(reminder.user);

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

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

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

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

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

if(!channel) return;

await channel.send({ content: `<@${reminder.user}>`, embeds: [embed] });
} catch {}
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/models/Reminder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const schema = new Schema({
delay: Number,
set: String,
due: String,
reason: String
reason: String,
send_in_channel: Boolean
})

export default model("reminders", schema, "reminders");
30 changes: 23 additions & 7 deletions src/util/setReminder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,34 @@ export default async function (reminder: any, client: ExtendedClient): Promise<B
.setFooter({ text: `ID: ${reminder.reminder_id}` })
.setTimestamp()

try {
const user = client.users.cache.get(reminder.user);

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

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

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

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

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

if(!channel) return;

await channel.send({ content: `<@${reminder.user}>`, embeds: [embed] });
} catch {}
}
}
}, delay))

Expand Down

0 comments on commit 687490d

Please sign in to comment.