diff --git a/README.md b/README.md index 8ba645ca..047f3b04 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,17 @@ Did you like my project? Click on the star button (⭐️) right above your scre Looking for the old version? [Click here](https://github.com/TFAGaming/DiscordJS-V14-Bot-Template/releases/tag/v1.7.0-last). +## Features +- Supports all possible type of commands. + - Message commands (AKA: Prefix commands). + - Application commands: + - Chat Input + - User context + - Message context +- Easy and simple to use +- Updated to latest discord.js version. +- Included a database: MongoDB + ## Commands, events, and components structure > **Note** @@ -25,7 +36,8 @@ module.exports = { name: string, description: string, aliases: string[], - permissions: PermissionFlagBits + permissions?: PermissionFlagBits, + cooldown?: number }, run: async (client, message, args) => Promise }; @@ -35,6 +47,7 @@ module.exports = { ```ts module.exports = { structure: SlashCommandBuilder | ContextMenuCommandBuilder, + options?: { }, run: async (client, interaction) => Promise }; ``` @@ -60,7 +73,7 @@ module.exports = { ## Requirements ### Packages: - **chalk** v2.4.2 -- **discord.js** v^14.12.0 +- **discord.js** v^14.13.0 - **dotenv** v^latest - **mongoose** v^latest @@ -69,7 +82,7 @@ module.exports = { ``` npm init -y -npm i chalk@2.4.2 discord.js@14 dotenv mongoose +npm install chalk@2.4.2 discord.js dotenv mongoose ``` ## Setup diff --git a/package.json b/package.json index f3f5bb74..d1558c3e 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "license": "GPL-3.0", "dependencies": { "chalk": "^2.4.2", - "discord.js": "^14.12.1", + "discord.js": "^14.13.0", "dotenv": "^16.3.1", "mongoose": "^7.3.1" } diff --git a/src/class/ExtendedClient.js b/src/class/ExtendedClient.js index 0dfe4331..22fe2689 100644 --- a/src/class/ExtendedClient.js +++ b/src/class/ExtendedClient.js @@ -5,6 +5,7 @@ const events = require("../handlers/events"); const deploy = require("../handlers/deploy"); const mongoose = require("../handlers/mongoose"); const components = require("../handlers/components"); +const { ActivityType } = require("discord.js"); module.exports = class extends Client { collection = { @@ -25,7 +26,9 @@ module.exports = class extends Client { partials: [Object.keys(Partials)], presence: { activities: [{ - name: 'DiscordJS-V14-Bot-Template v2' + name: 'something goes here', + type: ActivityType.Custom, + state: 'DiscordJS-V14-Bot-Template v2' }] } }); diff --git a/src/commands/prefix/Info/help.js b/src/commands/prefix/Info/help.js index 688912a2..5fa488a8 100644 --- a/src/commands/prefix/Info/help.js +++ b/src/commands/prefix/Info/help.js @@ -7,7 +7,8 @@ module.exports = { structure: { name: 'help', description: 'View all the possible commands!', - aliases: ['h'] + aliases: ['h'], + cooldown: 15000 }, /** * @param {ExtendedClient} client diff --git a/src/commands/prefix/Utility/ping.js b/src/commands/prefix/Utility/ping.js index a37b7c4f..3638e801 100644 --- a/src/commands/prefix/Utility/ping.js +++ b/src/commands/prefix/Utility/ping.js @@ -6,7 +6,8 @@ module.exports = { name: 'ping', description: 'Replies with Pong!', aliases: ['p'], - permissions: 'Administrator' + permissions: 'Administrator', + cooldown: 5000 }, /** * @param {ExtendedClient} client diff --git a/src/commands/slash/Info/help.js b/src/commands/slash/Info/help.js index e88539ab..e2982d08 100644 --- a/src/commands/slash/Info/help.js +++ b/src/commands/slash/Info/help.js @@ -12,6 +12,9 @@ module.exports = { * @param {ChatInputCommandInteraction} interaction * @param {[]} args */ + options: { + cooldown: 15000 + }, run: async (client, interaction, args) => { await interaction.deferReply(); diff --git a/src/commands/slash/Utility/ping.js b/src/commands/slash/Utility/ping.js index fd07e239..878f7481 100644 --- a/src/commands/slash/Utility/ping.js +++ b/src/commands/slash/Utility/ping.js @@ -10,6 +10,9 @@ module.exports = { * @param {ChatInputCommandInteraction} interaction * @param {[]} args */ + options: { + cooldown: 5000 + }, run: async (client, interaction, args) => { await interaction.reply({ diff --git a/src/events/Guild/interactionCreate.js b/src/events/Guild/interactionCreate.js index f0b51ceb..0a3d8595 100644 --- a/src/events/Guild/interactionCreate.js +++ b/src/events/Guild/interactionCreate.js @@ -2,6 +2,8 @@ const config = require('../../config'); const { log } = require('../../functions'); const ExtendedClient = require('../../class/ExtendedClient'); +const cooldown = new Map(); + module.exports = { event: 'interactionCreate', /** @@ -10,7 +12,7 @@ module.exports = { * @param {import('discord.js').Interaction} interaction * @returns */ - run: (client, interaction) => { + run: async (client, interaction) => { if (config.handler.commands.slash === false && interaction.isChatInputCommand()) return; if (config.handler.commands.user === false && interaction.isUserContextMenuCommand()) return; if (config.handler.commands.message === false && interaction.isMessageContextMenuCommand()) return; @@ -20,6 +22,46 @@ module.exports = { if (!command) return; try { + if (command.options?.cooldown) { + const cooldownFunction = () => { + let data = cooldown.get(interaction.user.id); + + data.push(interaction.commandName); + + cooldown.set(interaction.user.id, data); + + setTimeout(() => { + let data = cooldown.get(interaction.user.id); + + data = data.filter((v) => v !== interaction.commandName); + + if (data.length <= 0) { + cooldown.delete(interaction.user.id); + } else { + cooldown.set(interaction.user.id, data); + }; + }, command.options?.cooldown); + }; + + if (cooldown.has(interaction.user.id)) { + let data = cooldown.get(interaction.user.id); + + if (data.some((v) => v === interaction.commandName)) { + await interaction.reply({ + content: 'Slow down buddy! You\'re too fast to use this command.' + }); + + return; + } else { + cooldownFunction(); + }; + } else { + cooldown.set(interaction.user.id, [interaction.commandName]); + + cooldownFunction(); + }; + }; + command.run(client, interaction); } catch (error) { log(error, 'err'); diff --git a/src/events/Guild/messageCreate.js b/src/events/Guild/messageCreate.js index d67d1600..fb6f047d 100644 --- a/src/events/Guild/messageCreate.js +++ b/src/events/Guild/messageCreate.js @@ -4,6 +4,8 @@ const { log } = require('../../functions'); const GuildSchema = require('../../schemas/GuildSchema'); const ExtendedClient = require('../../class/ExtendedClient'); +const cooldown = new Map(); + module.exports = { event: 'messageCreate', /** @@ -48,6 +50,46 @@ module.exports = { return; }; + if (command.structure?.cooldown) { + const cooldownFunction = () => { + let data = cooldown.get(message.author.id); + + data.push(commandInput); + + cooldown.set(message.author.id, data); + + setTimeout(() => { + let data = cooldown.get(message.author.id); + + data = data.filter((v) => v !== commandInput); + + if (data.length <= 0) { + cooldown.delete(message.author.id); + } else { + cooldown.set(message.author.id, data); + }; + }, command.structure?.cooldown); + }; + + if (cooldown.has(message.author.id)) { + let data = cooldown.get(message.author.id); + + if (data.some((v) => v === commandInput)) { + await message.reply({ + content: 'Slow down buddy! You\'re too fast to use this command.' + }); + + return; + } else { + cooldownFunction(); + }; + } else { + cooldown.set(message.author.id, [commandInput]); + + cooldownFunction(); + }; + }; + command.run(client, message, args); } catch (error) { log(error, 'err');