const Discord = require("discord.js"); const client = new Discord.Client(); const fs = require("fs"); require("dotenv").config(); const prefix = "!"; const commandFiles = fs.readdirSync("commands", "utf8"); client.commands = new Discord.Collection(); client.cooldowns = new Discord.Collection(); for (const file of commandFiles) { const command = require(`./commands/${file}`); client.commands.set(command.name, command); } client.once("ready", () => { console.log("Beep boop. Boop beep?"); }) client.on("message", message => { if (!message.content.startsWith(prefix) || message.author.bot) { return; } const args = message.content.slice(prefix.length).split(" "); const commandName = args.shift().toLowerCase(); const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName)); if (command) { if (!client.cooldowns.has(command.name)) { client.cooldowns.set(command.name, new Discord.Collection()); } const now = Date.now(); const timestamps = client.cooldowns.get(command.name); const cooldown = (command.cooldown || 3) * 1000; if (timestamps.has(message.author.id)) { const expiration = timestamps.get(message.author.id) + cooldown; if (now < expiration) { const timeLeft = (expiration - now) / 1000; message.reply(`Sorry, to avoid spam, you need to wait ${timeLeft.toFixed(1)} more seconds before you can use \`${command.name}\` again!`); } } else { timestamps.set(message.author.id, now); setTimeout(() => timestamps.delete(message.author.id), cooldown); if (command.args && args.length <= 0) { message.channel.send(`This command requires arguments: \`!${command.name} ${command.usage}\``); } else { try { command.execute(message, args); } catch (err) { message.channel.send(err.toString()); } } } } }) client.login(process.env.BOT_TOKEN);