warns can now be deleted ig
This commit is contained in:
parent
2b851c813c
commit
ceef16e01f
1 changed files with 75 additions and 52 deletions
|
@ -16,7 +16,7 @@ export default {
|
||||||
name: 'warns',
|
name: 'warns',
|
||||||
aliases: [ 'warnings', 'infractions', 'infraction' ],
|
aliases: [ 'warnings', 'infractions', 'infraction' ],
|
||||||
description: 'Show all user infractions',
|
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[]) => {
|
run: async (message: Message, args: string[]) => {
|
||||||
if (!await isModerator(message.member!)) return message.reply(NO_MANAGER_MSG);
|
if (!await isModerator(message.member!)) return message.reply(NO_MANAGER_MSG);
|
||||||
|
|
||||||
|
@ -42,67 +42,90 @@ export default {
|
||||||
|
|
||||||
message.reply(msg.substr(0, 1999));
|
message.reply(msg.substr(0, 1999));
|
||||||
} else {
|
} else {
|
||||||
let user = await parseUser(args[0]);
|
switch(args[0]?.toLowerCase()) {
|
||||||
if (!user) return message.reply('Unknown user');
|
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 (!inf) return message.reply('I can\'t find that 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 ((msg + toAdd).length > 1900 || Number(i) > 5) {
|
message.reply(`## Infraction deleted\n\u200b\n`
|
||||||
msg += `\u200b\n[${infs.length - Number(i) + 1} more, check attached file]`;
|
+ `ID: \`${inf._id}\`\n`
|
||||||
attachSpreadsheet = true;
|
+ `Reason: \`${inf.reason}\` `
|
||||||
break;
|
+ `(${inf.type == InfractionType.Manual ? await fetchUsername(inf.createdBy ?? '') : 'System'})\n`
|
||||||
}
|
+ `Created ${Day(inf.date).fromNow()}`);
|
||||||
else msg += toAdd;
|
break;
|
||||||
}
|
default:
|
||||||
|
let user = await parseUser(args[0]);
|
||||||
|
if (!user) return message.reply('Unknown user');
|
||||||
|
|
||||||
if (args[1]?.toLowerCase() == 'export-csv' ||
|
let infs = userInfractions.get(user._id);
|
||||||
args[1]?.toLowerCase() == 'csv' ||
|
if (!infs) return message.reply(`There are no infractions stored for \`@${user.username}\`.`);
|
||||||
args[1]?.toLowerCase() == 'export') attachSpreadsheet = true;
|
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) {
|
if ((msg + toAdd).length > 1900 || Number(i) > 5) {
|
||||||
try {
|
msg += `\u200b\n[${infs.length - Number(i) + 1} more, check attached file]`;
|
||||||
let csv_data = [
|
attachSpreadsheet = true;
|
||||||
[`Warns for @${user.username} (${user._id}) - ${Day().toString()}`],
|
break;
|
||||||
[],
|
}
|
||||||
['Date', 'Reason', 'Created By', 'Type', 'ID'],
|
else msg += toAdd;
|
||||||
];
|
|
||||||
|
|
||||||
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 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;
|
for (const inf of infs) {
|
||||||
let autumnURL = apiConfig.features.autumn.url;
|
csv_data.push([
|
||||||
|
Day(inf.date).toString(),
|
||||||
let data = new FormData();
|
inf.reason,
|
||||||
data.append("file", csv, { filename: `${user._id}.csv` });
|
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() });
|
let sheet = Xlsx.utils.aoa_to_sheet(csv_data);
|
||||||
message.reply({ content: msg, attachments: [ (req.data as any)['id'] as string ] });
|
|
||||||
} catch(e) {
|
let csv = Xlsx.utils.sheet_to_csv(sheet);
|
||||||
console.error(e);
|
|
||||||
message.reply(msg);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue