├── .gitignore ├── Commands ├── General │ └── Say.js └── Punishment │ └── Kick.js ├── LICENSE ├── README.md ├── Utils └── Command.js ├── app.js ├── config.json ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | .env -------------------------------------------------------------------------------- /Commands/General/Say.js: -------------------------------------------------------------------------------- 1 | const Command = require("../../Utils/Command.js"); 2 | 3 | class Write extends Command { 4 | 5 | constructor(Bot) { 6 | 7 | super(Bot, { 8 | enabled: true, 9 | required_perm: "ADMINISTRATOR", // Required perm to use. (If you don't want to set this value, you can type "0" or delete it.) 10 | usages: ["write"], // Command usages with aliases. 11 | description: "Write something with bot.", // Command description. 12 | category: "General", // Command category. (If you delete this option, the category is set as the name of the folder where the command file is located.) 13 | options: [{ 14 | name: "text", 15 | description: "write a text.", 16 | type: 3, // 6 is type USER 17 | required: true 18 | }] // All arguments options of command. 19 | }); 20 | 21 | } 22 | 23 | load() { 24 | 25 | return; 26 | 27 | } 28 | 29 | async run(interaction, guild, member, args) { 30 | 31 | return await this.Bot.send(interaction, args[0].value); 32 | 33 | } 34 | 35 | } 36 | 37 | module.exports = Write; 38 | -------------------------------------------------------------------------------- /Commands/Punishment/Kick.js: -------------------------------------------------------------------------------- 1 | const Command = require("../../Utils/Command.js"); 2 | 3 | class Kick extends Command { 4 | 5 | constructor(Bot) { 6 | 7 | super(Bot, { 8 | enabled: true, 9 | required_perm: "KICK_MEMBERS", 10 | usages: ["kick"], 11 | description: "Kick members from guild.", 12 | category: "Punishment", 13 | options: [{ 14 | name: "user", 15 | description: "Enter target user.", 16 | type: 6, // 6 is type USER 17 | required: true 18 | }] 19 | }); 20 | 21 | } 22 | 23 | load() { 24 | 25 | return; 26 | 27 | } 28 | 29 | async run(interaction, guild, member, args) { 30 | 31 | const Target = guild.members.cache.get(args[0].value); 32 | if(!Target) return await this.Bot.say(`User not found!`); 33 | if(!Target.kickable) return await this.Bot.send(`❌ You do not have a permission to kick this user!`); 34 | 35 | await Target.kick(); 36 | 37 | return await this.Bot.say(`${Target} successfully kicked from the server. ✅`); 38 | 39 | } 40 | 41 | } 42 | 43 | module.exports = Kick; 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 polemikal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # discord-slash-commands-bot 2 | A great command handling feature for Discord servers! 3 | 4 | ![Image](https://cdn.discordapp.com/attachments/761343698641747999/807906122404593684/unknown.png) 5 | 6 | # 📋 Setup the Project 7 | 1. Create a new application from [Discord Developer Portal](https://discord.com/developers/applications) and build a bot. 8 | 2. Then add bot to server from auth part. 9 | 3. Give a permission to Discord bot (for load slash commands) `https://discord.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&scope=applications.commands`. 10 | 4. Open the `config.json` configuration file and write your bot token. 11 | 5. Open a new terminal from project folder then run `app.js` file. (Example run command: `node app.js`) 12 | 13 | # ❓ Type of Slash Command Options 14 | ``` 15 | NAME VALUE 16 | SUB_COMMAND 1 17 | SUB_COMMAND_GROUP 2 18 | STRING 3 19 | INTEGER 4 20 | BOOLEAN 5 21 | USER 6 22 | CHANNEL 7 23 | ROLE 8 24 | ``` 25 | 26 | # 📜 Documents for Slash Commands 27 | 28 | If you have anything stuck in your mind, you can clear your questions here: [Slash Command Docs](https://discord.com/developers/docs/interactions/slash-commands) 29 | 30 | # 📌 Information 31 | 32 | **(!)** If there is something that can be added to this project or if you have found anything missing in this project, you can let us know and help us improve the project! [Let us know!](https://github.com/polemikal/discord-slash-commands-bot/issues) 33 | 34 | 35 | -------------------------------------------------------------------------------- /Utils/Command.js: -------------------------------------------------------------------------------- 1 | class Command { 2 | 3 | constructor(bot, options) { 4 | 5 | this.Bot = bot; 6 | this.required_perm = options.required_perm || 0; 7 | this.enabled = options.enabled || true; 8 | this.usages = options.usages || null; 9 | this.description = options.description || "No description for this command."; 10 | this.category = options.category || null; 11 | this.options = options.options || null; 12 | 13 | } 14 | } 15 | 16 | module.exports = Command; -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const { Client, Collection, APIMessage, Permissions } = require("discord.js"); 2 | 3 | const fs = require("fs"); 4 | const Config = (global.Config = JSON.parse(fs.readFileSync("./config.json", { encoding: "utf-8" }))); 5 | 6 | const Bot = (global.Bot = new Client({ fetchAllMembers: true, disableMentions: "none" })); 7 | const Commands = (global.Commands = new Collection()); 8 | 9 | const AsciiTable = require("ascii-table"); 10 | const CommandTable = new AsciiTable("List of Commands"); 11 | 12 | const fetch = require("node-fetch").default 13 | 14 | Bot.once("ready", async() => { 15 | await new Promise(async function(resolve, reject) { 16 | 17 | const commandsList = await Bot.api.applications(Bot.user.id).commands.get(); 18 | 19 | const Dirs = fs.readdirSync("./Commands"); 20 | for(const commandDir of Dirs) { 21 | const Files = fs.readdirSync("./Commands/" + commandDir).filter(e => e.endsWith(".js")); 22 | for(const commandFile of Files) { 23 | const Command = new (require("./Commands/" + commandDir + "/" + commandFile))(Bot); 24 | if(!Command.usages || !Command.usages.length) { 25 | reject("ERROR! Cannot load \'" + commandFile + "\' command file: Command usages not found!"); 26 | } 27 | if(!Command.options || !Array.isArray(Command.options)) { 28 | reject("ERROR! Cannot load \'" + commandFile + "\' command file: Command options is not set!"); 29 | } 30 | 31 | CommandTable.addRow(commandFile, `Command: ${Command.usages[0]} | Aliases: ${Command.usages.slice(1).join(", ")} | Category: ${Command.category || dir}`, "✅"); 32 | Commands.set(Command.usages[0], Command) 33 | Command.usages.forEach(usage => { 34 | if(commandsList.some(cmd => cmd.name === usage)) { 35 | Bot.api.applications(Bot.user.id).commands(commandsList.find(cmd => cmd.name === usage).id).patch({ 36 | data: { 37 | name: usage, 38 | description: Command.description, 39 | options: Command.options 40 | } 41 | }); 42 | } else { 43 | Bot.api.applications(Bot.user.id).commands.post({ 44 | data: { 45 | name: usage, 46 | description: Command.description, 47 | options: Command.options 48 | } 49 | }); 50 | } 51 | }); 52 | 53 | Command.load(); 54 | } 55 | } 56 | 57 | commandsList.filter(cmd => !Commands.keyArray().includes(cmd.name)).forEach(cmd => { 58 | fetch("https://discord.com/api/v8/applications/" + Bot.user.id + "/commands/" + cmd.id, { 59 | method: "DELETE", 60 | headers: { 61 | Authorization: `Bot ${Bot.token}`, 62 | "Content-Type": "application/json", 63 | } 64 | }); 65 | }); 66 | 67 | if(CommandTable.getRows().length < 1) CommandTable.addRow("❌", "❌", `❌ -> No commands found.`); 68 | console.log(CommandTable.toString()); 69 | resolve(); 70 | 71 | }); 72 | 73 | Bot.ws.on("INTERACTION_CREATE", async(interaction) => { 74 | const Command = Commands.get(interaction.data.name) || Commands.find(e => e.usages.some(a => a === interaction.data.name)); 75 | if(!Command || (!Command.enabled || Command.enabled != true)) return; 76 | if(Command.required_perm != 0 && Command.required_perm.length && !Bot.hasPermission(interaction.member, Command.required_perm)) return await Bot.say(interaction, `You must have a \`${Command.required_perm.toUpperCase()}\` permission to use this command!`) 77 | const Guild = Bot.guilds.cache.get(interaction.guild_id); 78 | const Member = Guild.member(interaction.member.user.id); 79 | return Command.run(interaction, Guild, Member, interaction.data.options); 80 | }); 81 | 82 | Bot.user.setPresence({ 83 | status: "dnd", 84 | activity: { 85 | name: Config.DEFAULTS.ACTIVITY_TEXT, 86 | type: "WATCHING" 87 | } 88 | }); 89 | 90 | console.log(`[BOT] \'${Bot.user.username}\' client has been activated!`); 91 | 92 | }); 93 | 94 | Bot.login(Config.DEFAULTS.TOKEN).catch(err => { 95 | console.error("ERROR! An occured error while connectiong to client: " + err.message); 96 | Bot.destroy(); 97 | }); 98 | 99 | const AllPermissions = new Permissions(Permissions.ALL).toArray(); 100 | Bot.hasPermission = function(member, permission) { 101 | if(!AllPermissions.includes(permission.toUpperCase())) return true; 102 | const Perms = new Permissions(Number(member.permissions)); 103 | if(Perms.has(permission.toUpperCase())) return true; 104 | return false; 105 | } 106 | 107 | Bot.send = async function(interaction, content) { 108 | return Bot.api.interactions(interaction.id, interaction.token).callback.post({ 109 | data: { 110 | type: 4, 111 | data: await createAPIMessage(interaction, content) 112 | } 113 | }); 114 | }; 115 | 116 | async function createAPIMessage(interaction, content) { 117 | const apiMessage = await APIMessage.create(Bot.channels.resolve(interaction.channel_id), content).resolveData().resolveFiles(); 118 | return { ...apiMessage.data, files: apiMessage.files }; 119 | }; 120 | -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "DEFAULTS": { 3 | "TOKEN": "", 4 | "AUTHORS": [], 5 | "ACTIVITY_TEXT": "Discord Slash Commands" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "polemikal-altyapi-v12", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "@discordjs/collection": { 8 | "version": "0.1.6", 9 | "resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-0.1.6.tgz", 10 | "integrity": "sha512-utRNxnd9kSS2qhyivo9lMlt5qgAUasH2gb7BEOn6p0efFh24gjGomHzWKMAPn2hEReOPQZCJaRKoURwRotKucQ==" 11 | }, 12 | "@discordjs/form-data": { 13 | "version": "3.0.1", 14 | "resolved": "https://registry.npmjs.org/@discordjs/form-data/-/form-data-3.0.1.tgz", 15 | "integrity": "sha512-ZfFsbgEXW71Rw/6EtBdrP5VxBJy4dthyC0tpQKGKmYFImlmmrykO14Za+BiIVduwjte0jXEBlhSKf0MWbFp9Eg==", 16 | "requires": { 17 | "asynckit": "^0.4.0", 18 | "combined-stream": "^1.0.8", 19 | "mime-types": "^2.1.12" 20 | } 21 | }, 22 | "abort-controller": { 23 | "version": "3.0.0", 24 | "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", 25 | "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", 26 | "requires": { 27 | "event-target-shim": "^5.0.0" 28 | } 29 | }, 30 | "ascii-table": { 31 | "version": "0.0.9", 32 | "resolved": "https://registry.npmjs.org/ascii-table/-/ascii-table-0.0.9.tgz", 33 | "integrity": "sha1-BqZgTWpV1L9BqaR9mHLXp42jHnM=" 34 | }, 35 | "asynckit": { 36 | "version": "0.4.0", 37 | "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", 38 | "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" 39 | }, 40 | "combined-stream": { 41 | "version": "1.0.8", 42 | "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", 43 | "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", 44 | "requires": { 45 | "delayed-stream": "~1.0.0" 46 | } 47 | }, 48 | "delayed-stream": { 49 | "version": "1.0.0", 50 | "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", 51 | "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" 52 | }, 53 | "discord.js": { 54 | "version": "12.5.1", 55 | "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-12.5.1.tgz", 56 | "integrity": "sha512-VwZkVaUAIOB9mKdca0I5MefPMTQJTNg0qdgi1huF3iwsFwJ0L5s/Y69AQe+iPmjuV6j9rtKoG0Ta0n9vgEIL6w==", 57 | "requires": { 58 | "@discordjs/collection": "^0.1.6", 59 | "@discordjs/form-data": "^3.0.1", 60 | "abort-controller": "^3.0.0", 61 | "node-fetch": "^2.6.1", 62 | "prism-media": "^1.2.2", 63 | "setimmediate": "^1.0.5", 64 | "tweetnacl": "^1.0.3", 65 | "ws": "^7.3.1" 66 | } 67 | }, 68 | "event-target-shim": { 69 | "version": "5.0.1", 70 | "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", 71 | "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==" 72 | }, 73 | "fs": { 74 | "version": "0.0.1-security", 75 | "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", 76 | "integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ=" 77 | }, 78 | "mime-db": { 79 | "version": "1.45.0", 80 | "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.45.0.tgz", 81 | "integrity": "sha512-CkqLUxUk15hofLoLyljJSrukZi8mAtgd+yE5uO4tqRZsdsAJKv0O+rFMhVDRJgozy+yG6md5KwuXhD4ocIoP+w==" 82 | }, 83 | "mime-types": { 84 | "version": "2.1.28", 85 | "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.28.tgz", 86 | "integrity": "sha512-0TO2yJ5YHYr7M2zzT7gDU1tbwHxEUWBCLt0lscSNpcdAfFyJOVEpRYNS7EXVcTLNj/25QO8gulHC5JtTzSE2UQ==", 87 | "requires": { 88 | "mime-db": "1.45.0" 89 | } 90 | }, 91 | "moment": { 92 | "version": "2.29.1", 93 | "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", 94 | "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==" 95 | }, 96 | "moment-duration-format": { 97 | "version": "2.3.2", 98 | "resolved": "https://registry.npmjs.org/moment-duration-format/-/moment-duration-format-2.3.2.tgz", 99 | "integrity": "sha512-cBMXjSW+fjOb4tyaVHuaVE/A5TqkukDWiOfxxAjY+PEqmmBQlLwn+8OzwPiG3brouXKY5Un4pBjAeB6UToXHaQ==" 100 | }, 101 | "node-fetch": { 102 | "version": "2.6.1", 103 | "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", 104 | "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==" 105 | }, 106 | "prism-media": { 107 | "version": "1.2.3", 108 | "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-1.2.3.tgz", 109 | "integrity": "sha512-fSrR66n0l6roW9Rx4rSLMyTPTjRTiXy5RVqDOurACQ6si1rKHHKDU5gwBJoCsIV0R3o9gi+K50akl/qyw1C74A==" 110 | }, 111 | "setimmediate": { 112 | "version": "1.0.5", 113 | "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", 114 | "integrity": "sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU=" 115 | }, 116 | "tweetnacl": { 117 | "version": "1.0.3", 118 | "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.3.tgz", 119 | "integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==" 120 | }, 121 | "ws": { 122 | "version": "7.4.2", 123 | "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.2.tgz", 124 | "integrity": "sha512-T4tewALS3+qsrpGI/8dqNMLIVdq/g/85U98HPMa6F0m6xTbvhXU6RCQLqPH3+SlomNV/LdY6RXEbBpMH6EOJnA==" 125 | } 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "polemikal-altyapi-v12", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "polemikal", 10 | "license": "ISC", 11 | "dependencies": { 12 | "ascii-table": "0.0.9", 13 | "discord.js": "^12.5.1", 14 | "fs": "0.0.1-security", 15 | "moment": "^2.29.1", 16 | "moment-duration-format": "^2.3.2", 17 | "node-fetch": "^2.6.1" 18 | } 19 | } 20 | --------------------------------------------------------------------------------