JaBa/events/MessageHandler.js

112 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-08-04 21:02:45 +05:00
const { PermissionsBitField } = require("discord.js");
const BaseEvent = require("../base/BaseEvent"),
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
}
/**
*
* @param {import("../base/JaBa")} 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;
if (message.content.includes("no bitches")) message.reply({ files: [{ name: "b.png", attachment: "./assets/img/b.png" }] });
2022-07-03 21:53:05 +05:00
if (message.content.includes("bitches") && !message.content.includes("no bitches")) message.reply({ files: [{ name: "nob.png", attachment: "./assets/img/nob.png" }] });
2022-01-04 02:18:28 +05:00
const data = {};
if (message.author.bot) return;
if (message.guild && !message.member) await message.guild.members.fetch(message.author.id);
if (message.guild) {
const guild = await client.findOrCreateGuild({
id: message.guild.id
});
2022-08-08 18:19:56 +05:00
message.guild.data = data.guildData = guild;
2022-01-13 00:26:23 +05:00
}
if (message.content.match(new RegExp(`^<@!?${client.user.id}>( |)$`))) return message.replyT("misc:HELLO_SERVER", null, { mention: true });
2022-01-04 02:18:28 +05:00
if (message.guild) {
const memberData = await client.findOrCreateMember({
id: message.author.id,
guildId: message.guild.id
2022-01-04 02:18:28 +05:00
});
data.memberData = memberData;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
const userData = await client.findOrCreateUser({
id: message.author.id
});
data.userData = userData;
if (message.guild) {
await updateXp(client, message, data);
2022-08-08 18:19:56 +05:00
if (data.guildData.plugins.automod.enabled && !data.guildData.plugins.automod.ignored.includes(message.channel.id)) {
2022-01-04 02:18:28 +05:00
if (/(discord\.(gg|io|me|li)\/.+|discordapp\.com\/invite\/.+)/i.test(message.content)) {
if (!message.channel.permissionsFor(message.member).has(PermissionsBitField.Flags.ManageMessages)) {
2022-01-04 02:18:28 +05:00
message.delete();
2022-08-09 23:48:33 +05:00
return message.error("administration/automod:DELETED");
2022-01-13 00:26:23 +05:00
}
}
}
2022-01-04 02:18:28 +05:00
const afkReason = data.userData.afk;
if (afkReason) {
data.userData.afk = null;
await data.userData.save();
message.replyT("general/afk:DELETED", {
2022-01-04 02:18:28 +05:00
username: 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
message.mentions.users.forEach(async (u) => {
const userData = await client.findOrCreateUser({
id: u.id
});
if (userData.afk) message.error("general/afk:IS_AFK", { user: u.tag, reason: userData.afk });
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
/**
*
* @param {import("../base/JaBa")} client
* @param {import("discord.js").Message} msg
* @param {*} data
* @returns
*/
2022-01-04 02:18:28 +05:00
async function updateXp(client, msg, data) {
const points = parseInt(data.memberData.exp),
level = parseInt(data.memberData.level),
isInCooldown = xpCooldown[msg.author.id];
2022-01-04 02:18:28 +05:00
if (isInCooldown) {
if (isInCooldown > Date.now()) return;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
const toWait = Date.now() + (60 * 1000); // 1 min
2022-01-04 02:18:28 +05:00
xpCooldown[msg.author.id] = toWait;
const won = 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) {
data.memberData.level = parseInt(level + 1, 10);
data.memberData.exp = 0;
msg.replyT("misc:LEVEL_UP", {
level: data.memberData.level
}, { mention: false });
} else data.memberData.exp = parseInt(newXp, 10);
2022-01-04 02:18:28 +05:00
await data.memberData.save();
}
module.exports = MessageCreate;