message.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. *
  3. * @param {require("../structures/DiscordMusicBot")} client
  4. * @param {require("discord.js").Message} message
  5. * @returns {void} aka: nothing ;-;
  6. */
  7. module.exports = async (client, message) => {
  8. if (message.author.bot || message.channel.type === "dm") return;
  9. let prefix = client.config.DefaultPrefix;
  10. let GuildDB = await client.GetGuild(message.guild.id);
  11. if (GuildDB && GuildDB.prefix) prefix = GuildDB.prefix;
  12. //Initialize GuildDB
  13. if (!GuildDB) {
  14. await client.database.guild.set(message.guild.id, {
  15. prefix: prefix,
  16. DJ: null,
  17. });
  18. GuildDB = await client.GetGuild(message.guild.id);
  19. }
  20. //Prefixes also have mention match
  21. const prefixMention = new RegExp(`^<@!?${client.user.id}> `);
  22. prefix = message.content.match(prefixMention)
  23. ? message.content.match(prefixMention)[0]
  24. : prefix;
  25. if (message.content.indexOf(prefix) !== 0) return;
  26. const args = message.content.slice(prefix.length).trim().split(/ +/g);
  27. //Making the command lowerCase because our file name will be in lowerCase
  28. const command = args.shift().toLowerCase();
  29. //Searching a command
  30. const cmd =
  31. client.commands.get(command) ||
  32. client.commands.find((x) => x.aliases && x.aliases.includes(command));
  33. //Executing the codes when we get the command or aliases
  34. if (cmd) {
  35. if (
  36. (cmd.permissions &&
  37. cmd.permissions.channel &&
  38. !message.channel
  39. .permissionsFor(client.user)
  40. .has(cmd.permissions.channel)) ||
  41. (cmd.permissions &&
  42. cmd.permissions.member &&
  43. !message.channel
  44. .permissionsFor(message.member)
  45. .has(cmd.permissions.member)) ||
  46. (cmd.permissions &&
  47. GuildDB.DJ &&
  48. !message.channel
  49. .permissionsFor(message.member)
  50. .has(["ADMINISTRATOR"]) &&
  51. !message.member.roles.cache.has(GuildDB.DJ))
  52. )
  53. return client.sendError(
  54. message.channel,
  55. "Missing Permissions!" + GuildDB.DJ
  56. ? " You need the `DJ` role to access this command."
  57. : ""
  58. );
  59. cmd.run(client, message, args, { GuildDB });
  60. client.CommandsRan++;
  61. } else return;
  62. };