JaBa/events/message.js

248 lines
8.4 KiB
JavaScript
Raw Normal View History

2021-12-10 21:39:54 +05:00
const xpCooldown = {},
cmdCooldown = {};
module.exports = class {
2021-12-26 19:29:37 +05:00
constructor(client) {
2021-12-10 21:39:54 +05:00
this.client = client;
}
2021-12-26 19:29:37 +05:00
async run(message) {
2021-12-10 21:39:54 +05:00
const data = {};
2021-12-16 23:42:58 +05:00
// If the message author is a bot
2021-12-10 21:39:54 +05:00
if (message.author.bot) return;
// If the member on a guild is invisible or not cached, fetch them.
if (message.guild && !message.member) await message.guild.members.fetch(message.author.id);
const client = this.client;
data.config = client.config;
if (message.guild) {
// Gets guild data
2021-12-26 19:29:37 +05:00
const guild = await client.findOrCreateGuild({
id: message.guild.id
});
2021-12-10 21:39:54 +05:00
message.guild.data = data.guild = guild;
};
// Check if the bot was mentionned
if (message.content.match(new RegExp(`^<@!?${client.user.id}>( |)$`))) {
2021-12-16 23:42:58 +05:00
if (message.guild) return message.sendT("misc:HELLO_SERVER", { username: message.author.username, prefix: data.guild.prefix });
else return message.sendT("misc:HELLO_DM", { username: message.author.username });
2021-12-10 21:39:54 +05:00
};
2021-12-16 23:42:58 +05:00
if (message.content.includes("@someone") && message.guild) return client.commands.get("someone").run(message, null, data);
2021-12-10 21:39:54 +05:00
if (message.guild) {
// Gets the data of the member
2021-12-26 19:29:37 +05:00
const memberData = await client.findOrCreateMember({
id: message.author.id,
guildID: message.guild.id
});
2021-12-10 21:39:54 +05:00
data.memberData = memberData;
};
2021-12-26 19:29:37 +05:00
const userData = await client.findOrCreateUser({
id: message.author.id
});
2021-12-10 21:39:54 +05:00
data.userData = userData;
if (message.guild) {
await updateXp(message, data);
if (!message.channel.permissionsFor(message.member).has("MANAGE_MESSAGES") && !message.editedAt) {
const channelSlowmode = data.guild.slowmode.channels.find((ch) => ch.id === message.channel.id);
if (channelSlowmode) {
const uSlowmode = data.guild.slowmode.users.find((d) => d.id === (message.author.id + message.channel.id));
if (uSlowmode) {
if (uSlowmode.time > Date.now()) {
message.delete();
const delay = message.convertTime(uSlowmode.time, "to", true);
2021-12-16 23:42:58 +05:00
2021-12-26 19:29:37 +05:00
return message.author.send(message.translate("administration/slowmode:PLEASE_WAIT", {
time: delay,
channel: message.channel.toString()
}));
2021-12-10 21:39:54 +05:00
} else {
uSlowmode.time = channelSlowmode.time + Date.now();
};
} else {
data.guild.slowmode.users.push({
id: message.author.id + message.channel.id,
time: channelSlowmode.time + Date.now()
});
};
data.guild.markModified("slowmode.users");
await data.guild.save();
};
};
if (data.guild.plugins.automod.enabled && !data.guild.plugins.automod.ignored.includes(message.channel.id)) {
if (/(discord\.(gg|io|me|li)\/.+|discordapp\.com\/invite\/.+)/i.test(message.content)) {
if (!message.channel.permissionsFor(message.member).has("MANAGE_MESSAGES")) {
message.delete();
message.author.send("```" + message.content + "```");
2021-12-26 19:29:37 +05:00
return message.error("administration/automod:DELETED", {
username: message.author.tag
});
2021-12-10 21:39:54 +05:00
};
};
};
const afkReason = data.userData.afk;
if (afkReason) {
data.userData.afk = null;
await data.userData.save();
2021-12-26 19:29:37 +05:00
message.sendT("general/setafk:DELETED", {
username: message.author.username
});
2021-12-10 21:39:54 +05:00
};
message.mentions.users.forEach(async (u) => {
2021-12-26 19:29:37 +05:00
const userData = await client.findOrCreateUser({
id: u.id
});
if (userData.afk) message.error("general/setafk:IS_AFK", {
user: u.tag,
reason: userData.afk
});
2021-12-10 21:39:54 +05:00
});
};
// Gets the prefix
const prefix = client.functions.getPrefix(message, data);
if (!prefix) return;
const args = message.content.slice((typeof prefix === "string" ? prefix.length : 0)).trim().split(/ +/g);
const command = args.shift().toLowerCase();
const cmd = client.commands.get(command) || client.commands.get(client.aliases.get(command));
const customCommand = message.guild ? data.guild.customCommands.find((c) => c.name === command) : null;
const customCommandAnswer = customCommand ? customCommand.answer : "";
if (!cmd && !customCommandAnswer && message.guild) return;
2021-12-11 21:08:37 +05:00
else if (!cmd && !customCommandAnswer && !message.guild) return message.sendT("misc:HELLO_DM", { username: message.author.username });
2021-12-10 21:39:54 +05:00
if (message.guild && data.guild.ignoredChannels.includes(message.channel.id) && !message.member.hasPermission("MANAGE_MESSAGES")) {
message.delete();
2021-12-26 19:29:37 +05:00
return message.author.send(message.translate("misc:RESTRICTED_CHANNEL", {
channel: message.channel.toString()
}));
2021-12-10 21:39:54 +05:00
};
if (customCommandAnswer) return message.channel.send(customCommandAnswer);
if (cmd.conf.guildOnly && !message.guild) return message.error("misc:GUILD_ONLY");
if (message.guild) {
let neededPermissions = [];
2021-12-11 21:08:37 +05:00
if (!cmd.conf.botPermissions.includes("EMBED_LINKS")) cmd.conf.botPermissions.push("EMBED_LINKS");
2021-12-16 23:42:58 +05:00
2021-12-10 21:39:54 +05:00
cmd.conf.botPermissions.forEach((perm) => {
if (!message.channel.permissionsFor(message.guild.me).has(perm)) {
neededPermissions.push(perm);
};
});
2021-12-16 23:42:58 +05:00
2021-12-26 19:29:37 +05:00
if (neededPermissions.length > 0) return message.error("misc:MISSING_BOT_PERMS", {
list: neededPermissions.map((p) => `\`${p}\``).join(", ")
});
2021-12-11 21:08:37 +05:00
2021-12-10 21:39:54 +05:00
neededPermissions = [];
cmd.conf.memberPermissions.forEach((perm) => {
if (!message.channel.permissionsFor(message.member).has(perm)) {
neededPermissions.push(perm);
};
});
2021-12-16 23:42:58 +05:00
2021-12-11 21:08:37 +05:00
if (neededPermissions.length > 0) return message.error("misc:MISSING_MEMBER_PERMS", { list: neededPermissions.map((p) => `\`${p}\``).join(", ") });
2021-12-10 21:39:54 +05:00
if (!message.channel.permissionsFor(message.member).has("MENTION_EVERYONE") && (message.content.includes("@everyone") || message.content.includes("@here"))) return message.error("misc:EVERYONE_MENTION");
if (!message.channel.nsfw && cmd.conf.nsfw) return message.error("misc:NSFW_COMMAND");
};
if (!cmd.conf.enabled) return message.error("misc:COMMAND_DISABLED");
2021-12-13 18:27:54 +05:00
if (cmd.conf.ownerOnly && message.author.id !== client.config.owner.id) return message.error("misc:OWNER_ONLY");
2021-12-10 21:39:54 +05:00
let uCooldown = cmdCooldown[message.author.id];
if (!uCooldown) {
cmdCooldown[message.author.id] = {};
uCooldown = cmdCooldown[message.author.id];
};
2021-12-16 23:42:58 +05:00
2021-12-10 21:39:54 +05:00
const time = uCooldown[cmd.help.name] || 0;
2021-12-26 19:29:37 +05:00
if (time && (time > Date.now())) return message.error("misc:COOLDOWNED", { seconds: Math.ceil((time - Date.now()) / 1000) });
2021-12-10 21:39:54 +05:00
cmdCooldown[message.author.id][cmd.help.name] = Date.now() + cmd.conf.cooldown;
2021-12-13 18:27:54 +05:00
client.logger.log(`${message.author.username} (${message.author.id}) ran command ${cmd.help.name} ${message.guild ? `on ${message.guild.name}` : "in DM"}`, "cmd");
2021-12-10 21:39:54 +05:00
const log = new this.client.logs({
commandName: cmd.help.name,
2021-12-26 19:29:37 +05:00
author: {
username: message.author.username,
discriminator: message.author.discriminator,
id: message.author.id
},
guild: {
name: message.guild ? message.guild.name : "dm",
id: message.guild ? message.guild.id : "dm"
}
2021-12-10 21:39:54 +05:00
});
log.save();
if (!data.userData.achievements.firstCommand.achieved) {
data.userData.achievements.firstCommand.progress.now = 1;
data.userData.achievements.firstCommand.achieved = true;
data.userData.markModified("achievements.firstCommand");
await data.userData.save();
2021-12-26 19:29:37 +05:00
await message.channel.send({
files: [{
name: "unlocked.png",
attachment: "./assets/img/achievements/achievement_unlocked2.png"
}]
});
2021-12-10 21:39:54 +05:00
};
try {
cmd.run(message, args, data);
if (cmd.help.category === "Moderation" && data.guild.autoDeleteModCommands) message.delete();
2021-12-26 19:29:37 +05:00
} catch (e) {
2021-12-10 21:39:54 +05:00
console.error(e);
return message.error("misc:ERR_OCCURRED");
};
}
};
/**
* updateXp
* This function update userdata by adding xp
2021-12-26 19:29:37 +05:00
*/
2021-12-10 21:39:54 +05:00
async function updateXp(msg, data) {
// Gets the user informations
const points = parseInt(data.memberData.exp);
const level = parseInt(data.memberData.level);
// if the member is already in the cooldown db
const isInCooldown = xpCooldown[msg.author.id];
if (isInCooldown) {
if (isInCooldown > Date.now()) return;
};
2021-12-10 21:39:54 +05:00
// Records in the database the time when the member will be able to win xp again (1min)
const toWait = Date.now() + 60000;
xpCooldown[msg.author.id] = toWait;
// Gets a random number between 10 and 5
2021-12-29 22:48:05 +05:00
const won = this.client.functions.randomNum(5, 10);
2021-12-10 21:39:54 +05:00
const newXp = parseInt(points + won, 10);
2021-12-10 21:39:54 +05:00
// calculation how many xp it takes for the next new one
const neededXp = 5 * (level * level) + 80 * level + 100;
// check if the member up to the next level
if (newXp > neededXp) data.memberData.level = parseInt(level + 1, 10);
// Update user data
data.memberData.exp = parseInt(newXp, 10);
await data.memberData.save();
};