From 80c93c95e00d0bd5082df076ba445de6516d2d8f Mon Sep 17 00:00:00 2001 From: "Jonny_Bro (Nikita)" <48434875+JonnyBro@users.noreply.github.com> Date: Fri, 4 Oct 2024 12:13:41 +0500 Subject: [PATCH] fix quote deletion, move some functions --- events/CommandHandler.js | 7 ++- events/MessageHandler.js | 103 ++++++++++++++++++---------------- events/{ready.js => Ready.js} | 0 3 files changed, 60 insertions(+), 50 deletions(-) rename events/{ready.js => Ready.js} (100%) diff --git a/events/CommandHandler.js b/events/CommandHandler.js index 1fee651e..91784015 100644 --- a/events/CommandHandler.js +++ b/events/CommandHandler.js @@ -15,6 +15,8 @@ class CommandHandler extends BaseEvent { * @param {import("discord.js").CommandInteraction} interaction */ async execute(client, interaction) { + if (interaction.isButton() && interaction.customId === "quote_delete" && interaction.message.deletable) return interaction.message.delete(); + const command = client.commands.get(interaction.commandName); if (!command) return interaction.reply({ content: "Command not found!", ephemeral: true }); @@ -27,7 +29,6 @@ class CommandHandler extends BaseEvent { interaction.data = data; - if (interaction.isButton() && interaction.customId === "quote_delete" && interaction.message.deletable) return interaction.message.delete(); if (interaction.isAutocomplete()) return await command.autocompleteRun(client, interaction); if (interaction.type !== InteractionType.ApplicationCommand || !interaction.isCommand()) return; @@ -47,13 +48,13 @@ class CommandHandler extends BaseEvent { await interaction.data.user.save(); - const achievementMessage = { + const message = { content: interaction.user.toString(), files: [{ name: "achievement_unlocked2.png", attachment: "./assets/img/achievements/achievement_unlocked2.png" }], }; try { - await interaction.user.send(achievementMessage); + await interaction.user.send(message); } catch (e) { client.logger.warn("Failed to send achievement message to user:", e); } diff --git a/events/MessageHandler.js b/events/MessageHandler.js index ad54ee93..0d4d0396 100644 --- a/events/MessageHandler.js +++ b/events/MessageHandler.js @@ -18,40 +18,37 @@ class MessageCreate extends BaseEvent { */ async execute(client, message) { if (message.author.bot) return; - if (message.content.match(new RegExp(`^<@!?${client.user.id}>( |)$`))) return message.replyT("misc:HELLO_SERVER", null, { mention: true }); - const data = await this.initializeMessageData(client, message); - message.data = data; - - if (message.guild) - await this.handleGuildMessage(client, message); - - return; - } - - async initializeMessageData(client, message) { const data = { user: await client.getUserData(message.author.id) }; if (message.guild) { if (!message.member) await message.guild.members.fetch(message.author.id); - + data.guild = await client.getGuildData(message.guildId); data.member = await client.getMemberData(message.author.id, message.guildId); } + message.data = data; - return data; - } - - async handleGuildMessage(client, message) { - await updateXp(message); - - if (message.content.match(/(https|http):\/\/(ptb\.|canary\.)?(discord.com)\/(channels)\/\d+\/\d+\/\d+/g)) await this.handleLinkQuote(client, message); - if (message.data.guild.plugins.automod.enabled && !message.data.guild.plugins.automod.ignored.includes(message.channelId)) await this.checkAutomod(message); - - await this.checkAfkStatus(client, message); - await this.checkMentionedUsersAfk(client, message); + if (message.content.match(new RegExp(`^<@!?${client.user.id}>( |)$`))) return message.replyT("misc:HELLO_SERVER", null, { mention: true, locale: message.data.guild.language }); + + if (message.guild) { + await this.updateXp(message); + + if (message.content.match(/(https|http):\/\/(ptb\.|canary\.)?(discord.com)\/(channels)\/\d+\/\d+\/\d+/g)) await this.handleLinkQuote(client, message); + if (message.data.guild.plugins.automod.enabled && !message.data.guild.plugins.automod.ignored.includes(message.channelId)) await this.checkAutomod(message); + + await this.checkAfkStatus(client, message); + await this.checkMentionedUsersAfk(client, message); + } + + return; } + /** + * + * @param {import("../base/Client")} client + * @param {import("discord.js").Message} message + */ async handleLinkQuote(client, message) { const link = message.content.match(/(https|http):\/\/(ptb\.|canary\.)?(discord.com)\/(channels)\/\d+\/\d+\/\d+/g)[0]; const ids = link.match(/\d+/g); @@ -94,6 +91,10 @@ class MessageCreate extends BaseEvent { } } + /** + * + * @param {import("discord.js").Message} message + */ async checkAutomod(message) { const inviteRegex = /(discord\.(gg|io|me|li)\/.+|discordapp\.com\/invite\/.+)/i; if (inviteRegex.test(message.content) && !message.channel.permissionsFor(message.member).has(PermissionsBitField.Flags.ManageMessages)) { @@ -102,6 +103,11 @@ class MessageCreate extends BaseEvent { } } + /** + * + * @param {import("../base/Client")} client + * @param {import("discord.js").Message} message + */ async checkAfkStatus(client, message) { if (message.data.user.afk) { message.data.user.afk = null; @@ -111,6 +117,11 @@ class MessageCreate extends BaseEvent { } } + /** + * + * @param {import("../base/Client")} client + * @param {import("discord.js").Message} message + */ async checkMentionedUsersAfk(client, message) { for (const user of message.mentions.users.values()) { const userData = await client.getUserData(user.id); @@ -118,37 +129,35 @@ class MessageCreate extends BaseEvent { if (userData.afk) message.replyT("general/afk:IS_AFK", { user: user.getUsername(), message: userData.afk }, { ephemeral: true }); } } -} -/** - * - * @param {import("../base/Client")} client - * @param {import("discord.js").Message} message - * @returns - */ -async function updateXp(message) { - const memberData = message.data.member; - const isInCooldown = xpCooldown[message.author.id]; + /** + * + * @param {import("discord.js").Message} message + */ + async updateXp(message) { + const memberData = message.data.member; + const isInCooldown = xpCooldown[message.author.id]; - if (isInCooldown && isInCooldown > Date.now()) return; + if (isInCooldown && isInCooldown > Date.now()) return; - const toWait = Date.now() + 60 * 1000; // 1 min - xpCooldown[message.author.id] = toWait; + const toWait = Date.now() + 60 * 1000; // 1 min + xpCooldown[message.author.id] = toWait; - const won = message.client.functions.randomNum(1, 2); - const newXp = memberData.exp + won; - const neededXp = 5 * memberData.level ** 2 + 80 * memberData.level + 100; + const won = message.client.functions.randomNum(1, 2); + const newXp = memberData.exp + won; + const neededXp = 5 * memberData.level ** 2 + 80 * memberData.level + 100; - if (newXp > neededXp) { - memberData.level += 1; - memberData.exp = 0; + if (newXp > neededXp) { + memberData.level += 1; + memberData.exp = 0; - message.replyT("misc:LEVEL_UP", { level: memberData.level }, { mention: false }); - } else { - memberData.exp = newXp; + message.replyT("misc:LEVEL_UP", { level: memberData.level }, { mention: false }); + } else { + memberData.exp = newXp; + } + + await memberData.save(); } - - await memberData.save(); } module.exports = MessageCreate; diff --git a/events/ready.js b/events/Ready.js similarity index 100% rename from events/ready.js rename to events/Ready.js