From 4309b11588a4c06bfc5e169af4e3cdbce04003ba Mon Sep 17 00:00:00 2001 From: Nawapat Buakoet Date: Sat, 31 Dec 2022 23:23:20 +0700 Subject: [PATCH 1/2] add object `enable` in Command & SlashCommand for optional add object `enable` in Command & SlashCommand for optional --- src/commands/changePrefix.ts | 25 ++++++------ src/commands/greet.ts | 27 +++++++------ src/handlers/Command.ts | 74 ++++++++++++++++++++++------------- src/slashCommands/clear.ts | 53 +++++++++++++++---------- src/slashCommands/embed.ts | 66 +++++++++++++++++++------------ src/slashCommands/ping.ts | 41 +++++++++++--------- src/types.d.ts | 75 ++++++++++++++++++++---------------- 7 files changed, 216 insertions(+), 145 deletions(-) diff --git a/src/commands/changePrefix.ts b/src/commands/changePrefix.ts index 27e265e..ecdaf8e 100644 --- a/src/commands/changePrefix.ts +++ b/src/commands/changePrefix.ts @@ -2,16 +2,17 @@ import { setGuildOption } from "../functions"; import { Command } from "../types"; const command: Command = { - name: "changePrefix", - execute: (message, args) => { - let prefix = args[1] - if (!prefix) return message.channel.send("No prefix provided") - if (!message.guild) return; - setGuildOption(message.guild, "prefix", prefix) - message.channel.send("Prefix successfully changed!") - }, - permissions: ["Administrator"], - aliases: [] -} + enable: true, + name: "changePrefix", + execute: (message, args) => { + let prefix = args[1]; + if (!prefix) return message.channel.send("No prefix provided"); + if (!message.guild) return; + setGuildOption(message.guild, "prefix", prefix); + message.channel.send("Prefix successfully changed!"); + }, + permissions: ["Administrator"], + aliases: [], +}; -export default command \ No newline at end of file +export default command; diff --git a/src/commands/greet.ts b/src/commands/greet.ts index 53c4f62..4166f3b 100644 --- a/src/commands/greet.ts +++ b/src/commands/greet.ts @@ -1,15 +1,20 @@ import { PermissionFlagsBits } from "discord.js"; import { Command } from "../types"; -const command : Command = { - name: "greet", - execute: (message, args) => { - let toGreet = message.mentions.members?.first() - message.channel.send(`Hello there ${toGreet ? toGreet.user.username : message.member?.user.username}!`) - }, - cooldown: 10, - aliases: ["sayhello"], - permissions: ["Administrator", PermissionFlagsBits.ManageEmojisAndStickers] // to test -} +const command: Command = { + enable: true, + name: "greet", + execute: (message, args) => { + let toGreet = message.mentions.members?.first(); + message.channel.send( + `Hello there ${ + toGreet ? toGreet.user.username : message.member?.user.username + }!` + ); + }, + cooldown: 10, + aliases: ["sayhello"], + permissions: ["Administrator", PermissionFlagsBits.ManageEmojisAndStickers], // to test +}; -export default command \ No newline at end of file +export default command; diff --git a/src/handlers/Command.ts b/src/handlers/Command.ts index e4154e2..bb27500 100644 --- a/src/handlers/Command.ts +++ b/src/handlers/Command.ts @@ -1,40 +1,60 @@ import { Client, Routes, SlashCommandBuilder } from "discord.js"; -import { REST } from "@discordjs/rest" +import { REST } from "@discordjs/rest"; import { readdirSync } from "fs"; import { join } from "path"; import { color } from "../functions"; import { Command, SlashCommand } from "../types"; -module.exports = (client : Client) => { - const slashCommands : SlashCommandBuilder[] = [] - const commands : Command[] = [] +module.exports = (client: Client) => { + const slashCommands: SlashCommandBuilder[] = []; + const commands: Command[] = []; - let slashCommandsDir = join(__dirname,"../slashCommands") - let commandsDir = join(__dirname,"../commands") + let slashCommandsDir = join(__dirname, "../slashCommands"); + let commandsDir = join(__dirname, "../commands"); - readdirSync(slashCommandsDir).forEach(file => { - if (!file.endsWith(".js")) return; - let command : SlashCommand = require(`${slashCommandsDir}/${file}`).default - slashCommands.push(command.command) - client.slashCommands.set(command.command.name, command) - }) + readdirSync(slashCommandsDir).forEach((file) => { + if (!file.endsWith(".js")) return; + let command: SlashCommand = require(`${slashCommandsDir}/${file}`).default; + if (!command.enable) return; + slashCommands.push(command.command); + client.slashCommands.set(command.command.name, command); + }); - readdirSync(commandsDir).forEach(file => { - if (!file.endsWith(".js")) return; - let command : Command = require(`${commandsDir}/${file}`).default - commands.push(command) - client.commands.set(command.name, command) - }) + readdirSync(commandsDir).forEach((file) => { + if (!file.endsWith(".js")) return; + let command: Command = require(`${commandsDir}/${file}`).default; + if (!command.enable) return; + commands.push(command); + client.commands.set(command.name, command); + }); - const rest = new REST({version: "10"}).setToken(process.env.TOKEN); + const rest = new REST({ version: "10" }).setToken(process.env.TOKEN); - rest.put(Routes.applicationCommands(process.env.CLIENT_ID), { - body: slashCommands.map(command => command.toJSON()) + rest + .put(Routes.applicationCommands(process.env.CLIENT_ID), { + body: slashCommands.map((command) => command.toJSON()), }) - .then((data : any) => { - console.log(color("text", `🔥 Successfully loaded ${color("variable", data.length)} slash command(s)`)) - console.log(color("text", `🔥 Successfully loaded ${color("variable", commands.length)} command(s)`)) - }).catch(e => { - console.log(e) + .then((data: any) => { + console.log( + color( + "text", + `🔥 Successfully loaded ${color( + "variable", + data.length + )} slash command(s)` + ) + ); + console.log( + color( + "text", + `🔥 Successfully loaded ${color( + "variable", + commands.length + )} command(s)` + ) + ); }) -} \ No newline at end of file + .catch((e) => { + console.log(e); + }); +}; diff --git a/src/slashCommands/clear.ts b/src/slashCommands/clear.ts index 8c4d9f0..855d006 100644 --- a/src/slashCommands/clear.ts +++ b/src/slashCommands/clear.ts @@ -1,30 +1,43 @@ -import { ChannelType, PermissionFlagsBits, SlashCommandBuilder } from "discord.js"; +import { + ChannelType, + PermissionFlagsBits, + SlashCommandBuilder, +} from "discord.js"; import { SlashCommand } from "../types"; -const ClearCommand : SlashCommand = { - command: new SlashCommandBuilder() +const ClearCommand: SlashCommand = { + enable: true, + command: new SlashCommandBuilder() .setName("clear") .setDescription("Delets messages from the current channel.") - .addIntegerOption(option => { - return option + .addIntegerOption((option) => { + return option .setMaxValue(100) .setMinValue(1) .setName("messagecount") - .setDescription("Message amount to be cleared") + .setDescription("Message amount to be cleared"); }) .setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages), - execute: interaction => { - let messageCount = Number(interaction.options.get("messagecount")?.value) - interaction.channel?.messages.fetch({limit: messageCount}) - .then(async msgs => { - if(interaction.channel?.type === ChannelType.DM) return; - const deletedMessages = await interaction.channel?.bulkDelete(msgs,true) - if (deletedMessages?.size === 0) interaction.reply("No messages were deleted.") - else interaction.reply(`Successfully deleted ${deletedMessages?.size} message(s)`) - setTimeout(() => interaction.deleteReply(), 5000) - }) - }, - cooldown: 10 -} + execute: (interaction) => { + let messageCount = Number(interaction.options.get("messagecount")?.value); + interaction.channel?.messages + .fetch({ limit: messageCount }) + .then(async (msgs) => { + if (interaction.channel?.type === ChannelType.DM) return; + const deletedMessages = await interaction.channel?.bulkDelete( + msgs, + true + ); + if (deletedMessages?.size === 0) + interaction.reply("No messages were deleted."); + else + interaction.reply( + `Successfully deleted ${deletedMessages?.size} message(s)` + ); + setTimeout(() => interaction.deleteReply(), 5000); + }); + }, + cooldown: 10, +}; -export default ClearCommand; \ No newline at end of file +export default ClearCommand; diff --git a/src/slashCommands/embed.ts b/src/slashCommands/embed.ts index 94daa92..c1dc0dd 100644 --- a/src/slashCommands/embed.ts +++ b/src/slashCommands/embed.ts @@ -1,36 +1,45 @@ -import { SlashCommandBuilder, ChannelType, TextChannel, EmbedBuilder, ColorResolvable, ApplicationCommandChoicesData } from "discord.js" +import { + SlashCommandBuilder, + ChannelType, + TextChannel, + EmbedBuilder, + ColorResolvable, + ApplicationCommandChoicesData, +} from "discord.js"; import { SlashCommand } from "../types"; const command: SlashCommand = { + enable: true, command: new SlashCommandBuilder() .setName("embed") .setDescription("Create a new embed message.") - .addStringOption(option => { + .addStringOption((option) => { return option .setName("title") .setDescription("Title of the embed message") .setRequired(true); }) - .addStringOption(option => { + .addStringOption((option) => { return option .setName("description") .setDescription("Description of the embed message.") .setRequired(true); }) - .addChannelOption(option => { + .addChannelOption((option) => { return option .setName("channel") .setDescription("Text channel where the embed message will be sent.") .setRequired(true); }) - .addStringOption(option => { + .addStringOption((option) => { return option .setName("color") - .setDescription("Select an option or type an hex color, for example: #000000") + .setDescription( + "Select an option or type an hex color, for example: #000000" + ) .setRequired(true) .setAutocomplete(true); - }) - , + }), autocomplete: async (interaction) => { try { const focusedValue = interaction.options.getFocused(); @@ -59,46 +68,55 @@ const command: SlashCommand = { { name: "DarkGrey", value: "DarkGrey" }, { name: "DarkerGrey", value: "DarkerGrey" }, { name: "LightGrey", value: "LightGrey" }, - { name: "DarkNavy", value: "DarkNavy" } + { name: "DarkNavy", value: "DarkNavy" }, ]; - let filtered: { name: string, value: string }[] = [] + let filtered: { name: string; value: string }[] = []; for (let i = 0; i < choices.length; i++) { const choice = choices[i]; if (choice.name.includes(focusedValue)) filtered.push(choice); } - await interaction.respond( - filtered - ); + await interaction.respond(filtered); } catch (error) { - console.log(`Error: ${error.message}`) + console.log(`Error: ${error.message}`); } }, execute: async (interaction) => { try { await interaction.deferReply({ ephemeral: true }); const options: { [key: string]: string | number | boolean } = {}; - if (!interaction.options) return interaction.editReply({ content: "Something went wrong..." }); + if (!interaction.options) + return interaction.editReply({ content: "Something went wrong..." }); for (let i = 0; i < interaction.options.data.length; i++) { const element = interaction.options.data[i]; - if (element.name && element.value) options[element.name] = element.value; + if (element.name && element.value) + options[element.name] = element.value; } const embed = new EmbedBuilder() .setColor(options.color.toString() as ColorResolvable) .setTitle(options.title.toString()) .setDescription(options.description.toString()) - .setAuthor({ name: interaction.client.user?.username || 'Default Name', iconURL: interaction.client.user?.avatarURL() || undefined }) + .setAuthor({ + name: interaction.client.user?.username || "Default Name", + iconURL: interaction.client.user?.avatarURL() || undefined, + }) .setThumbnail(interaction.client.user?.avatarURL() || null) .setTimestamp() - .setFooter({ text: "Test embed message", iconURL: interaction.client.user?.avatarURL() || undefined }); - let selectedTextChannel = interaction.channel?.client.channels.cache.get(options.channel.toString()) as TextChannel + .setFooter({ + text: "Test embed message", + iconURL: interaction.client.user?.avatarURL() || undefined, + }); + let selectedTextChannel = interaction.channel?.client.channels.cache.get( + options.channel.toString() + ) as TextChannel; selectedTextChannel.send({ embeds: [embed] }); - return interaction.editReply({ content: "Embed message successfully sent." }) + return interaction.editReply({ + content: "Embed message successfully sent.", + }); } catch (error) { interaction.editReply({ content: "Something went wrong..." }); } - }, - cooldown: 10 -} + cooldown: 10, +}; -export default command \ No newline at end of file +export default command; diff --git a/src/slashCommands/ping.ts b/src/slashCommands/ping.ts index 39aea12..a4a90df 100644 --- a/src/slashCommands/ping.ts +++ b/src/slashCommands/ping.ts @@ -1,23 +1,28 @@ -import { SlashCommandBuilder, ChannelType, TextChannel, EmbedBuilder } from "discord.js" +import { + SlashCommandBuilder, + ChannelType, + TextChannel, + EmbedBuilder, +} from "discord.js"; import { getThemeColor } from "../functions"; import { SlashCommand } from "../types"; -const command : SlashCommand = { - command: new SlashCommandBuilder() +const command: SlashCommand = { + enable: true, + command: new SlashCommandBuilder() .setName("ping") - .setDescription("Shows the bot's ping") - , - execute: interaction => { - interaction.reply({ - embeds: [ - new EmbedBuilder() - .setAuthor({name: "MRC License"}) - .setDescription(`🏓 Pong! \n 📡 Ping: ${interaction.client.ws.ping}`) - .setColor(getThemeColor("text")) - ] - }) - }, - cooldown: 10 -} + .setDescription("Shows the bot's ping"), + execute: (interaction) => { + interaction.reply({ + embeds: [ + new EmbedBuilder() + .setAuthor({ name: "MRC License" }) + .setDescription(`🏓 Pong! \n 📡 Ping: ${interaction.client.ws.ping}`) + .setColor(getThemeColor("text")), + ], + }); + }, + cooldown: 10, +}; -export default command \ No newline at end of file +export default command; diff --git a/src/types.d.ts b/src/types.d.ts index 8ee5160..038cc33 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,54 +1,63 @@ -import { SlashCommandBuilder, CommandInteraction, Collection, PermissionResolvable, Message, AutocompleteInteraction } from "discord.js" -import mongoose from "mongoose" +import { + SlashCommandBuilder, + CommandInteraction, + Collection, + PermissionResolvable, + Message, + AutocompleteInteraction, +} from "discord.js"; +import mongoose from "mongoose"; export interface SlashCommand { - command: SlashCommandBuilder | any, - execute: (interaction : CommandInteraction) => void, - autocomplete?: (interaction: AutocompleteInteraction) => void, - cooldown?: number // in seconds + enable: boolean; + command: SlashCommandBuilder | any; + execute: (interaction: CommandInteraction) => void; + autocomplete?: (interaction: AutocompleteInteraction) => void; + cooldown?: number; // in seconds } export interface Command { - name: string, - execute: (message: Message, args: Array) => void, - permissions: Array, - aliases: Array, - cooldown?: number, + name: string; + enable: boolean; + execute: (message: Message, args: Array) => void; + permissions: Array; + aliases: Array; + cooldown?: number; } interface GuildOptions { - prefix: string, + prefix: string; } export interface IGuild extends mongoose.Document { - guildID: string, - options: GuildOptions - joinedAt: Date + guildID: string; + options: GuildOptions; + joinedAt: Date; } -export type GuildOption = keyof GuildOptions +export type GuildOption = keyof GuildOptions; export interface BotEvent { - name: string, - once?: boolean | false, - execute: (...args?) => void + name: string; + once?: boolean | false; + execute: (...args) => void; } declare global { - namespace NodeJS { - interface ProcessEnv { - TOKEN: string, - CLIENT_ID: string, - PREFIX: string, - MONGO_URI: string, - MONGO_DATABASE_NAME: string - } + namespace NodeJS { + interface ProcessEnv { + TOKEN: string; + CLIENT_ID: string; + PREFIX: string; + MONGO_URI: string; + MONGO_DATABASE_NAME: string; } + } } declare module "discord.js" { - export interface Client { - slashCommands: Collection - commands: Collection, - cooldowns: Collection - } -} \ No newline at end of file + export interface Client { + slashCommands: Collection; + commands: Collection; + cooldowns: Collection; + } +} From f1238a1c48de6728a1d74538a2c03d82bc2e5bdf Mon Sep 17 00:00:00 2001 From: Nawapat Buakoet Date: Sun, 1 Jan 2023 01:29:25 +0700 Subject: [PATCH 2/2] added enable for Event --- src/events/guildCreate.ts | 23 +++--- src/events/interactionCreate.ts | 91 ++++++++++++++--------- src/events/messageCreate.ts | 127 +++++++++++++++++++------------- src/events/ready.ts | 21 +++--- src/handlers/Event.ts | 27 ++++--- src/types.d.ts | 1 + 6 files changed, 170 insertions(+), 120 deletions(-) diff --git a/src/events/guildCreate.ts b/src/events/guildCreate.ts index c33b2eb..187bffd 100644 --- a/src/events/guildCreate.ts +++ b/src/events/guildCreate.ts @@ -3,15 +3,16 @@ import GuildModel from "../schemas/Guild"; import { BotEvent } from "../types"; const event: BotEvent = { - name: "guildCreate", - execute: (guild : Guild) => { - let newGuild = new GuildModel({ - guildID: guild.id, - options: {}, - joinedAt: Date.now() - }) - newGuild.save() - } -} + enable: true, + name: "guildCreate", + execute: (guild: Guild) => { + let newGuild = new GuildModel({ + guildID: guild.id, + options: {}, + joinedAt: Date.now(), + }); + newGuild.save(); + }, +}; -export default event; \ No newline at end of file +export default event; diff --git a/src/events/interactionCreate.ts b/src/events/interactionCreate.ts index c674bc8..2306e85 100644 --- a/src/events/interactionCreate.ts +++ b/src/events/interactionCreate.ts @@ -1,41 +1,62 @@ import { Interaction } from "discord.js"; import { BotEvent } from "../types"; -const event : BotEvent = { - name: "interactionCreate", - execute: (interaction: Interaction) => { - if (interaction.isChatInputCommand()) { - let command = interaction.client.slashCommands.get(interaction.commandName) - let cooldown = interaction.client.cooldowns.get(`${interaction.commandName}-${interaction.user.username}`) - if (!command) return; - if (command.cooldown && cooldown) { - if (Date.now() < cooldown) { - interaction.reply(`You have to wait ${Math.floor(Math.abs(Date.now() - cooldown) / 1000)} second(s) to use this command again.`) - setTimeout(() => interaction.deleteReply(), 5000) - return - } - interaction.client.cooldowns.set(`${interaction.commandName}-${interaction.user.username}`, Date.now() + command.cooldown * 1000) - setTimeout(() => { - interaction.client.cooldowns.delete(`${interaction.commandName}-${interaction.user.username}`) - }, command.cooldown * 1000) - } else if (command.cooldown && !cooldown) { - interaction.client.cooldowns.set(`${interaction.commandName}-${interaction.user.username}`, Date.now() + command.cooldown * 1000) - } - command.execute(interaction) - } else if (interaction.isAutocomplete()) { - const command = interaction.client.slashCommands.get(interaction.commandName); - if (!command) { - console.error(`No command matching ${interaction.commandName} was found.`); - return; - } - try { - if(!command.autocomplete) return; - command.autocomplete(interaction); - } catch (error) { - console.error(error); - } +const event: BotEvent = { + enable: true, + name: "interactionCreate", + execute: (interaction: Interaction) => { + if (interaction.isChatInputCommand()) { + let command = interaction.client.slashCommands.get( + interaction.commandName + ); + let cooldown = interaction.client.cooldowns.get( + `${interaction.commandName}-${interaction.user.username}` + ); + if (!command) return; + if (command.cooldown && cooldown) { + if (Date.now() < cooldown) { + interaction.reply( + `You have to wait ${Math.floor( + Math.abs(Date.now() - cooldown) / 1000 + )} second(s) to use this command again.` + ); + setTimeout(() => interaction.deleteReply(), 5000); + return; } + interaction.client.cooldowns.set( + `${interaction.commandName}-${interaction.user.username}`, + Date.now() + command.cooldown * 1000 + ); + setTimeout(() => { + interaction.client.cooldowns.delete( + `${interaction.commandName}-${interaction.user.username}` + ); + }, command.cooldown * 1000); + } else if (command.cooldown && !cooldown) { + interaction.client.cooldowns.set( + `${interaction.commandName}-${interaction.user.username}`, + Date.now() + command.cooldown * 1000 + ); + } + command.execute(interaction); + } else if (interaction.isAutocomplete()) { + const command = interaction.client.slashCommands.get( + interaction.commandName + ); + if (!command) { + console.error( + `No command matching ${interaction.commandName} was found.` + ); + return; + } + try { + if (!command.autocomplete) return; + command.autocomplete(interaction); + } catch (error) { + console.error(error); + } } -} + }, +}; -export default event; \ No newline at end of file +export default event; diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index c1c6339..51e6dab 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -1,63 +1,84 @@ import { ChannelType, Message } from "discord.js"; -import { checkPermissions, getGuildOption, sendTimedMessage } from "../functions"; +import { + checkPermissions, + getGuildOption, + sendTimedMessage, +} from "../functions"; import { BotEvent } from "../types"; import mongoose from "mongoose"; const event: BotEvent = { - name: "messageCreate", - execute: async (message: Message) => { - if (!message.member || message.member.user.bot) return; - if (!message.guild) return; - let prefix = process.env.PREFIX - if (mongoose.connection.readyState === 1) { - let guildPrefix = await getGuildOption(message.guild, "prefix") - if (guildPrefix) prefix = guildPrefix; - } - - if (!message.content.startsWith(prefix)) return; - if (message.channel.type !== ChannelType.GuildText) return; - - let args = message.content.substring(prefix.length).split(" ") - let command = message.client.commands.get(args[0]) - - if (!command) { - let commandFromAlias = message.client.commands.find((command) => command.aliases.includes(args[0])) - if (commandFromAlias) command = commandFromAlias - else return; - } - - let cooldown = message.client.cooldowns.get(`${command.name}-${message.member.user.username}`) - let neededPermissions = checkPermissions(message.member, command.permissions) - if (neededPermissions !== null) - return sendTimedMessage( - ` + enable: true, + name: "messageCreate", + execute: async (message: Message) => { + if (!message.member || message.member.user.bot) return; + if (!message.guild) return; + let prefix = process.env.PREFIX; + if (mongoose.connection.readyState === 1) { + let guildPrefix = await getGuildOption(message.guild, "prefix"); + if (guildPrefix) prefix = guildPrefix; + } + + if (!message.content.startsWith(prefix)) return; + if (message.channel.type !== ChannelType.GuildText) return; + + let args = message.content.substring(prefix.length).split(" "); + let command = message.client.commands.get(args[0]); + + if (!command) { + let commandFromAlias = message.client.commands.find((command) => + command.aliases.includes(args[0]) + ); + if (commandFromAlias) command = commandFromAlias; + else return; + } + + let cooldown = message.client.cooldowns.get( + `${command.name}-${message.member.user.username}` + ); + let neededPermissions = checkPermissions( + message.member, + command.permissions + ); + if (neededPermissions !== null) + return sendTimedMessage( + ` You don't have enough permissions to use this command. \n Needed permissions: ${neededPermissions.join(", ")} `, - message.channel, - 5000 - ) - - - if (command.cooldown && cooldown) { - if (Date.now() < cooldown) { - sendTimedMessage( - `You have to wait ${Math.floor(Math.abs(Date.now() - cooldown) / 1000)} second(s) to use this command again.`, - message.channel, - 5000 - ) - return - } - message.client.cooldowns.set(`${command.name}-${message.member.user.username}`, Date.now() + command.cooldown * 1000) - setTimeout(() => { - message.client.cooldowns.delete(`${command?.name}-${message.member?.user.username}`) - }, command.cooldown * 1000) - } else if (command.cooldown && !cooldown) { - message.client.cooldowns.set(`${command.name}-${message.member.user.username}`, Date.now() + command.cooldown * 1000) - } - - command.execute(message, args) + message.channel, + 5000 + ); + + if (command.cooldown && cooldown) { + if (Date.now() < cooldown) { + sendTimedMessage( + `You have to wait ${Math.floor( + Math.abs(Date.now() - cooldown) / 1000 + )} second(s) to use this command again.`, + message.channel, + 5000 + ); + return; + } + message.client.cooldowns.set( + `${command.name}-${message.member.user.username}`, + Date.now() + command.cooldown * 1000 + ); + setTimeout(() => { + message.client.cooldowns.delete( + `${command?.name}-${message.member?.user.username}` + ); + }, command.cooldown * 1000); + } else if (command.cooldown && !cooldown) { + message.client.cooldowns.set( + `${command.name}-${message.member.user.username}`, + Date.now() + command.cooldown * 1000 + ); } -} -export default event \ No newline at end of file + command.execute(message, args); + }, +}; + +export default event; diff --git a/src/events/ready.ts b/src/events/ready.ts index 5526c32..5631125 100644 --- a/src/events/ready.ts +++ b/src/events/ready.ts @@ -2,14 +2,15 @@ import { Client } from "discord.js"; import { BotEvent } from "../types"; import { color } from "../functions"; -const event : BotEvent = { - name: "ready", - once: true, - execute: (client : Client) => { - console.log( - color("text", `💪 Logged in as ${color("variable", client.user?.tag)}`) - ) - } -} +const event: BotEvent = { + enable: true, + name: "ready", + once: true, + execute: (client: Client) => { + console.log( + color("text", `💪 Logged in as ${color("variable", client.user?.tag)}`) + ); + }, +}; -export default event; \ No newline at end of file +export default event; diff --git a/src/handlers/Event.ts b/src/handlers/Event.ts index 6097edb..f551487 100644 --- a/src/handlers/Event.ts +++ b/src/handlers/Event.ts @@ -5,15 +5,20 @@ import { color } from "../functions"; import { BotEvent } from "../types"; module.exports = (client: Client) => { - let eventsDir = join(__dirname, "../events") + let eventsDir = join(__dirname, "../events"); - readdirSync(eventsDir).forEach(file => { - if (!file.endsWith(".js")) return; - let event: BotEvent = require(`${eventsDir}/${file}`).default - event.once ? - client.once(event.name, (...args) => event.execute(...args)) - : - client.on(event.name, (...args) => event.execute(...args)) - console.log(color("text", `🌠 Successfully loaded event ${color("variable", event.name)}`)) - }) -} + readdirSync(eventsDir).forEach((file) => { + if (!file.endsWith(".js")) return; + let event: BotEvent = require(`${eventsDir}/${file}`).default; + if (!event.enable) return; + event.once + ? client.once(event.name, (...args) => event.execute(...args)) + : client.on(event.name, (...args) => event.execute(...args)); + console.log( + color( + "text", + `🌠 Successfully loaded event ${color("variable", event.name)}` + ) + ); + }); +}; diff --git a/src/types.d.ts b/src/types.d.ts index 038cc33..cb7a8f2 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -38,6 +38,7 @@ export interface IGuild extends mongoose.Document { export type GuildOption = keyof GuildOptions; export interface BotEvent { name: string; + enable: boolean; once?: boolean | false; execute: (...args) => void; }