JaBa/helpers/checkUnmutes.js

64 lines
2 KiB
JavaScript
Raw Normal View History

2021-12-10 21:39:54 +05:00
const Discord = require("discord.js");
/* THIS CHECK IF THERE IS A USER TO UNMUTE */
module.exports = {
/**
* Starts checking...
* @param {object} client The Discord Client instance
*/
2021-12-11 21:08:37 +05:00
async init(client) {
2021-12-26 19:29:37 +05:00
client.membersData
.find({ "mute.muted": true })
.then((members) => {
members.forEach((member) => {
client.databaseCache.mutedUsers.set(`${member.id}${member.guildID}`, member);
});
2021-12-10 21:39:54 +05:00
});
setInterval(async () => {
2022-01-03 23:20:33 +05:00
client.databaseCache.mutedUsers.filter((m) => m.mute.endDate <= Date.now()).forEach(async (memberData) => {
2021-12-10 21:39:54 +05:00
const guild = client.guilds.cache.get(memberData.guildID);
if (!guild) return;
const member = guild.members.cache.get(memberData.id) || await guild.members.fetch(memberData.id).catch(() => {
memberData.mute = {
muted: false,
endDate: null,
case: null
};
memberData.save();
2021-12-24 20:52:27 +05:00
client.logger.log("[unmute] " + memberData.id + " cannot be found.");
2021-12-16 23:42:58 +05:00
2021-12-10 21:39:54 +05:00
return null;
});
2021-12-26 19:29:37 +05:00
const guildData = await client.findOrCreateGuild({
id: guild.id
});
2021-12-10 21:39:54 +05:00
guild.data = guildData;
if (member) {
guild.channels.cache.forEach((channel) => {
const permOverwrites = channel.permissionOverwrites.cache.get(member.id);
2021-12-10 21:39:54 +05:00
if (permOverwrites) permOverwrites.delete();
});
};
const user = member ? member.user : await client.users.fetch(memberData.id);
const embed = new Discord.MessageEmbed()
.setDescription(guild.translate("moderation/unmute:SUCCESS_CASE", {
user: user.toString(),
usertag: user.tag,
count: memberData.mute.case
}))
.setColor("#f44271")
.setFooter({ text: guild.client.config.embed.footer });
2021-12-10 21:39:54 +05:00
const channel = guild.channels.cache.get(guildData.plugins.modlogs);
2022-01-03 23:20:33 +05:00
if (channel) channel.send({ embeds: [embed] });
2021-12-16 23:42:58 +05:00
2021-12-10 21:39:54 +05:00
memberData.mute = {
muted: false,
endDate: null,
case: null
};
client.databaseCache.mutedUsers.delete(`${memberData.id}${memberData.guildID}`);
await memberData.save();
});
}, 1000);
}
};