fix quote deletion, move some functions

This commit is contained in:
Jonny_Bro (Nikita) 2024-10-04 12:13:41 +05:00 committed by GitHub
parent 6967581788
commit 80c93c95e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 60 additions and 50 deletions

View file

@ -15,6 +15,8 @@ class CommandHandler extends BaseEvent {
* @param {import("discord.js").CommandInteraction} interaction * @param {import("discord.js").CommandInteraction} interaction
*/ */
async execute(client, 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); const command = client.commands.get(interaction.commandName);
if (!command) return interaction.reply({ content: "Command not found!", ephemeral: true }); if (!command) return interaction.reply({ content: "Command not found!", ephemeral: true });
@ -27,7 +29,6 @@ class CommandHandler extends BaseEvent {
interaction.data = data; 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.isAutocomplete()) return await command.autocompleteRun(client, interaction);
if (interaction.type !== InteractionType.ApplicationCommand || !interaction.isCommand()) return; if (interaction.type !== InteractionType.ApplicationCommand || !interaction.isCommand()) return;
@ -47,13 +48,13 @@ class CommandHandler extends BaseEvent {
await interaction.data.user.save(); await interaction.data.user.save();
const achievementMessage = { const message = {
content: interaction.user.toString(), content: interaction.user.toString(),
files: [{ name: "achievement_unlocked2.png", attachment: "./assets/img/achievements/achievement_unlocked2.png" }], files: [{ name: "achievement_unlocked2.png", attachment: "./assets/img/achievements/achievement_unlocked2.png" }],
}; };
try { try {
await interaction.user.send(achievementMessage); await interaction.user.send(message);
} catch (e) { } catch (e) {
client.logger.warn("Failed to send achievement message to user:", e); client.logger.warn("Failed to send achievement message to user:", e);
} }

View file

@ -18,40 +18,37 @@ class MessageCreate extends BaseEvent {
*/ */
async execute(client, message) { async execute(client, message) {
if (message.author.bot) return; 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) }; const data = { user: await client.getUserData(message.author.id) };
if (message.guild) { if (message.guild) {
if (!message.member) await message.guild.members.fetch(message.author.id); if (!message.member) await message.guild.members.fetch(message.author.id);
data.guild = await client.getGuildData(message.guildId); data.guild = await client.getGuildData(message.guildId);
data.member = await client.getMemberData(message.author.id, message.guildId); data.member = await client.getMemberData(message.author.id, message.guildId);
} }
message.data = data;
return data; 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) {
async handleGuildMessage(client, message) { await this.updateXp(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.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);
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.checkAfkStatus(client, message); await this.checkMentionedUsersAfk(client, message);
await this.checkMentionedUsersAfk(client, message); }
return;
} }
/**
*
* @param {import("../base/Client")} client
* @param {import("discord.js").Message} message
*/
async handleLinkQuote(client, message) { async handleLinkQuote(client, message) {
const link = message.content.match(/(https|http):\/\/(ptb\.|canary\.)?(discord.com)\/(channels)\/\d+\/\d+\/\d+/g)[0]; const link = message.content.match(/(https|http):\/\/(ptb\.|canary\.)?(discord.com)\/(channels)\/\d+\/\d+\/\d+/g)[0];
const ids = link.match(/\d+/g); const ids = link.match(/\d+/g);
@ -94,6 +91,10 @@ class MessageCreate extends BaseEvent {
} }
} }
/**
*
* @param {import("discord.js").Message} message
*/
async checkAutomod(message) { async checkAutomod(message) {
const inviteRegex = /(discord\.(gg|io|me|li)\/.+|discordapp\.com\/invite\/.+)/i; 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)) { 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) { async checkAfkStatus(client, message) {
if (message.data.user.afk) { if (message.data.user.afk) {
message.data.user.afk = null; 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) { async checkMentionedUsersAfk(client, message) {
for (const user of message.mentions.users.values()) { for (const user of message.mentions.users.values()) {
const userData = await client.getUserData(user.id); 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 }); 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
* @param {import("discord.js").Message} message */
* @returns async updateXp(message) {
*/ const memberData = message.data.member;
async function updateXp(message) { const isInCooldown = xpCooldown[message.author.id];
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 const toWait = Date.now() + 60 * 1000; // 1 min
xpCooldown[message.author.id] = toWait; xpCooldown[message.author.id] = toWait;
const won = message.client.functions.randomNum(1, 2); const won = message.client.functions.randomNum(1, 2);
const newXp = memberData.exp + won; const newXp = memberData.exp + won;
const neededXp = 5 * memberData.level ** 2 + 80 * memberData.level + 100; const neededXp = 5 * memberData.level ** 2 + 80 * memberData.level + 100;
if (newXp > neededXp) { if (newXp > neededXp) {
memberData.level += 1; memberData.level += 1;
memberData.exp = 0; memberData.exp = 0;
message.replyT("misc:LEVEL_UP", { level: memberData.level }, { mention: false }); message.replyT("misc:LEVEL_UP", { level: memberData.level }, { mention: false });
} else { } else {
memberData.exp = newXp; memberData.exp = newXp;
}
await memberData.save();
} }
await memberData.save();
} }
module.exports = MessageCreate; module.exports = MessageCreate;