From ceef16e01f869cb366d361437e2ae724d5f816c4 Mon Sep 17 00:00:00 2001 From: JandereDev Date: Wed, 13 Oct 2021 21:38:22 +0200 Subject: [PATCH] warns can now be deleted ig --- src/bot/commands/warns.ts | 127 ++++++++++++++++++++++---------------- 1 file changed, 75 insertions(+), 52 deletions(-) diff --git a/src/bot/commands/warns.ts b/src/bot/commands/warns.ts index 421d1b9..a082557 100644 --- a/src/bot/commands/warns.ts +++ b/src/bot/commands/warns.ts @@ -16,7 +16,7 @@ export default { name: 'warns', aliases: [ 'warnings', 'infractions', 'infraction' ], description: 'Show all user infractions', - syntax: '/warns; /warns @username; /warns @username export-csv', + syntax: '/warns; /warns @username ["export-csv"]; /warns rm [ID]', run: async (message: Message, args: string[]) => { if (!await isModerator(message.member!)) return message.reply(NO_MANAGER_MSG); @@ -42,67 +42,90 @@ export default { message.reply(msg.substr(0, 1999)); } else { - let user = await parseUser(args[0]); - if (!user) return message.reply('Unknown user'); + switch(args[0]?.toLowerCase()) { + case 'delete': + case 'remove': + case 'rm': + case 'del': + let id = args[1]; + if (!id) return message.reply('No infraction ID provided.'); + let inf: Infraction|null = await client.db.get('infractions').findOneAndDelete({ + _id: { $eq: id.toUpperCase() }, + server: message.channel?.server_id + }); - let infs = userInfractions.get(user._id); - if (!infs) return message.reply(`There are no infractions stored for \`@${user.username}\`.`); - else { - let msg = `## ${infs.length} infractions stored for @${user.username}\n\u200b\n`; - let attachSpreadsheet = false; - for (const i in infs) { console.log(i) - let inf = infs[i]; - let toAdd = ''; - toAdd += `#${Number(i)+1}: \`${inf.reason}\` (${inf.type == InfractionType.Manual ? await fetchUsername(inf.createdBy!) : 'System'})\n`; - toAdd += `\u200b \u200b \u200b \u200b \u200b ↳ ${Day(inf.date).fromNow()} (Infraction ID: \`${inf._id}\`)\n`; + if (!inf) return message.reply('I can\'t find that ID.'); - if ((msg + toAdd).length > 1900 || Number(i) > 5) { - msg += `\u200b\n[${infs.length - Number(i) + 1} more, check attached file]`; - attachSpreadsheet = true; - break; - } - else msg += toAdd; - } + message.reply(`## Infraction deleted\n\u200b\n` + + `ID: \`${inf._id}\`\n` + + `Reason: \`${inf.reason}\` ` + + `(${inf.type == InfractionType.Manual ? await fetchUsername(inf.createdBy ?? '') : 'System'})\n` + + `Created ${Day(inf.date).fromNow()}`); + break; + default: + let user = await parseUser(args[0]); + if (!user) return message.reply('Unknown user'); - if (args[1]?.toLowerCase() == 'export-csv' || - args[1]?.toLowerCase() == 'csv' || - args[1]?.toLowerCase() == 'export') attachSpreadsheet = true; + let infs = userInfractions.get(user._id); + if (!infs) return message.reply(`There are no infractions stored for \`@${user.username}\`.`); + else { + let msg = `## ${infs.length} infractions stored for @${user.username}\n\u200b\n`; + let attachSpreadsheet = false; + for (const i in infs) { console.log(i) + let inf = infs[i]; + let toAdd = ''; + toAdd += `#${Number(i)+1}: \`${inf.reason}\` (${inf.type == InfractionType.Manual ? await fetchUsername(inf.createdBy!) : 'System'})\n`; + toAdd += `\u200b \u200b \u200b \u200b \u200b ↳ ${Day(inf.date).fromNow()} (Infraction ID: \`${inf._id}\`)\n`; - if (attachSpreadsheet) { - try { - let csv_data = [ - [`Warns for @${user.username} (${user._id}) - ${Day().toString()}`], - [], - ['Date', 'Reason', 'Created By', 'Type', 'ID'], - ]; - - for (const inf of infs) { - csv_data.push([ - Day(inf.date).toString(), - inf.reason, - inf.type == InfractionType.Manual ? `${await fetchUsername(inf.createdBy!)} (${inf.createdBy})` : 'SYSTEM', - inf.type == InfractionType.Automatic ? 'Automatic' : 'Manual', - inf._id, - ]); + if ((msg + toAdd).length > 1900 || Number(i) > 5) { + msg += `\u200b\n[${infs.length - Number(i) + 1} more, check attached file]`; + attachSpreadsheet = true; + break; + } + else msg += toAdd; } - let sheet = Xlsx.utils.aoa_to_sheet(csv_data); + if (args[1]?.toLowerCase() == 'export-csv' || + args[1]?.toLowerCase() == 'csv' || + args[1]?.toLowerCase() == 'export') attachSpreadsheet = true; - let csv = Xlsx.utils.sheet_to_csv(sheet); + if (attachSpreadsheet) { + try { + let csv_data = [ + [`Warns for @${user.username} (${user._id}) - ${Day().toString()}`], + [], + ['Date', 'Reason', 'Created By', 'Type', 'ID'], + ]; - let apiConfig: any = (await axios.get(client.apiURL)).data; - let autumnURL = apiConfig.features.autumn.url; - - let data = new FormData(); - data.append("file", csv, { filename: `${user._id}.csv` }); + for (const inf of infs) { + csv_data.push([ + Day(inf.date).toString(), + inf.reason, + inf.type == InfractionType.Manual ? `${await fetchUsername(inf.createdBy!)} (${inf.createdBy})` : 'SYSTEM', + inf.type == InfractionType.Automatic ? 'Automatic' : 'Manual', + inf._id, + ]); + } - let req = await axios.post(autumnURL + '/attachments', data, { headers: data.getHeaders() }); - message.reply({ content: msg, attachments: [ (req.data as any)['id'] as string ] }); - } catch(e) { - console.error(e); - message.reply(msg); + let sheet = Xlsx.utils.aoa_to_sheet(csv_data); + + let csv = Xlsx.utils.sheet_to_csv(sheet); + + let apiConfig: any = (await axios.get(client.apiURL)).data; + let autumnURL = apiConfig.features.autumn.url; + + let data = new FormData(); + data.append("file", csv, { filename: `${user._id}.csv` }); + + let req = await axios.post(autumnURL + '/attachments', data, { headers: data.getHeaders() }); + message.reply({ content: msg, attachments: [ (req.data as any)['id'] as string ] }); + } catch(e) { + console.error(e); + message.reply(msg); + } + } else message.reply(msg); } - } else message.reply(msg); + break; } } }