JaBa/events/MessageHandler.js

108 lines
3.2 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 = {};
data.config = client.config;
2022-01-04 02:18:28 +05:00
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
}
2022-01-04 02:18:28 +05:00
if (message.content.match(new RegExp(`^<@!?${client.user.id}>( |)$`))) return message.replyT("misc:HELLO_SERVER", { username: message.author.username });
2022-01-04 02:18:28 +05:00
if (message.guild) {
const memberData = await client.findOrCreateMember({
id: message.author.id,
guildID: message.guild.id
});
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();
message.author.send("```" + message.content + "```");
return message.error("administration/automod:DELETED", {
username: message.author.tag
});
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-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
async function updateXp(client, msg, data) {
const points = parseInt(data.memberData.exp);
const level = parseInt(data.memberData.level);
const isInCooldown = xpCooldown[msg.author.id];
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() + 60000; // 1 min
2022-01-04 02:18:28 +05:00
xpCooldown[msg.author.id] = toWait;
const won = client.functions.randomNum(1, 4);
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 = parseInt(newXp, 10);
await data.memberData.save();
}
module.exports = MessageCreate;