thaldrin/events/message.js

77 lines
2.2 KiB
JavaScript

const { Collection } = require("discord.js");
const ShortLinks = require("../utils/shortlinks");
let enabled = true;
module.exports = {
name: "message",
run: async (client, msg) => {
const prefix = client.config.prefixes.find(p =>
msg.content.toLowerCase().startsWith(p)
);
if (!prefix && enabled) return ShortLinks(enabled, msg);
if (!prefix) return;
if (msg.author.bot) return;
const args = msg.content.slice(prefix.length).split(/ +/g);
const command = args.shift().toLowerCase();
const cmd = client.commands.find(
c => c.name == command || (c.aliases && c.aliases.includes(command))
);
const ctx = {
send: msg.channel.send.bind(msg.channel),
client,
msg,
args,
command: cmd,
me: msg.guild.me,
guild: msg.guild,
channel: msg.channel,
author: msg.author,
member: msg.member,
isDeveloper: client.config.developers.find(id => msg.author.id == id)
};
if (!cmd) return;
if (!client.cooldowns.has(cmd.name)) {
client.cooldowns.set(cmd.name, new Collection());
}
if (cmd.guildOnly && !msg.guild) return;
if (
cmd.developerOnly &&
!client.config.developers.find(devs => msg.author.id == devs.id)
)
return;
const now = Date.now();
const timestamps = client.cooldowns.get(cmd.name);
const cooldownAmount = (cmd.cooldown || 1) * 1000;
if (timestamps.has(msg.author.id)) {
const expirationTime = timestamps.get(msg.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return ctx.send(
`\`${cmd.name}\` has a cooldown of \`${cmd.cooldown} second${
cmd.cooldown > 1 ? "s" : ""
}\`, wait \`${`${Math.round(timeLeft)} second${
Math.round(timeLeft) > 1 ? "s" : ""
}`.replace(
"0 second",
"just a second longer"
)}\` before trying to use it again.`
);
}
} else {
timestamps.set(msg.author.id, now);
setTimeout(() => timestamps.delete(msg.author.id), cooldownAmount);
cmd
.command(ctx)
.then(() => {})
.catch(console.error);
}
}
};