Skip to content

Commit

Permalink
one config var & maxReminderTime + timeToSet to config
Browse files Browse the repository at this point in the history
  • Loading branch information
wdhdev committed Jul 31, 2024
1 parent abb15ee commit 68e6696
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 47 deletions.
5 changes: 2 additions & 3 deletions src/classes/ExtendedClient.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Client, Collection, Snowflake } from "discord.js";
import { embeds, main } from "../config";
import config from "../config";
import * as Sentry from "@sentry/node";

export default class ExtendedClient extends Client {
public commandIds: Collection<string, Snowflake>;
public commands: Collection<string, any>;
public config_embeds: typeof embeds;
public config_main: typeof main;
public config: typeof config;
public events: Collection<string, any>;
public logCommandError: Function;
public logError: Function;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/info/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const command: Command = {
async execute(interaction: ChatInputCommandInteraction, client: ExtendedClient, Discord: typeof import("discord.js")) {
try {
const info = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setColor(client.config.embeds.default)
.setAuthor({ name: client.user.tag, iconURL: client.user.displayAvatarURL({ extension: "png", forceStatic: false }), url: `https://discord.com/users/${client.user.id}` })
.setDescription(bot.description)
.addFields (
Expand Down
4 changes: 2 additions & 2 deletions src/commands/info/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const command: Command = {
}

const help = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setColor(client.config.embeds.default)
.setThumbnail(client.user.displayAvatarURL({ extension: "png", forceStatic: false }))
.setTitle("Commands")
.setDescription(cmds.sort().join("\n"))
Expand All @@ -98,7 +98,7 @@ const command: Command = {
const cooldown = command.cooldown ? `${command.cooldown} second${command.cooldown === 1 ? "" : "s"}` : "None";

const commandHelp = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setColor(client.config.embeds.default)
.setTitle(`Command: ${command.name}`)
.addFields (
{ name: "Description", value: description },
Expand Down
2 changes: 1 addition & 1 deletion src/commands/info/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const command: Command = {
}

const ping = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setColor(client.config.embeds.default)
.addFields (
{ name: "Bot Latency", value: botLatencyValue, inline: true },
{ name: "API Latency", value: apiLatencyValue, inline: true }
Expand Down
4 changes: 2 additions & 2 deletions src/commands/reminders/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const command: Command = {

if(!reminder) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setDescription(`${emoji.cross} I could not find that reminder!`)

await interaction.editReply({ embeds: [error] });
Expand All @@ -50,7 +50,7 @@ const command: Command = {
await reminder.deleteOne();

const cancelled = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setColor(client.config.embeds.default)
.setDescription(`${emoji.tick} The reminder \`${id}\` has been cancelled!`)

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

if(!reminder) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setDescription(`${emoji.cross} I could not find that reminder!`)

await interaction.editReply({ embeds: [error] });
return;
}

const info = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setColor(client.config.embeds.default)
.setTitle(reminder.reminder_id)
.addFields(
{ name: "Reason", value: reminder.reason },
Expand Down
4 changes: 2 additions & 2 deletions src/commands/reminders/reminders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const command: Command = {

if(!reminders.length) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setDescription(`${emoji.cross} You do not have any reminders set.`)

await interaction.editReply({ embeds: [error] });
Expand All @@ -43,7 +43,7 @@ const command: Command = {
reminders = reminders.sort((a: any, b: any) => a.due - b.due);

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

Expand Down
16 changes: 8 additions & 8 deletions src/commands/reminders/remindme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const command: Command = {

if(reminders.length >= 10) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setDescription(`${emoji.cross} You can only have up to 10 active reminders at once!`)

await interaction.editReply({ embeds: [error] });
Expand All @@ -64,7 +64,7 @@ const command: Command = {

if(!match) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setDescription(`${emoji.cross} Invalid time format. Use \`mo\` for months, \`d\` for days, \`h\` for hours, \`m\` for minutes, or \`s\` for seconds.`)

await interaction.editReply({ embeds: [error] });
Expand All @@ -87,11 +87,11 @@ const command: Command = {

time = (months * month) + (days * day) + (hours * hour) + (minutes * minute) + (seconds * second);

const maxReminderTimeDays = Math.floor(client.maxReminderTime / (24 * 60 * 60 * 1000));
const maxReminderTimeDays = Math.floor(client.config.main.maxReminderTime / (24 * 60 * 60 * 1000));

if(time > client.maxReminderTime) {
if(time > client.config.main.maxReminderTime) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setDescription(`${emoji.cross} Your reminder cannot be more than ${maxReminderTimeDays} day${maxReminderTimeDays === 1 ? "" : "s"} in the future.`)

await interaction.editReply({ embeds: [error] });
Expand All @@ -111,13 +111,13 @@ const command: Command = {
send_in_channel: sendInChannel
}).save()

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

const embed = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setColor(client.config.embeds.default)
.setTitle("Reminder")
.setDescription(reason)
.addFields (
Expand Down Expand Up @@ -154,7 +154,7 @@ const command: Command = {
}

const reminderSet = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setColor(client.config.embeds.default)
.setDescription(`${emoji.tick} Your reminder has been set for <t:${reminder.due.toString().slice(0, -3)}:f> with ID \`${reminder.reminder_id}\`!`)

await interaction.editReply({ embeds: [reminderSet] });
Expand Down
4 changes: 3 additions & 1 deletion src/config.example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const emojis = {
}

const main = {
owner: ""
maxReminderTime: 365 * 24 * 60 * 60 * 1000, // 1 year (365 days)
owner: "",
timeToSet: 10 * 60 * 1000 // 10 minutes
}

export {
Expand Down
2 changes: 1 addition & 1 deletion src/events/client/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const event: Event = {
reminders = reminders.filter(r => r !== reminder);

const embed = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setColor(client.config.embeds.default)
.setTitle("Overdue Reminder")
.setDescription(reminder.reason)
.addFields (
Expand Down
16 changes: 8 additions & 8 deletions src/events/guild/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const event: Event = {
try {
if(!args[0]) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setDescription(`${emoji.cross} No code was provided.`)

message.reply({ embeds: [error] });
Expand All @@ -37,13 +37,13 @@ const event: Event = {
console.log(`[eval] [input] ${message.author.tag} (${message.author.id}): ${args.join(" ")}`);

const evalInput = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setColor(client.config.embeds.default)
.setTitle("📥 Input")
.setDescription(`\`\`\`js\n${cap(args.join(" "), 4000)}\`\`\``)
.setTimestamp()

const evaluating = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setColor(client.config.embeds.default)
.setDescription(`${emoji.ping} Evaluating...`)

const msg = await message.reply({ embeds: [evaluating] });
Expand All @@ -63,7 +63,7 @@ const event: Event = {
console.log(`[eval] [output] ${message.author.tag} (${message.author.id}):\n` + output);

const evalOutput = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setColor(client.config.embeds.default)
.setTitle("📤 Output")
.setTimestamp()

Expand All @@ -82,7 +82,7 @@ const event: Event = {
console.log(`[eval] [output] ${message.author.tag} (${message.author.id}):\n` + output);

const evalOutput = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setTitle("📤 Output")
.setDescription("No output was returned.")
.setTimestamp()
Expand All @@ -100,7 +100,7 @@ const event: Event = {
console.log(`[eval] [error] ${message.author.tag} (${message.author.id}):\n` + err);

const evalOutput = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setTitle("📤 Output")
.setTimestamp()

Expand All @@ -121,7 +121,7 @@ const event: Event = {
console.error(err);

const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setTitle("💥 An error occurred")
.setDescription(`\`\`\`${err.message}\`\`\``)
.addFields (
Expand All @@ -136,7 +136,7 @@ const event: Event = {
}

const mention = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setColor(client.config.embeds.default)
.setDescription(`👋 Hello there **${message.author.globalName || message.author.username}**!\n\n👤 My name is **${client.user.username}** and I am a reminder bot.\n⏰ I can remind you of things you need to do, or just send you a message at a certain time!\n\n✨ To get started with me, use the command: </help:${client.commandIds.get("help")}>`)
.setTimestamp()

Expand Down
2 changes: 1 addition & 1 deletion src/handlers/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export = async (client: ExtendedClient) => {
console.error(err);

const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setTitle("💥 An error occurred")
.setDescription(`\`\`\`${err.message}\`\`\``)
.addFields (
Expand Down
12 changes: 4 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import config from "./config";
require("dotenv").config();

import * as Sentry from "@sentry/node";
Expand All @@ -9,7 +10,6 @@ Sentry.init({

import Discord from "discord.js";
import ExtendedClient from "./classes/ExtendedClient";
import config from "./config";

const client = new ExtendedClient({
intents: ["Guilds", "GuildMessages"],
Expand All @@ -24,17 +24,16 @@ const client = new ExtendedClient({
}
})

// Config
client.config = config;

// Error Handling
process.on("unhandledRejection", (err: Error) => Sentry.captureException(err));

// Connect to Database
import database from "./util/database";
database();

// Configs
client.config_embeds = config.embeds;
client.config_main = config.main;

// Handlers
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
Expand All @@ -50,9 +49,6 @@ client.commandIds = new Discord.Collection();
client.reminders = new Map();
client.sentry = Sentry;

client.maxReminderTime = 365 * 24 * 60 * 60 * 1000; // 1 year (365 days)
client.timeToSet = 10 * 60 * 1000; // 10 minutes

client.validPermissions = [
"CreateInstantInvite",
"KickMembers",
Expand Down
10 changes: 5 additions & 5 deletions src/util/interaction/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export = async (client: ExtendedClient, Discord: typeof import("discord.js"), in

if(!command.enabled) {
const disabled = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setDescription(`${emoji.cross} This command has been disabled!`)

await interaction.reply({ embeds: [disabled], ephemeral: true });
Expand All @@ -35,7 +35,7 @@ export = async (client: ExtendedClient, Discord: typeof import("discord.js"), in

if(invalidPerms.length) {
const permError = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setDescription(`I am missing these permissions: \`${invalidPerms.join("\`, \`")}\``)

await interaction.reply({ embeds: [permError], ephemeral: true });
Expand All @@ -56,7 +56,7 @@ export = async (client: ExtendedClient, Discord: typeof import("discord.js"), in
client.logError(err);

const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setDescription(`${emoji.cross} There was an error while executing that command!`)

command.deferReply ? await interaction.editReply({ embeds: [error] }) : await interaction.reply({ embeds: [error], ephemeral: true });
Expand All @@ -77,7 +77,7 @@ export = async (client: ExtendedClient, Discord: typeof import("discord.js"), in
const timeLeft: string = (((expirationTime - currentTime) / 1000).toFixed(0)).toString();

const cooldown = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setDescription(`⏰ Please wait ${timeLeft} second${timeLeft === "1" ? "" : "s"} before running that command again!`)

command.deferReply ? await interaction.editReply({ embeds: [cooldown] }) : await interaction.reply({ embeds: [cooldown], ephemeral: true });
Expand All @@ -100,7 +100,7 @@ export = async (client: ExtendedClient, Discord: typeof import("discord.js"), in
client.logError(err);

const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setColor(client.config.embeds.error)
.setDescription(`${emoji.cross} There was an error while executing that command!`)

command.deferReply ? await interaction.editReply({ embeds: [error] }) : await interaction.reply({ embeds: [error], ephemeral: 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 @@ -4,14 +4,14 @@ import ExtendedClient from "../classes/ExtendedClient";
export default async function (reminder: any, client: ExtendedClient): Promise<Boolean> {
const delay = Number(reminder.due) - Date.now();

if(delay > client.timeToSet) return false;
if(delay > client.config.main.timeToSet) return false;

client.reminders.set(`${reminder.user}-${reminder.reminder_id}`, setTimeout(async () => {
await reminder.deleteOne();
client.reminders.delete(`${reminder.user}-${reminder.reminder_id}`);

const embed = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setColor(client.config.embeds.default)
.setTitle("Reminder")
.setDescription(reminder.reason)
.addFields (
Expand Down

0 comments on commit 68e6696

Please sign in to comment.