diff --git a/Commands/Message/Misc/avatar.js b/Commands/Message/Misc/avatar.js new file mode 100644 index 0000000..d9b3351 --- /dev/null +++ b/Commands/Message/Misc/avatar.js @@ -0,0 +1,41 @@ +const { + Message, + PermissionFlagsBits, + Client, + EmbedBuilder, +} = require("discord.js"); + +module.exports = { + name: "avatar", + description: "Get Avar Of a User !!", + userPermissions: PermissionFlagsBits.SendMessages, + botPermissions: PermissionFlagsBits.SendMessages, + category: "Misc", + cooldown: 5, + /** + * + * @param {Client} client + * @param {Message} message + * @param {String[]} args + * @param {String} prefix + */ + run: async (client, message, args, prefix) => { + // Code + let AvatarUrl = message.author.displayAvatarURL({ + extension: "png", + size: 512, + }); + message.reply({ + embeds: [ + new EmbedBuilder() + .setColor("Blurple") + .setAuthor({ + name: `Avatar Of ${message.author.tag}`, + iconURL: AvatarUrl, + }) + .setImage(AvatarUrl) + .setTimestamp(), + ], + }); + }, +}; diff --git a/Commands/Message/Misc/ping.js b/Commands/Message/Misc/ping.js new file mode 100644 index 0000000..1630b7f --- /dev/null +++ b/Commands/Message/Misc/ping.js @@ -0,0 +1,23 @@ +const { Message, PermissionFlagsBits, Client } = require("discord.js"); + +module.exports = { + name: "ping", + description: "Get Bot Real Ping !!", + userPermissions: PermissionFlagsBits.SendMessages, + botPermissions: PermissionFlagsBits.SendMessages, + category: "Misc", + cooldown: 5, + /** + * + * @param {Client} client + * @param {Message} message + * @param {String[]} args + * @param {String} prefix + */ + run: async (client, message, args, prefix) => { + // Code + message.reply({ + content: `> Pong \`${client.ws.ping}\``, + }); + }, +}; diff --git a/Commands/Slash/Misc/avatar.js b/Commands/Slash/Misc/avatar.js new file mode 100644 index 0000000..b9d0d16 --- /dev/null +++ b/Commands/Slash/Misc/avatar.js @@ -0,0 +1,41 @@ +const { + CommandInteraction, + ApplicationCommandType, + PermissionFlagsBits, + Client, + EmbedBuilder, +} = require("discord.js"); + +module.exports = { + name: "avatar", + description: `Get Avatar of a User !!`, + userPermissions: PermissionFlagsBits.SendMessages, + botPermissions: PermissionFlagsBits.SendMessages, + category: "Misc", + type: ApplicationCommandType.ChatInput, + /** + * + * @param {Client} client + * @param {CommandInteraction} interaction + */ + run: async (client, interaction) => { + // Code + let AvatarUrl = interaction.user.displayAvatarURL({ + extension: "png", + size: 512, + }); + interaction.reply({ + embeds: [ + new EmbedBuilder() + .setColor("Blurple") + .setAuthor({ + name: `Avatar Of ${interaction.user.tag}`, + iconURL: AvatarUrl, + }) + .setImage(AvatarUrl) + .setTimestamp(), + ], + ephemeral: true, + }); + }, +}; diff --git a/Commands/Slash/Misc/ping.js b/Commands/Slash/Misc/ping.js new file mode 100644 index 0000000..dee0dcf --- /dev/null +++ b/Commands/Slash/Misc/ping.js @@ -0,0 +1,27 @@ +const { + CommandInteraction, + ApplicationCommandType, + PermissionFlagsBits, + Client, +} = require("discord.js"); + +module.exports = { + name: "ping", + description: `Get Bot Real Ping !!`, + userPermissions: PermissionFlagsBits.SendMessages, + botPermissions: PermissionFlagsBits.SendMessages, + category: "Misc", + type: ApplicationCommandType.ChatInput, + /** + * + * @param {Client} client + * @param {CommandInteraction} interaction + */ + run: async (client, interaction) => { + // Code + interaction.reply({ + content: `> Pong \`${client.ws.ping}\``, + ephemeral: true, + }); + }, +}; diff --git a/LICENSE b/LICENSE index 6043202..201c2e0 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 ᴋᴀʙɪʀ々ꜱɪɴɢʜ +Copyright (c) 2022 ᴋᴀʙɪʀ々ꜱɪɴɢʜ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 870fc8a..abfe9da 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,5 @@ -# Hi, I'm Kabir! 👋 - -I'm a Discord Bot Developer and here is mine bot handler - -# Discord.JS V14 Handler - -a discord.js handler which support slash commands , message commands , events and more... +

+Discord Bot Using JavaScript Discord.JS


## **Installation | How to use the Bot** @@ -14,138 +9,18 @@ a discord.js handler which support slash commands , message commands , events an **3.** Fill in everything in **`settings/config.js`** -**4.** after Fill everything in config Type in shall **`npm install`** - -**5.** start the bot with **`node index.js`** +**4.** start the bot with **`node index.js`**
### _Modify - config.js_ ```javascript -{ - token: "BOT_TOKEN" - prefix: "BOT_PREFIX", -} -``` - -## Handler Features - -- easy to use Handler -- support event Handler -- slash commands support -- message commands support -- based on [discord.js](https://discord.js.org/#/) -- provied code snipet for commands -- support sub directory in commands folder -- support code suggestions in Handler - -## Feedback - -If you have any feedback, please reach out to us at [Discord Server](https://discord.gg/PcUVWApWN3) - -## Usage/Examples - -- Commands Example - -```javascript -// chat input slash commands -const { - CommandInteraction, - ApplicationCommandType, - PermissionFlagsBits, -} = require("discord.js"); -const BOT = require("../../../handlers/Client"); - -module.exports = { - name: "", - description: ``, - userPermissions: PermissionFlagsBits.SendMessages, - botPermissions: PermissionFlagsBits.SendMessages, - category: "", - cooldown: 10, - type: ApplicationCommandType.ChatInput, - /** - * - * @param {BOT} client - * @param {CommandInteraction} interaction - * @param {String[]} args - */ - run: async (client, interaction, args) => { - // Code - }, -}; - -// message input slash commands -const { Message, PermissionFlagsBits } = require("discord.js"); -const BOT = require("../../../handlers/Client"); - -module.exports = { - name: "", - description: ``, - userPermissions: PermissionFlagsBits.SendMessages, - botPermissions: PermissionFlagsBits.SendMessages, - category: "", - cooldown: 10, - /** - * - * @param {BOT} client - * @param {Message} message - * @param {String[]} args - * @param {String} prefix - */ - run: async (client, message, args, prefix) => { - // Code - }, -}; - -// user slash commands - -const { - ContextMenuCommandInteraction, - ApplicationCommandType, -} = require("discord.js"); -const BOT = require("../../../handlers/Client"); - -module.exports = { - name: "", - category: "", - type: ApplicationCommandType.User, - /** - * - * @param {BOT} client - * @param {ContextMenuCommandInteraction} interaction - */ - run: async (client, interaction) => { - // Code - }, -}; - -// message commands -const { Message } = require("discord.js"); -const BOT = require("../../../handlers/Client"); - module.exports = { - name: "", - description: ``, - userPermissions: [], - botPermissions: [], - category: "", - cooldown: 10, - /** - * - * @param {BOT} client - * @param {Message} message - * @param {String[]} args - * @param {String} prefix - */ - run: async (client, message, args, prefix) => { - // Code - }, -}; + TOKEN : process.env.TOKEN || "BOT_TOKEN", + PREFIX : process.env.PREFIX || "BOT_PREFIX" +} ``` -## License - -[MIT](https://choosealicense.com/licenses/mit/) +
-# Thanks For Using Mine Handler Please Give a Star +If Any Bug Open Pull Request diff --git a/command_example.js b/command_example.js index d33980a..323a286 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: "", @@ -12,15 +12,13 @@ module.exports = { userPermissions: PermissionFlagsBits.SendMessages, botPermissions: PermissionFlagsBits.SendMessages, category: "", - cooldown: 10, type: ApplicationCommandType.ChatInput, /** * - * @param {BOT} client + * @param {Client} client * @param {CommandInteraction} interaction - * @param {String[]} args */ - run: async (client, interaction, args) => { + run: async (client, interaction) => { // Code }, }; @@ -29,8 +27,8 @@ module.exports = { const { ContextMenuCommandInteraction, ApplicationCommandType, + Client, } = require("discord.js"); -const BOT = require("../../../handlers/Client"); module.exports = { name: "", @@ -38,7 +36,7 @@ module.exports = { type: ApplicationCommandType.Message, /** * - * @param {BOT} client + * @param {Client} client * @param {ContextMenuCommandInteraction} interaction */ run: async (client, interaction) => { @@ -51,8 +49,8 @@ module.exports = { const { ContextMenuCommandInteraction, ApplicationCommandType, + Client, } = require("discord.js"); -const BOT = require("../../../handlers/Client"); module.exports = { name: "", @@ -60,7 +58,7 @@ module.exports = { type: ApplicationCommandType.User, /** * - * @param {BOT} client + * @param {Client} client * @param {ContextMenuCommandInteraction} interaction */ run: async (client, interaction) => { @@ -69,8 +67,7 @@ module.exports = { }; // message commands -const { Message, PermissionFlagsBits } = require("discord.js"); -const BOT = require("../../../handlers/Client"); +const { Message, PermissionFlagsBits, Client } = require("discord.js"); module.exports = { name: "", @@ -81,7 +78,7 @@ module.exports = { cooldown: 10, /** * - * @param {BOT} client + * @param {Client} client * @param {Message} message * @param {String[]} args * @param {String} prefix diff --git a/events/interactionCreate.js b/events/interactionCreate.js new file mode 100644 index 0000000..b1eb4b4 --- /dev/null +++ b/events/interactionCreate.js @@ -0,0 +1,35 @@ +const { InteractionType } = require("discord.js"); +const client = require("../index"); + +client.on("interactionCreate", async (interaction) => { + // code + if (interaction.type == InteractionType.ApplicationCommand) { + const command = client.scommands.get(interaction.commandName); + if (!command) { + return interaction.reply({ + content: `\`${interaction.commandName}\` is not valid command !!`, + ephemeral: true, + }); + } else { + if ( + command.userPermissions && + !interaction.member.permissions.has(command.userPermissions) + ) { + return interaction.reply({ + content: `you don't have enough permissions !!`, + ephemeral: true, + }); + } else if ( + command.botPermissions && + !interaction.guild.members.me.permissions.has(command.botPermissions) + ) { + return interaction.reply({ + content: `i don't have enough permissions !!`, + ephemeral: true, + }); + } else { + command.run(client, interaction); + } + } + } +}); diff --git a/events/messageCreate.js b/events/messageCreate.js new file mode 100644 index 0000000..938a056 --- /dev/null +++ b/events/messageCreate.js @@ -0,0 +1,65 @@ +const { Collection } = require("discord.js"); +const client = require("../index"); +const { PREFIX } = require("../settings/config"); + +client.on("messageCreate", async (message) => { + if (message.author.bot || !message.guild) return; + let prefix = PREFIX; + let args = message.content.slice(PREFIX.length).trim().split(/ +/); + let cmd = args.shift()?.toLowerCase(); + const command = client.mcommands.get(cmd); + if (!command) return; + if (command) { + if ( + command.userPermissions && + !message.member.permissions.has(command.userPermissions) + ) { + return message.reply({ + content: `you don't have enough permissions !!`, + }); + } else if ( + command.botPermissions && + !message.guild.members.me.permissions.has(command.botPermissions) + ) { + return message.reply({ + content: `i don't have enough permissions !!`, + }); + } else if (cooldown(message, command)) { + return message.reply({ + content: ` You are On Cooldown , wait \`${cooldown( + message, + command + ).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; + } +} diff --git a/events/ready.js b/events/ready.js new file mode 100644 index 0000000..9042fe9 --- /dev/null +++ b/events/ready.js @@ -0,0 +1,10 @@ +const { ActivityType } = require("discord.js"); +const client = require("../index"); + +client.on("ready", () => { + console.log(`bot is ready for work !!`); + client.user.setActivity({ + name: `Coded By Kabir ❤️‍🔥`, + type: ActivityType.Watching, + }); +}); diff --git a/handlers/cmd_handler.js b/handlers/cmd_handler.js new file mode 100644 index 0000000..1ce6798 --- /dev/null +++ b/handlers/cmd_handler.js @@ -0,0 +1,28 @@ +const { Client } = require("discord.js"); +const { readdirSync } = require("fs"); + +/** + * + * @param {Client} client + */ +module.exports = async (client) => { + 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); + } +}; diff --git a/handlers/event_handler.js b/handlers/event_handler.js new file mode 100644 index 0000000..8058403 --- /dev/null +++ b/handlers/event_handler.js @@ -0,0 +1,17 @@ +const { Client } = require("discord.js"); +const { readdirSync } = require("fs"); + +/** + * + * @param {Client} client + */ +module.exports = async (client) => { + readdirSync("./events") + .filter((f) => f.endsWith(".js")) + .forEach((event) => { + require(`../events/${event}`); + client.events++; + }); + + console.log(`> ${client.events} Events Loaded !!`); +}; diff --git a/handlers/slash_handler.js b/handlers/slash_handler.js new file mode 100644 index 0000000..86fa7b8 --- /dev/null +++ b/handlers/slash_handler.js @@ -0,0 +1,41 @@ +const { Client } = require("discord.js"); +const { readdirSync } = require("fs"); +const { + Slash: { Global, GuildID }, +} = require("../settings/config"); + +/** + * + * @param {Client} client + */ +module.exports = async (client) => { + 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); + } +}; diff --git a/index.js b/index.js index 1f8346e..44e7114 100644 --- a/index.js +++ b/index.js @@ -1,22 +1,47 @@ require("dotenv").config(); -const BOT = require("./handlers/Client"); -const { token } = require("./settings/config"); +const { + Client, + Partials, + Collection, + GatewayIntentBits, +} = require("discord.js"); +const { TOKEN } = require("./settings/config"); -const client = new BOT(); +const client = new Client({ + intents: 3276799, + // intents: [ + // GatewayIntentBits.Guilds, + // GatewayIntentBits.GuildMembers, + // GatewayIntentBits.MessageContent, + // GatewayIntentBits.GuildMessages, + // ], + 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; module.exports = client; -client.build(token); +// handlers +["event_handler", "slash_handler", "cmd_handler"].forEach((file) => { + require(`./handlers/${file}`)(client); +}); -// process.on("unhandledRejection", (reason, p) => { -// console.log("[Error_Handling] :: Unhandled Rejection/Catch"); -// console.log(reason, p); -// }); -// process.on("uncaughtException", (err, origin) => { -// console.log(" [Error_Handling] :: Uncaught Exception/Catch"); -// console.log(err, origin); -// }); -// process.on("uncaughtExceptionMonitor", (err, origin) => { -// console.log(" [Error_Handling] :: Uncaught Exception/Catch (MONITOR)"); -// console.log(err, origin); -// }); +// login bot +client.login(TOKEN); diff --git a/settings/config.js b/settings/config.js index 15de941..129f552 100644 --- a/settings/config.js +++ b/settings/config.js @@ -1,24 +1,8 @@ module.exports = { - token: process.env.TOKEN || "BOT_TOKEN", - prefix: process.env.PREFIX || "BOT_PREFIX", - embed: { - color: "#2f3136", - wrongcolor: "#2f3136", - }, - emoji: { - ERROR: "❌", - SUCCESS: "✅", - disabled: "🔴", - enabled: "🟢", - cleared: "🧹", - time: "⏲️", - search: "🔎", - ping: "🏓", - bot: "🤖", - }, - // others - slash: { - global: true, - guildID: "GuildId", // for slash command + TOKEN: process.env.TOKEN || "BOT_TOKEN", + PREFIX: process.env.PREFIX || "BOT_PREFIX", + Slash: { + Global: false, + GuildID: process.env.GuildID || "GUILD_ID", }, };