// Base requires const { Client, GatewayIntentBits, Events } = require("discord.js") const fs = require("fs") // TOKEN is stored in the .env file (node --env-file=.env index.js) const token = process.env.TOKEN // Creating a client const client = new Client({ intents: [ GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.DirectMessages, GatewayIntentBits.Guilds ] } ) // Exporting the client for event and command files to use it module.exports.client = client module.exports.dir = __dirname // Requiring all the event files. fs.readdir(`${__dirname}/events/`, (err, files) => { files.forEach(file => { if (!file.endsWith(".js")) { return } require(`${__dirname}/events/${file}`) }) }) // Command list const commands = [] // Requiring the command files fs.readdir(`${__dirname}/commands`, (err, files) => { files.forEach(file => { if (!file.endsWith("js")) { return } const commandList = require(`${__dirname}/commands/${file}`) commands.push(commandList) }) }) // Function to get the command with that name function getCommand(commandName) { var command; commands.forEach(commandList => { commandList.forEach(commandArray => { if (commandArray.name == commandName) { command = commandArray } }) }) return command; } // Parsing commands on message create client.on("messageCreate", (message) => { var content = message.content // Continue only if starts with the prefix if (!content.startsWith('!')) { return } // Need to remove the prefix after the check content = content.slice(1, content.length) // Splitting the message into command and args const parsed = content.split(" ") // Getting command name const command = parsed[0] // Removing command from the args const args = parsed.filter(value => {return value != command}) // Parsing args.forEach((arg, index) => { if (arg.startsWith("<@")) { // User, lets get the id const id = arg.slice(2, arg.length - 1) const member = message.guild.members.cache.find(member => member.user.id == id) if (member) { args[index] = member } } else if (arg.startsWith("<#")) { // Channel, lets get the id const id = arg.slice(2, arg.length - 1) const channel = message.guild.channels.cache.find(channel => channel.id == id) if (channel) { args[index] = channel } } }) // Actual command const realCommand = getCommand(command) // Only continue if command exists if (!realCommand) { return } // Run command realCommand.run(message, ...args) //...args passes it as separate args rather than an Array }) // After getting the command list we can export it module.exports.commands = commands // Time to login client.login(token)