JaBa/events/MessageHandler.js

152 lines
4.6 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-02-15 01:26:29 +05:00
if (message.guild && message.guild.id === "568120814776614924") return;
2024-02-09 23:26:57 +05:00
const data = [];
2023-06-27 21:19:55 +05:00
2022-01-04 02:18:28 +05:00
if (message.author.bot) return;
2023-06-29 14:00:08 +05:00
if (message.content.match(new RegExp(`^<@!?${client.user.id}>( |)$`))) return message.replyT("misc:HELLO_SERVER", null, { mention: true });
2023-06-27 21:19:55 +05:00
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);
2023-06-29 14:00:08 +05:00
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
}
2022-01-04 02:18:28 +05:00
2024-02-09 23:26:57 +05:00
message.data = data;
2022-01-04 02:18:28 +05:00
if (message.guild) {
2024-02-09 23:26:57 +05:00
await updateXp(message);
2022-01-04 02:18:28 +05:00
2023-10-21 21:15:25 +05:00
if (message.content.match(/(https|http):\/\/(ptb\.|canary\.)?(discord.com)\/(channels)\/\d+\/\d+\/\d+/g)) {
2023-06-08 12:53:31 +05:00
const link = message.content.match(/(https|http):\/\/(ptb\.|canary\.)?(discord.com)\/(channels)\/\d+\/\d+\/\d+/g)[0],
2023-06-08 12:31:06 +05:00
ids = link.match(/\d+/g),
channelId = ids[1],
messageId = ids[2];
2024-01-13 23:17:21 +05:00
const msg = await message.guild.channels.cache.get(channelId).messages.fetch(messageId);
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: {
text: message.translate("misc:QUOTE_FOOTER", { user: message.author.getUsername() }),
},
timestamp: msg.createdTimestamp,
});
2023-10-21 21:15:25 +05:00
if (msg.content !== "") embed.addFields([
{
name: message.translate("misc:QUOTE_CONTENT"),
value: msg.content,
},
]);
if (msg.attachments.size > 0) {
2023-10-22 14:24:53 +05:00
if (msg.attachments.find(a => a.contentType.includes("image/"))) embed.setImage(msg.attachments.find(a => a.contentType.includes("image/")).url);
2023-10-21 21:15:25 +05:00
embed.addFields([
{
name: message.translate("misc:QUOTE_ATTACHED"),
value: msg.attachments.map(a => { return `[${a.name}](${a.url})`; }).join(", "),
},
]);
}
2023-07-05 00:58:06 +05:00
const row = new ActionRowBuilder().addComponents(
new ButtonBuilder().setLabel(message.translate("misc:QUOTE_JUMP")).setStyle(ButtonStyle.Link).setURL(msg.url),
new ButtonBuilder().setCustomId("quote_delete").setEmoji("1102200816582000750").setStyle(ButtonStyle.Danger),
);
message.reply({
embeds: [embed],
components: [row],
});
}
2024-02-09 23:26:57 +05:00
if (message.data.guild.plugins.automod.enabled && !message.data.guild.plugins.automod.ignored.includes(message.channelId))
if (/(discord\.(gg|io|me|li)\/.+|discordapp\.com\/invite\/.+)/i.test(message.content))
if (!message.channel.permissionsFor(message.member).has(PermissionsBitField.Flags.ManageMessages)) {
2023-07-05 00:58:06 +05:00
await message.error("administration/automod:DELETED", null, { mention: true });
2023-06-17 00:44:47 +05:00
message.delete();
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
2024-02-09 23:26:57 +05:00
if (message.data.user.afk) {
message.data.user.afk = null;
2024-02-09 23:26:57 +05:00
await message.data.user.save();
2023-07-03 19:30:47 +05:00
message.replyT("general/afk:DELETED", {
user: message.author.username,
2022-10-09 14:26:22 +05:00
}, { mention: true });
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
2023-07-02 21:02:55 +05:00
message.mentions.users.forEach(async u => {
const userData = await client.getUserData(u.id);
2023-07-02 21:02:55 +05:00
if (userData.afk) message.replyT("general/afk:IS_AFK", { user: u.getUsername(), reason: userData.afk }, { ephemeral: true });
2022-01-04 02:18:28 +05:00
});
2022-01-13 00:26:23 +05:00
}
return;
2022-01-04 02:18:28 +05:00
}
}
2022-01-04 02:18:28 +05:00
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../base/Client")} client
2024-02-09 23:26:57 +05:00
* @param {import("discord.js").Message} message
* @returns
*/
2024-02-09 23:26:57 +05:00
async function updateXp(message) {
const memberData = message.data.member,
points = parseInt(memberData.exp),
2023-06-27 21:19:55 +05:00
level = parseInt(memberData.level),
2024-02-09 23:26:57 +05:00
isInCooldown = xpCooldown[message.author.id];
2023-07-05 00:58:06 +05:00
if (isInCooldown) if (isInCooldown > Date.now()) return;
2022-01-04 02:18:28 +05:00
2023-07-05 00:58:06 +05:00
const toWait = Date.now() + 60 * 1000; // 1 min
2024-02-09 23:26:57 +05:00
xpCooldown[message.author.id] = toWait;
2022-01-04 02:18:28 +05:00
2024-02-09 23:26:57 +05:00
const won = message.client.functions.randomNum(1, 2);
2022-01-04 02:18:28 +05:00
const newXp = parseInt(points + won, 10);
const neededXp = 5 * (level * level) + 80 * level + 100;
if (newXp > neededXp) {
2023-06-27 21:19:55 +05:00
memberData.level = parseInt(level + 1, 10);
memberData.exp = 0;
2024-02-09 23:26:57 +05:00
message.replyT("misc:LEVEL_UP", {
2023-06-27 21:19:55 +05:00
level: memberData.level,
}, { mention: false });
2023-06-27 21:19:55 +05:00
} else memberData.exp = parseInt(newXp, 10);
2022-01-04 02:18:28 +05:00
2023-06-27 21:19:55 +05:00
await memberData.save();
}
2023-07-05 00:58:06 +05:00
module.exports = MessageCreate;