|
| 1 | +import {ChannelType, Collection, Events} from 'discord.js'; |
| 2 | +import config from '../base/config.js'; |
| 3 | +const cooldown = new Collection(); |
| 4 | + |
| 5 | +export default { |
| 6 | + name: Events.MessageCreate, |
| 7 | + async execute(message) { |
| 8 | + const {client} = message; |
| 9 | + |
| 10 | + if (message.author.bot) { |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + if (message.channel.type === ChannelType.DM) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + const {prefix} = config; |
| 19 | + if (!message.content.startsWith(prefix)) { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + const args = message.content.slice(prefix.length).trim().split(/ +/g); |
| 24 | + const cmd = args.shift().toLowerCase(); |
| 25 | + |
| 26 | + if (cmd.length === 0) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + let command = client.commands.get(cmd); |
| 31 | + command ||= client.commands.get(client.commandAliases.get(cmd)); |
| 32 | + |
| 33 | + if (command) { |
| 34 | + if (command.ownerOnly && !config.owners.includes(message.author.id)) { |
| 35 | + return message.reply({content: 'Only my **developers** can use this command.'}); |
| 36 | + } |
| 37 | + |
| 38 | + if (command.cooldown) { |
| 39 | + if (cooldown.has(`${command.name}-${message.author.id}`)) { |
| 40 | + const nowDate = message.createdTimestamp; |
| 41 | + const waitedDate = cooldown.get(`${command.name}-${message.author.id}`) - nowDate; |
| 42 | + return message.reply({ |
| 43 | + content: `Cooldown is currently active, please try again <t:${Math.floor(new Date(nowDate + waitedDate).getTime() / 1000)}:R>.`, |
| 44 | + }).then(msg => setTimeout(() => msg.delete(), cooldown.get(`${command.name}-${message.author.id}`) - Date.now() + 1000)); |
| 45 | + } |
| 46 | + |
| 47 | + command.prefixRun(client, message, args); |
| 48 | + |
| 49 | + cooldown.set(`${command.name}-${message.author.id}`, Date.now() + command.cooldown); |
| 50 | + |
| 51 | + setTimeout(() => { |
| 52 | + cooldown.delete(`${command.name}-${message.author.id}`); |
| 53 | + }, command.cooldown); |
| 54 | + } else { |
| 55 | + command.prefixRun(client, message, args); |
| 56 | + } |
| 57 | + } |
| 58 | + }, |
| 59 | +}; |
0 commit comments