/[sudobot]/branches/4.x/src/commands/moderation/InfractionViewCommand.ts
ViewVC logotype

Contents of /branches/4.x/src/commands/moderation/InfractionViewCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months, 1 week ago) by rakinar2
File MIME type: application/typescript
File size: 4803 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 { formatDistanceToNowStrict } from "date-fns";
21 import { Message, CacheType, CommandInteraction, User, Util, Permissions } from "discord.js";
22 import Client from "../../client/Client";
23 import MessageEmbed from "../../client/MessageEmbed";
24 import Punishment from "../../models/Punishment";
25 import CommandOptions from "../../types/CommandOptions";
26 import InteractionOptions from "../../types/InteractionOptions";
27 import PunishmentType from "../../types/PunishmentType";
28 import BaseCommand from "../../utils/structures/BaseCommand";
29 import { convert } from "./HistoryCommand";
30
31 export default class InfractionViewCommand extends BaseCommand {
32 name = "infraction__view";
33 category = "moderation";
34 aliases = ['i', 'infr', 'punishment'];
35 supportsInteractions = true;
36 permissions = [Permissions.FLAGS.MANAGE_MESSAGES];
37
38 async run(client: Client, message: CommandInteraction<CacheType> | Message<boolean>, options: CommandOptions | InteractionOptions): Promise<void> {
39 if (!options.isInteraction && options.args[0] === undefined) {
40 await message.reply(":x: You must provide an ID of an infraction to view it!");
41 return;
42 }
43
44 const id = options.isInteraction ? options.options.getInteger('id', true) : options.args[0];
45
46 if (!/^\d+$/.test(id.toString())) {
47 await message.reply(":x: Invalid ID given.");
48 return;
49 }
50
51 if (message instanceof CommandInteraction)
52 await message.deferReply();
53
54 const punishment = await Punishment.findOne({
55 numericId: id,
56 guild_id: message.guild!.id,
57 });
58
59 if (!punishment) {
60 await this.deferReply(message, ":x: No infraction found! Make sure the ID is correct and/or the infraction was not deleted manually!");
61 return;
62 }
63
64 let user: User | undefined;
65
66 try {
67 user = await client.users.fetch(punishment.user_id);
68 }
69 catch (e) {
70 console.log(e);
71 }
72
73 let str = '';
74
75 if (punishment.meta) {
76 const json = typeof punishment.meta === 'string' ? JSON.parse(punishment.meta) : punishment.meta;
77
78 if (Object.keys(json).length > 0) {
79 str += "Additional Attributes:\n```\n";
80
81 for (const key in json) {
82 str += `${key}: ${json[key]}\n`;
83 }
84
85 str += '\n```\n';
86 }
87 }
88
89 await this.deferReply(message, {
90 embeds: [
91 new MessageEmbed({
92 author: {
93 name: user?.tag ?? `Unknown (ID: ${punishment.user_id})`,
94 iconURL: user?.displayAvatarURL()
95 },
96 title: 'Viewing Infraction: ' + id,
97 fields: [
98 {
99 name: 'Type',
100 value: convert(punishment.type as PunishmentType),
101 inline: true
102 },
103 {
104 name: 'Moderator',
105 value: `<@${punishment.mod_id}> (${Util.escapeMarkdown(punishment.mod_tag)})`,
106 inline: true
107 },
108 {
109 name: 'Meta Info',
110 value: str.trim() === '' ? '*No info*' : str,
111 },
112 {
113 name: 'Reason',
114 value: punishment.reason && punishment.reason.trim() !== '' ? punishment.reason : '*No reason provided*',
115 },
116 {
117 name: 'Date',
118 value: `${punishment.createdAt.toUTCString()} (${formatDistanceToNowStrict(punishment.createdAt, { addSuffix: true })})`,
119 },
120 ],
121 })
122 .setTimestamp()
123 ]
124 });
125 }
126 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26