Created
July 11, 2020 13:24
-
-
Save simon-tiger/82e4c7c340062c63832bfcc9f05032db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment