JaBa/events/MessageHandler.js

164 lines
5.1 KiB
JavaScript
Raw Permalink Normal View History

const { PermissionsBitField, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js");
2023-06-06 22:48:46 +05:00
const BaseEvent = require("../base/BaseEvent");
const xpCooldown = {};
2022-01-04 02:18:28 +05:00
class MessageCreate extends BaseEvent {
constructor() {
super({
name: "messageCreate",
once: false,
});
2022-01-04 02:18:28 +05:00
}
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../base/Client")} client
* @param {import("discord.js").Message} message
*/
async execute(client, message) {
2022-01-04 02:18:28 +05:00
if (message.author.bot) return;
2023-06-27 21:19:55 +05:00
2024-10-03 11:07:02 +05:00
const data = { user: await client.getUserData(message.author.id) };
2023-06-27 21:19:55 +05:00
2022-01-04 02:18:28 +05:00
if (message.guild) {
2024-02-09 23:26:57 +05:00
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);
2022-01-13 00:26:23 +05:00
}
message.data = data;
2022-01-04 02:18:28 +05:00
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);
2024-02-09 23:26:57 +05:00
2024-10-04 12:22:35 +05:00
if (message.content.match(/(https|http):\/\/(ptb\.|canary\.)?(discord.com)\/(channels)\/\d+\/\d+\/\d+/g)) return 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);
2024-10-03 11:07:02 +05:00
await this.checkAfkStatus(client, message);
await this.checkMentionedUsersAfk(client, message);
}
2024-10-03 11:07:02 +05:00
return;
2024-10-03 11:07:02 +05:00
}
/**
*
* @param {import("../base/Client")} client
* @param {import("discord.js").Message} message
*/
2024-10-03 11:07:02 +05:00
async handleLinkQuote(client, message) {
2024-10-03 21:15:41 +05:00
const link = message.content.match(/(https|http):\/\/(ptb\.|canary\.)?(discord.com)\/(channels)\/\d+\/\d+\/\d+/g)[0];
2024-10-03 11:07:02 +05:00
const ids = link.match(/\d+/g);
const channelId = ids[1];
const messageId = ids[2];
try {
const msg = await message.guild.channels.cache.get(channelId).messages.fetch(messageId);
2024-10-03 21:15:41 +05:00
const embed = client.embed({
author: {
name: message.translate("misc:QUOTE_TITLE", { user: msg.author.getUsername() }),
iconURL: "https://wynem.com/assets/images/icons/quote.webp",
},
thumbnail: msg.author.displayAvatarURL(),
footer: message.translate("misc:QUOTE_FOOTER", { user: message.author.getUsername() }),
timestamp: msg.createdTimestamp,
});
if (msg.content) embed.addFields([{ name: message.translate("misc:QUOTE_CONTENT"), value: msg.content }]);
if (msg.attachments.size > 0) {
if (msg.attachments.find(a => a.contentType.includes("image/")))
embed.setImage(msg.attachments.find(a => a.contentType.includes("image/")).url);
embed.addFields([
{
name: message.translate("misc:QUOTE_ATTACHED"),
value: msg.attachments.map(a => `[${a.name}](${a.url})`).join(", "),
},
]);
}
2024-10-03 11:07:02 +05:00
const row = new ActionRowBuilder().addComponents(
2024-10-03 21:15:41 +05:00
new ButtonBuilder().setLabel(message.translate("misc:QUOTE_JUMP")).setStyle(ButtonStyle.Link).setURL(msg.url),
2024-10-03 11:07:02 +05:00
new ButtonBuilder().setCustomId("quote_delete").setEmoji("1273665480451948544").setStyle(ButtonStyle.Danger),
);
await message.reply({ embeds: [embed], components: [row] });
} catch (error) {
2024-10-03 21:15:41 +05:00
console.log(error);
2022-01-13 00:26:23 +05:00
}
2024-10-03 11:07:02 +05:00
}
/**
*
* @param {import("discord.js").Message} message
*/
2024-10-03 11:07:02 +05:00
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)) {
await message.error("administration/automod:DELETED", null, { mention: true });
await message.delete();
}
}
/**
*
* @param {import("../base/Client")} client
* @param {import("discord.js").Message} message
*/
2024-10-03 11:07:02 +05:00
async checkAfkStatus(client, message) {
if (message.data.user.afk) {
message.data.user.afk = null;
await message.data.user.save();
message.replyT("general/afk:DELETED", { user: message.author.username }, { mention: true });
}
}
/**
*
* @param {import("../base/Client")} client
* @param {import("discord.js").Message} message
*/
2024-10-03 11:07:02 +05:00
async checkMentionedUsersAfk(client, message) {
for (const user of message.mentions.users.values()) {
const userData = await client.getUserData(user.id);
if (userData.afk) message.replyT("general/afk:IS_AFK", { user: user.getUsername(), message: userData.afk }, { ephemeral: true });
}
2022-01-04 02:18:28 +05:00
}
/**
*
* @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;
const toWait = Date.now() + 60 * 1000; // 1 min
xpCooldown[message.author.id] = toWait;
2022-01-04 02:18:28 +05:00
const won = message.client.functions.randomNum(1, 2);
const newXp = memberData.exp + won;
const neededXp = 5 * memberData.level ** 2 + 80 * memberData.level + 100;
2022-01-04 02:18:28 +05:00
if (newXp > neededXp) {
memberData.level += 1;
memberData.exp = 0;
2022-01-04 02:18:28 +05:00
message.replyT("misc:LEVEL_UP", { level: memberData.level }, { mention: false });
} else {
memberData.exp = newXp;
}
2024-10-03 11:07:02 +05:00
await memberData.save();
2024-10-03 11:07:02 +05:00
}
}
2023-07-05 00:58:06 +05:00
module.exports = MessageCreate;