/[sudobot]/branches/4.x/src/utils/ModerationHistoryGenerator.ts
ViewVC logotype

Contents of /branches/4.x/src/utils/ModerationHistoryGenerator.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 3761 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 /**
2 * This file is part of SudoBot.
3 *
4 * Copyright (C) 2021-2022 OSN Inc.
5 *
6 * SudoBot is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * SudoBot is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 import { User } from "discord.js";
21 import path from "path";
22 import fs from "fs/promises";
23 import Punishment from "../models/Punishment";
24 import { formatDistanceToNowStrict, format } from "date-fns";
25 import { convert } from "../commands/moderation/HistoryCommand";
26 import PunishmentType from "../types/PunishmentType";
27 import { v4 as uuid } from 'uuid';
28
29 export default class ModerationHistoryGenerator {
30 version = require(path.resolve(__dirname, '../../package.json')).version;
31 tmpFilePath: string | undefined = undefined;
32
33 constructor(protected user: User, protected guild: { id: string, name: string }) {
34
35 }
36
37 async generate(sendDM: boolean = false) {
38 const punishments = await Punishment.find({
39 guild_id: this.guild.id,
40 user_id: this.user.id,
41 }).sort({ createdAt: -1 });
42
43 const tmpFilePath = path.join(__dirname, '../../tmp', `history-${uuid()}.txt`);
44 this.tmpFilePath = tmpFilePath;
45
46 const tmpFile = await fs.open(tmpFilePath, 'a+');
47
48 await tmpFile.appendFile(`Moderation history of ${this.user.tag} (ID: ${this.user.id}) in ${this.guild.name}\n`);
49 await tmpFile.appendFile(`* ${punishments.length} total entries\n`);
50 await tmpFile.appendFile(`* Seeing something unexpected? Contact the moderators of ${this.guild.name}\n`);
51 await tmpFile.appendFile(`* Format: [Serial Number] - [Case ID] - [Type] - [Reason] - [Date]\n\n====================================================================\n\n`);
52
53 let i = 1;
54
55 for await (const punishment of punishments) {
56 await tmpFile.appendFile(`${i}. ${punishment.numericId} - ${convert(punishment.type as PunishmentType)} - ${punishment.reason ?? '*No Reason Provided*'} - ${punishment.createdAt} (${formatDistanceToNowStrict(punishment.createdAt, { addSuffix: true })})\n`);
57 i++;
58 }
59
60 if (punishments.length === 0) {
61 await tmpFile.appendFile(`No record available.`);
62 }
63
64 await tmpFile.appendFile(`\n\nGenerated by SudoBot/${this.version} on ${format(new Date(), "dd MMMM yyyy, h:mm a O")}`);
65 await tmpFile.close();
66
67 let dmStatus = false;
68
69 if (sendDM) {
70 try {
71 await this.user.send({
72 content: `We've generated your moderation history. Download the attached text file to see them. There are ${punishments.length} record(s) available.`,
73 files: [
74 {
75 name: `history-${this.user.id}.txt`,
76 attachment: tmpFilePath
77 }
78 ],
79 });
80
81 dmStatus = true;
82 }
83 catch (e) {
84 console.log(e);
85 }
86 }
87
88 return { dmStatus, tmpFilePath, size: punishments.length };
89 }
90
91 async removeTempFile() {
92 if (this.tmpFilePath)
93 return await fs.rm(this.tmpFilePath);
94 }
95 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26