'use strict'; const commando = require("@iceprod/discord.js-commando"); const { trySend, findMemberRegEx, cleanMentionID, findChannelRegEx, findRoleRegEx, defaultImageEmbed } = require("../../resources/functions"); const { database } = require("../../database/mongo"); const col = database.collection("Guild"); const schedule = database.collection("Schedule"); const { scheduler } = require("../../resources/scheduler"); module.exports = class mute extends commando.Command { constructor(client) { super(client, { name: "mute", memberName: "mute", group: "moderation", description: "Mute.", guildOnly: true, userPermissions:['MANAGE_ROLES'] }); } /** * @param {commando.CommandoMessage} msg * @param {*} arg * @returns */ async run(msg, arg) { const doc = await col.findOne({document: msg.guild.id}), modDoc = doc?.["moderation"], muteSettingsDoc = modDoc?.["settings"]?.mute, defaultDurationDoc = muteSettingsDoc?.defaultDuration, infractionsDoc = modDoc?.infractions, args = arg.trim().split(/(? 0 && argument !== "--") { reason = argument.trim(); } else { if (settingLogChannel && !settingLogChannelHasSet && argument.length > 0 && argument !== "--" && setArg !== "log") { settingLogChannelHasSet = true; const key = cleanMentionID(argument); let logChannel; if (/^\d{17,19}$/.test(key)) { logChannel = msg.guild.channels.cache.get(argument); } else { const found = findChannelRegEx(msg, key); logChannel = found[0]; } if (/^none$/.test(key)) { logChannel = undefined; } if (logChannel || /^none$/.test(key)) { theSettingUp.logChannel = logChannel?.id; } else { resultMsg += `No channel found for: **${argument}**\n`; } } if (settingRole && !settingRoleHasSet && argument.length > 0 && argument !== "--" && setArg !== "role") { settingRoleHasSet = true; const key = cleanMentionID(argument); let role; if (/^\d{17,19}$/.test(key)) { role = msg.guild.roles.cache.get(argument); } else { const found = findRoleRegEx(msg, key); role = found[0]?.id; } if (/^none$/.test(key)) { role = undefined; } if (role || /^none$/.test(key)) { theSettingUp.role = role; } else { resultMsg += `No role found for: **${argument}**\n`; } } } } } const roleConfCheck = msg.guild.roles.cache.get(muteSettingsDoc?.role); if (!roleConfCheck && !settingUp) { return trySend(this.clientPermissions, msg, `No mute role configured! Run \`${this.client.commandPrefix}mute --settings <--role --> [--duration -- | --log --]\` to set it up.`); } if (duration.year > 275500) { duration.year = 275500; } let untilDate = new Date(String(duration.year), String(duration.month), String(duration.date), String(duration.hour), String(duration.minute), String(duration.second)); if (untilDate.toUTCString() === invokedAt.toUTCString() && !settingDuration) { if (defaultDurationDoc?.date?.valueOf() > 0) { untilDate = new Date(invokedAt.valueOf() + defaultDurationDoc.date.valueOf() - 1000); } else { untilDate = "Indefinite"; } } if (untilDate !== "Indefinite") { timeForMessage = []; const elapsedTime = new Date(untilDate.valueOf() - invokedAt.valueOf() + 1000), elapsed = [ elapsedTime.getFullYear() - 1970, elapsedTime.getMonth(), elapsedTime.getDate() - 1, elapsedTime.getHours(), elapsedTime.getMinutes(), elapsedTime.getSeconds() ], elapsedName = [ "year", "month", "day", "hour", "minute", "second" ]; for (let index = 0; index < elapsed.length; index++) { if (elapsed[index] > 0) { let mes = `${elapsed[index]} ${elapsedName[index]}`; if (elapsed[index] > 1) { mes += "s"; } else {} timeForMessage.push(mes); } else {} } if (timeForMessage.length > 1) { timeForMessage[timeForMessage.length - 2] += " and"; } if (settingDuration && !settingDurationHasSet && timeForMessage.length > 0) { settingDurationHasSet = true; theSettingUp.defaultDuration.date = elapsedTime, theSettingUp.defaultDuration.string = timeForMessage.join(" "); } } if (settingUp) { if(settingRoleHasSet) { await col.updateOne({document:msg.guild.id}, {$set:{"moderation.settings.mute.role":theSettingUp.role}}, {upsert:true}).catch(e => {return trySend(this.client, msg, "```js\n"+e.stack+"```")}); } if (durationHasSet) { await col.updateOne({document:msg.guild.id}, {$set:{"moderation.settings.mute.defaultDuration":theSettingUp.defaultDuration}}, {upsert:true}).catch(e => {return trySend(this.client, msg, "```js\n"+e.stack+"```")}); } if (settingLogChannelHasSet) { await col.updateOne({document:msg.guild.id}, {$set:{"moderation.settings.mute.logChannel":theSettingUp.logChannel}}, {upsert:true}).catch(e => {return trySend(this.client, msg, "```js\n"+e.stack+"```")}); } const doc = await col.findOne({document: msg.guild.id}), modDoc = doc?.["moderation"], muteSettingsDoc = modDoc?.["settings"]?.mute, defaultDurationDoc = muteSettingsDoc?.defaultDuration, logChannelDoc = muteSettingsDoc?.logChannel, roleDoc = muteSettingsDoc?.role; let settings = await defaultImageEmbed(this.client, msg, msg.member); settings .setTitle("Mute Configuration") .addField("Role", roleDoc ? "<@&"+roleDoc+">" : "Not set") .addField("Duration", defaultDurationDoc?.string ?? "Not set") .addField("Log", logChannelDoc ? "<#"+logChannelDoc+">" : "Not set"); return trySend(this.client, msg, {content:resultMsg, embed:settings}); } for (const usermention of mentions) { if (usermention.length > 0) { let found = [], nameid = cleanMentionID(usermention); if (/^\d{17,19}$/.test(nameid)) { const findmem = msg.guild.member(nameid); if (findmem) { found.push(findmem.user); } else { await this.client.users.fetch(nameid).then(fetchUser => found.push(fetchUser)).catch(() => {}); } } else { found = findMemberRegEx(msg, nameid).map(r => r.user); } if (found.length > 0 && found[0] !== null) { const foundDupli = targetUser.findIndex(r => r === found[0]); if (foundDupli !== -1) { resultMsg += `**[WARNING]** Duplicate for user **${targetUser[foundDupli].tag}** with keyword: **${usermention.trim()}**\n`; } else { targetUser.push(found[0]); if (found.length > 1) { resultMsg += `**[WARNING]** Multiple users found for: **${usermention.trim()}**\n`; } } } else { resultMsg += `Can't find user: **${usermention.trim()}**\n`; } } else { if (!settingUp) { return trySend(this.client, msg, "Who are you wanna mute? Provide as first argument `<[RegExp | user_[mention | ID]]>`"); } else { } } } let infractionToDoc; if (targetUser.length > 0) { let targetMember = [], notInServer = []; for (const user of targetUser) { const member = msg.guild.member(user); if (member) { const pushIt = { name:member.user.tag, id:member.id, roles:member.roles.cache.map(r => r.id) } targetMember.push(pushIt); } else { const pushIt = { name: user.tag, id:user.id } notInServer.push(pushIt); } } const infractionCase = infractionsDoc?.map(r => r.infraction)?.length; infractionToDoc = { infraction: infractionCase ? infractionCase + 1 : 1, by: targetUser, moderator: `**${msg.author.tag}** <@${msg.author.id}> (${msg.author.id})`, punishment: "mute", at: invokedAt, for: timeForMessage, until: untilDate, reason: reason, scene: msg.url, members: targetMember, users: notInServer } await col.updateOne({document: msg.guild.id}, { $push:{"moderation.infractions": infractionToDoc}}, {upsert:true}); const newUnmuteSchedule = { name: "unmute", path: "./scheduler/unmute.js", worker: { argv: [msg, infractionToDoc.infraction] }, date: untilDate } } resultMsg += `Result:\`\`\`js\nUsers: ${targetUser.map(r => r?.tag).join(", ")}\nReason: ${reason}\nAt: ${invokedAt.toUTCString()}\nFor: ${timeForMessage.join(" ")}\nUntil: ${typeof untilDate !== "string" ? untilDate.toUTCString() : untilDate}\`\`\``; trySend(this.client, msg, {content:resultMsg+"```js\n" + JSON.stringify(infractionToDoc, null, 2) + "```",split:{maxLength:2000,append:",```",prepend:"```js\n",char:","}}); return } }; /* if (config.mute.role.length === 0) { return msg.channel.send(`Mute role isn't set! Run \`${this.client.commandPrefix}mute --role \`. If you insist i will just give them admin perms <:purifyLife:774102054046007298>`) } if (setArgs) { for(let set of setArgs) { set = set.toLowerCase(); switch(set) { case startsWith('role'): { let role = set.slice('role'.length).trim(); if (role.startsWith('<&')) { role = role.slice(2,-1); } //const foundRole = } } } }*/ //scheduler.add() /*const yearDate = dateDur.getFullYear(); const monthDate = dateDur.getMonth(); const dayDate = dateDur.getDay(); const hourDate = dateDur.getHours(); const minuteDate = dateDur.getMinutes(); const secondDate = dateDur.getSeconds();*/