From 0c37f49c1a866cd16374563c513936f9af4fc619 Mon Sep 17 00:00:00 2001 From: Fire Bird <73753258+kabirsingh2004@users.noreply.github.com> Date: Sat, 4 Mar 2023 00:47:49 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Fixed=20Issue=20and=20Updated=20?= =?UTF-8?q?=E2=9C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Fixed Issue and Updated ✅ --- Commands/Message/Misc/ping.js | 22 +++++++++ Commands/Slash/Misc/ping.js | 24 ++++++++++ command_example.js | 10 ++-- events/interactionCreate.js | 27 ++++++----- events/messageCreate.js | 84 +++++++++++++++++----------------- handlers/Client.js | 86 +++++++++++++++++++++++++++++++++++ handlers/functions.js | 37 +++++++++++++++ handlers/handler.js | 78 +++++++++++++++++++++++++++++++ index.js | 44 ++---------------- 9 files changed, 314 insertions(+), 98 deletions(-) create mode 100644 Commands/Message/Misc/ping.js create mode 100644 Commands/Slash/Misc/ping.js create mode 100644 handlers/Client.js create mode 100644 handlers/functions.js create mode 100644 handlers/handler.js diff --git a/Commands/Message/Misc/ping.js b/Commands/Message/Misc/ping.js new file mode 100644 index 0000000..100fae5 --- /dev/null +++ b/Commands/Message/Misc/ping.js @@ -0,0 +1,22 @@ +const { Message, PermissionFlagsBits } = require("discord.js"); +const { Bot } = require("../../../handlers/Client"); + +module.exports = { + name: "ping", + description: "Get Bot Real Ping !!", + userPermissions: PermissionFlagsBits.SendMessages, + botPermissions: PermissionFlagsBits.SendMessages, + category: "Misc", + cooldown: 5, + /** + * + * @param {Bot} client + * @param {Message} message + * @param {String[]} args + * @param {String} prefix + */ + run: async (client, message, args, prefix) => { + // Code + return client.sendEmbed(message, `🏓 Pong \`${client.ws.ping}\``); + }, +}; diff --git a/Commands/Slash/Misc/ping.js b/Commands/Slash/Misc/ping.js new file mode 100644 index 0000000..58cae15 --- /dev/null +++ b/Commands/Slash/Misc/ping.js @@ -0,0 +1,24 @@ +const { + CommandInteraction, + ApplicationCommandType, + PermissionFlagsBits, +} = require("discord.js"); +const { Bot } = require("../../../handlers/Client"); + +module.exports = { + name: "ping", + description: `Get Bot Real Ping !!`, + userPermissions: PermissionFlagsBits.SendMessages, + botPermissions: PermissionFlagsBits.SendMessages, + category: "Misc", + type: ApplicationCommandType.ChatInput, + /** + * + * @param {Bot} client + * @param {CommandInteraction} interaction + */ + run: async (client, interaction) => { + // Code + return client.sendEmbed(interaction, `🏓 Pong \`${client.ws.ping}\``); + }, +}; diff --git a/command_example.js b/command_example.js index 323a286..0529644 100644 --- a/command_example.js +++ b/command_example.js @@ -3,8 +3,8 @@ const { CommandInteraction, ApplicationCommandType, PermissionFlagsBits, - Client, } = require("discord.js"); +const { Bot } = require("../../../handlers/Client"); module.exports = { name: "", @@ -15,7 +15,7 @@ module.exports = { type: ApplicationCommandType.ChatInput, /** * - * @param {Client} client + * @param {Bot} client * @param {CommandInteraction} interaction */ run: async (client, interaction) => { @@ -67,10 +67,12 @@ module.exports = { }; // message commands -const { Message, PermissionFlagsBits, Client } = require("discord.js"); +const { Message, PermissionFlagsBits } = require("discord.js"); +const { Bot } = require("../../../handlers/Client"); module.exports = { name: "", + aliases: [], description: ``, userPermissions: PermissionFlagsBits.SendMessages, botPermissions: PermissionFlagsBits.SendMessages, @@ -78,7 +80,7 @@ module.exports = { cooldown: 10, /** * - * @param {Client} client + * @param {Bot} client * @param {Message} message * @param {String[]} args * @param {String} prefix diff --git a/events/interactionCreate.js b/events/interactionCreate.js index b1eb4b4..86b5176 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -1,8 +1,9 @@ -const { InteractionType } = require("discord.js"); +const { InteractionType, PermissionsBitField } = require("discord.js"); const client = require("../index"); client.on("interactionCreate", async (interaction) => { // code + if (interaction.user.bot || !interaction.guild) return; if (interaction.type == InteractionType.ApplicationCommand) { const command = client.scommands.get(interaction.commandName); if (!command) { @@ -13,20 +14,24 @@ client.on("interactionCreate", async (interaction) => { } else { if ( command.userPermissions && - !interaction.member.permissions.has(command.userPermissions) + !interaction.member.permissions.has( + PermissionsBitField.resolve(command.userPermissions) + ) ) { - return interaction.reply({ - content: `you don't have enough permissions !!`, - ephemeral: true, - }); + return client.sendEmbed( + interaction, + `You don't have enough Permissions !!` + ); } else if ( command.botPermissions && - !interaction.guild.members.me.permissions.has(command.botPermissions) + !interaction.guild.members.me.permissions.has( + PermissionsBitField.resolve(command.botPermissions) + ) ) { - return interaction.reply({ - content: `i don't have enough permissions !!`, - ephemeral: true, - }); + return client.sendEmbed( + interaction, + `I don't have enough Permissions !!` + ); } else { command.run(client, interaction); } diff --git a/events/messageCreate.js b/events/messageCreate.js index 938a056..8faae0f 100644 --- a/events/messageCreate.js +++ b/events/messageCreate.js @@ -1,65 +1,65 @@ -const { Collection } = require("discord.js"); +const { EmbedBuilder, PermissionsBitField } = require("discord.js"); +const { cooldown } = require("../handlers/functions"); const client = require("../index"); const { PREFIX } = require("../settings/config"); client.on("messageCreate", async (message) => { - if (message.author.bot || !message.guild) return; + if (message.author.bot || !message.guild || !message.id) return; + let prefix = PREFIX; - let args = message.content.slice(PREFIX.length).trim().split(/ +/); - let cmd = args.shift()?.toLowerCase(); - const command = client.mcommands.get(cmd); + let mentionprefix = new RegExp( + `^(<@!?${client.user.id}>|${escapeRegex(prefix)})\\s*` + ); + if (!mentionprefix.test(message.content)) return; + const [, nprefix] = message.content.match(mentionprefix); + const args = message.content.slice(nprefix.length).trim().split(/ +/); + const cmd = args.shift().toLowerCase(); + if (cmd.length === 0) { + if (nprefix.includes(client.user.id)) { + return message.reply({ + embeds: [ + new EmbedBuilder() + .setColor(client.config.embed.color) + .setDescription( + ` ${client.config.emoji.success} To See My All Commands Type \`/help\` or \`${prefix}help\`` + ), + ], + }); + } + } + const command = + client.mcommands.get(cmd) || + client.mcommands.find((cmds) => cmds.aliases && cmds.aliases.includes(cmd)); if (!command) return; if (command) { if ( command.userPermissions && - !message.member.permissions.has(command.userPermissions) + !message.member.permissions.has( + PermissionsBitField.resolve(command.userPermissions) + ) ) { - return message.reply({ - content: `you don't have enough permissions !!`, - }); + return client.sendEmbed(message, `You don't have enough Permissions !!`); } else if ( command.botPermissions && - !message.guild.members.me.permissions.has(command.botPermissions) + !message.guild.members.me.permissions.has( + PermissionsBitField.resolve(command.botPermissions) + ) ) { - return message.reply({ - content: `i don't have enough permissions !!`, - }); + return client.sendEmbed(message, `I don't have enough Permissions !!`); } else if (cooldown(message, command)) { - return message.reply({ - content: ` You are On Cooldown , wait \`${cooldown( + return client.sendEmbed( + message, + ` You are On Cooldown , wait \`${cooldown( message, command - ).toFixed()}\` Seconds`, - }); + ).toFixed()}\` Seconds` + ); } else { command.run(client, message, args, prefix); } } }); -function cooldown(message, cmd) { - if (!message || !cmd) return; - let { client, member } = message; - if (!client.cooldowns.has(cmd.name)) { - client.cooldowns.set(cmd.name, new Collection()); - } - const now = Date.now(); - const timestamps = client.cooldowns.get(cmd.name); - const cooldownAmount = cmd.cooldown * 1000; - if (timestamps.has(member.id)) { - const expirationTime = timestamps.get(member.id) + cooldownAmount; - if (now < expirationTime) { - const timeLeft = (expirationTime - now) / 1000; //get the lefttime - //return true - return timeLeft; - } else { - timestamps.set(member.id, now); - setTimeout(() => timestamps.delete(member.id), cooldownAmount); - return false; - } - } else { - timestamps.set(member.id, now); - setTimeout(() => timestamps.delete(member.id), cooldownAmount); - return false; - } +function escapeRegex(newprefix) { + return newprefix.replace(/[.*+?^${}()|[\]\\]/g, `\\$&`); } diff --git a/handlers/Client.js b/handlers/Client.js new file mode 100644 index 0000000..a21150a --- /dev/null +++ b/handlers/Client.js @@ -0,0 +1,86 @@ +const { + Client, + GatewayIntentBits, + Partials, + Collection, + EmbedBuilder, +} = require("discord.js"); + +class Bot extends Client { + constructor() { + super({ + partials: [ + Partials.Channel, + Partials.GuildMember, + Partials.Message, + Partials.User, + ], + intents: [ + GatewayIntentBits.Guilds, + GatewayIntentBits.GuildMessages, + GatewayIntentBits.GuildVoiceStates, + GatewayIntentBits.MessageContent, + GatewayIntentBits.GuildMembers, + ], + shards: "auto", + failIfNotExists: false, + allowedMentions: { + parse: ["everyone", "roles", "users"], + users: [], + roles: [], + repliedUser: false, + }, + }); + + // global variables + this.config = require("../settings/config"); + this.scommands = new Collection(); + this.mcommands = new Collection(); + this.cooldowns = new Collection(); + this.events = 0; + } + + async build(token) { + await loadHandlers(this); + this.login(token); + } + + sendEmbed(interaction, data) { + if (interaction.deferred) { + interaction + .followUp({ + embeds: [ + new EmbedBuilder() + .setColor(this.config.embed.color) + .setDescription(`${data.substring(0, 3000)}`), + ], + }) + .catch((e) => {}); + } else { + interaction + .reply({ + embeds: [ + new EmbedBuilder() + .setColor(this.config.embed.color) + .setDescription(`${data.substring(0, 3000)}`), + ], + }) + .catch((e) => {}); + } + } + + getFooter(user) { + return { + text: `Requested By ${user.username}`, + iconURL: user.displayAvatarURL(), + }; + } +} + +module.exports = { Bot }; + +function loadHandlers(client) { + ["handler"].forEach((file) => { + require(`./${file}`)(client); + }); +} diff --git a/handlers/functions.js b/handlers/functions.js new file mode 100644 index 0000000..e6e489c --- /dev/null +++ b/handlers/functions.js @@ -0,0 +1,37 @@ +const { Interaction, Collection } = require("discord.js"); + +/** + * + * @param {Interaction} interaction + * @param {String} cmd + */ +function cooldown(interaction, cmd) { + if (!interaction || !cmd) return; + let { client, member } = interaction; + if (!client.cooldowns.has(cmd.name)) { + client.cooldowns.set(cmd.name, new Collection()); + } + const now = Date.now(); + const timestamps = client.cooldowns.get(cmd.name); + const cooldownAmount = cmd.cooldown * 1000; + if (timestamps.has(member.id)) { + const expirationTime = timestamps.get(member.id) + cooldownAmount; + if (now < expirationTime) { + const timeLeft = (expirationTime - now) / 1000; //get the lefttime + //return true + return timeLeft; + } else { + timestamps.set(member.id, now); + setTimeout(() => timestamps.delete(member.id), cooldownAmount); + return false; + } + } else { + timestamps.set(member.id, now); + setTimeout(() => timestamps.delete(member.id), cooldownAmount); + return false; + } +} + +module.exports = { + cooldown, +}; diff --git a/handlers/handler.js b/handlers/handler.js new file mode 100644 index 0000000..e1d1892 --- /dev/null +++ b/handlers/handler.js @@ -0,0 +1,78 @@ +const { Bot } = require("./Client"); +const { readdirSync } = require("fs"); +const { + Slash: { Global, GuildID }, +} = require("../settings/config"); + +/** + * + * @param {Bot} client + */ +module.exports = async (client) => { + // code + // Message Command Handler + try { + readdirSync("./Commands/Message").forEach((dir) => { + const commands = readdirSync(`./Commands/Message/${dir}`).filter((f) => + f.endsWith(".js") + ); + + for (const cmd of commands) { + const command = require(`../Commands/Message/${dir}/${cmd}`); + if (command.name) { + client.mcommands.set(command.name, command); + } else { + console.log(`${cmd} is not ready`); + } + } + }); + console.log(`> ${client.mcommands.size} Message Commands Loaded !!`); + } catch (error) { + console.log(error); + } + + // Slash Command Handler + try { + let allCommands = []; + readdirSync("./Commands/Slash").forEach((dir) => { + const commands = readdirSync(`./Commands/Slash/${dir}`).filter((f) => + f.endsWith(".js") + ); + + for (const cmd of commands) { + const command = require(`../Commands/Slash/${dir}/${cmd}`); + if (command.name) { + client.scommands.set(command.name, command); + allCommands.push(command); + } else { + console.log(`${cmd} is not ready`); + } + } + }); + console.log(`> ${client.scommands.size} Slash Commands Loaded !!`); + + client.on("ready", async () => { + if (Global) { + client.application.commands.set(allCommands); + } else { + client.guilds.cache.get(GuildID)?.commands.set(allCommands); + } + }); + } catch (error) { + console.log(error); + } + + // Event Handler + try { + readdirSync("./events") + .filter((f) => f.endsWith(".js")) + .forEach((event) => { + require(`../events/${event}`); + client.events++; + }); + + console.log(`> ${client.events} Events Loaded !!`); + } catch (error) { + console.log(error); + } +}; diff --git a/index.js b/index.js index 1e5c8f7..927f760 100644 --- a/index.js +++ b/index.js @@ -1,48 +1,10 @@ require("dotenv").config(); -const { - Client, - Partials, - Collection, - GatewayIntentBits, -} = require("discord.js"); +const { Bot } = require("./handlers/Client"); const { TOKEN } = require("./settings/config"); -const client = new Client({ - // intents: 3276799, - intents: [ - GatewayIntentBits.Guilds, - GatewayIntentBits.GuildMembers, - GatewayIntentBits.MessageContent, - GatewayIntentBits.GuildMessages, - GatewayIntentBits.GuildVoiceStates, - ], - partials: [ - Partials.Channel, - Partials.Message, - Partials.User, - Partials.GuildMember, - ], - failIfNotExists: false, - allowedMentions: { - parse: ["everyone", "roles", "users"], - users: [], - roles: [], - repliedUser: false, - }, -}); - -// global variables -client.scommands = new Collection(); -client.mcommands = new Collection(); -client.cooldowns = new Collection(); -client.events = 0; +const client = new Bot(); module.exports = client; -// handlers -["event_handler", "slash_handler", "cmd_handler"].forEach((file) => { - require(`./handlers/${file}`)(client); -}); - // login bot -client.login(TOKEN); +client.build(TOKEN);