|
1 |
| -const { Collection } = require("discord.js"); |
| 1 | +const { EmbedBuilder, PermissionsBitField } = require("discord.js"); |
| 2 | +const { cooldown } = require("../handlers/functions"); |
2 | 3 | const client = require("../index");
|
3 | 4 | const { PREFIX } = require("../settings/config");
|
4 | 5 |
|
5 | 6 | client.on("messageCreate", async (message) => {
|
6 |
| - if (message.author.bot || !message.guild) return; |
| 7 | + if (message.author.bot || !message.guild || !message.id) return; |
| 8 | + |
7 | 9 | let prefix = PREFIX;
|
8 |
| - let args = message.content.slice(PREFIX.length).trim().split(/ +/); |
9 |
| - let cmd = args.shift()?.toLowerCase(); |
10 |
| - const command = client.mcommands.get(cmd); |
| 10 | + let mentionprefix = new RegExp( |
| 11 | + `^(<@!?${client.user.id}>|${escapeRegex(prefix)})\\s*` |
| 12 | + ); |
| 13 | + if (!mentionprefix.test(message.content)) return; |
| 14 | + const [, nprefix] = message.content.match(mentionprefix); |
| 15 | + const args = message.content.slice(nprefix.length).trim().split(/ +/); |
| 16 | + const cmd = args.shift().toLowerCase(); |
| 17 | + if (cmd.length === 0) { |
| 18 | + if (nprefix.includes(client.user.id)) { |
| 19 | + return message.reply({ |
| 20 | + embeds: [ |
| 21 | + new EmbedBuilder() |
| 22 | + .setColor(client.config.embed.color) |
| 23 | + .setDescription( |
| 24 | + ` ${client.config.emoji.success} To See My All Commands Type \`/help\` or \`${prefix}help\`` |
| 25 | + ), |
| 26 | + ], |
| 27 | + }); |
| 28 | + } |
| 29 | + } |
| 30 | + const command = |
| 31 | + client.mcommands.get(cmd) || |
| 32 | + client.mcommands.find((cmds) => cmds.aliases && cmds.aliases.includes(cmd)); |
11 | 33 | if (!command) return;
|
12 | 34 | if (command) {
|
13 | 35 | if (
|
14 | 36 | command.userPermissions &&
|
15 |
| - !message.member.permissions.has(command.userPermissions) |
| 37 | + !message.member.permissions.has( |
| 38 | + PermissionsBitField.resolve(command.userPermissions) |
| 39 | + ) |
16 | 40 | ) {
|
17 |
| - return message.reply({ |
18 |
| - content: `you don't have enough permissions !!`, |
19 |
| - }); |
| 41 | + return client.sendEmbed(message, `You don't have enough Permissions !!`); |
20 | 42 | } else if (
|
21 | 43 | command.botPermissions &&
|
22 |
| - !message.guild.members.me.permissions.has(command.botPermissions) |
| 44 | + !message.guild.members.me.permissions.has( |
| 45 | + PermissionsBitField.resolve(command.botPermissions) |
| 46 | + ) |
23 | 47 | ) {
|
24 |
| - return message.reply({ |
25 |
| - content: `i don't have enough permissions !!`, |
26 |
| - }); |
| 48 | + return client.sendEmbed(message, `I don't have enough Permissions !!`); |
27 | 49 | } else if (cooldown(message, command)) {
|
28 |
| - return message.reply({ |
29 |
| - content: ` You are On Cooldown , wait \`${cooldown( |
| 50 | + return client.sendEmbed( |
| 51 | + message, |
| 52 | + ` You are On Cooldown , wait \`${cooldown( |
30 | 53 | message,
|
31 | 54 | command
|
32 |
| - ).toFixed()}\` Seconds`, |
33 |
| - }); |
| 55 | + ).toFixed()}\` Seconds` |
| 56 | + ); |
34 | 57 | } else {
|
35 | 58 | command.run(client, message, args, prefix);
|
36 | 59 | }
|
37 | 60 | }
|
38 | 61 | });
|
39 | 62 |
|
40 |
| -function cooldown(message, cmd) { |
41 |
| - if (!message || !cmd) return; |
42 |
| - let { client, member } = message; |
43 |
| - if (!client.cooldowns.has(cmd.name)) { |
44 |
| - client.cooldowns.set(cmd.name, new Collection()); |
45 |
| - } |
46 |
| - const now = Date.now(); |
47 |
| - const timestamps = client.cooldowns.get(cmd.name); |
48 |
| - const cooldownAmount = cmd.cooldown * 1000; |
49 |
| - if (timestamps.has(member.id)) { |
50 |
| - const expirationTime = timestamps.get(member.id) + cooldownAmount; |
51 |
| - if (now < expirationTime) { |
52 |
| - const timeLeft = (expirationTime - now) / 1000; //get the lefttime |
53 |
| - //return true |
54 |
| - return timeLeft; |
55 |
| - } else { |
56 |
| - timestamps.set(member.id, now); |
57 |
| - setTimeout(() => timestamps.delete(member.id), cooldownAmount); |
58 |
| - return false; |
59 |
| - } |
60 |
| - } else { |
61 |
| - timestamps.set(member.id, now); |
62 |
| - setTimeout(() => timestamps.delete(member.id), cooldownAmount); |
63 |
| - return false; |
64 |
| - } |
| 63 | +function escapeRegex(newprefix) { |
| 64 | + return newprefix.replace(/[.*+?^${}()|[\]\\]/g, `\\$&`); |
65 | 65 | }
|
0 commit comments