thaldrin/DiscordEvents/message.js

93 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-10-09 16:19:30 +00:00
const { Collection } = require("discord.js");
2019-10-14 11:19:41 +00:00
const { ShortLinks, SourceFynnder } = require("../utils");
let ShortLinksEnabled = true;
let SourceFynnderEnabled = true;
const { table } = require("quick.db");
const Servers = new table("servers");
const Users = new table("users");
const Backend = new table("backend");
2019-10-09 16:19:30 +00:00
module.exports = {
name: "message",
run: async (client, msg) => {
const prefix = client.config.prefixes.find(p =>
msg.content.toLowerCase().startsWith(p)
);
if (msg.author.bot) return;
2019-10-14 11:19:41 +00:00
if (msg.author.id !== "318044130796109825") return;
let Server = Servers.get(msg.guild.id);
let enabled;
if (Server === null) {
enabled = require("../utils").db.defaults.server;
} else {
enabled = Server;
}
ShortLinks(enabled.Shortlinks, msg);
if (!prefix) return;
2019-10-09 16:19:30 +00:00
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,
2019-10-14 11:19:41 +00:00
db: { users: Users, servers: Servers, backend: Backend },
utils: require("../utils"),
2019-10-09 16:19:30 +00:00
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);
}
}
};