/[sudobot]/branches/5.x/src/commands/moderation/ReportMessageCommand.ts
ViewVC logotype

Contents of /branches/5.x/src/commands/moderation/ReportMessageCommand.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: 6857 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-2023 OSN Developers.
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 {
21 ApplicationCommandType,
22 Colors,
23 EmbedBuilder,
24 GuildMember,
25 MessageContextMenuCommandInteraction,
26 PermissionsString
27 } from "discord.js";
28 import Command, { CommandReturn, ValidationRule } from "../../core/Command";
29 import { ContextMenuCommandContext } from "../../services/CommandManager";
30 import { channelInfo, messageInfo, userInfo } from "../../utils/embed";
31 import { safeChannelFetch } from "../../utils/fetch";
32 import { logError } from "../../utils/logger";
33
34 export default class ReportMessageCommand extends Command {
35 public readonly name = "Report Message";
36 public readonly validationRules: ValidationRule[] = [];
37 public readonly permissions = [];
38 public readonly applicationCommandType = ApplicationCommandType.Message;
39
40 public readonly supportsLegacy = false;
41 public readonly description = "Reports the target message to the message log channel and deletes it.";
42
43 permissionCheck(interaction: MessageContextMenuCommandInteraction) {
44 const config = this.client.configManager.config[interaction.guildId!]?.message_reporting;
45
46 if (config?.users?.includes(interaction.user.id)) {
47 return true;
48 }
49
50 const member = interaction.member as GuildMember;
51
52 if (member?.roles.cache.hasAll(...(config?.roles ?? []))) {
53 return true;
54 }
55
56 if (
57 config?.permissionLevel !== undefined &&
58 config?.permissionLevel >= 0 &&
59 this.client.configManager.config[interaction.guildId!]?.permissions.mode === "levels" &&
60 this.client.permissionManager.getMemberPermissionLevel(member) >= config?.permissionLevel
61 ) {
62 return true;
63 }
64
65 if (member?.permissions.has((config?.permissions ?? []) as PermissionsString[], true)) {
66 return true;
67 }
68
69 return false;
70 }
71
72 async execute(interaction: MessageContextMenuCommandInteraction, context: ContextMenuCommandContext): Promise<CommandReturn> {
73 const { targetMessage } = interaction;
74
75 await interaction.deferReply({ ephemeral: true });
76
77 if (!this.client.permissionManager.shouldModerate(targetMessage.member!, interaction.member as GuildMember)) {
78 await this.error(interaction, "You don't have permission to report messsages from this user!");
79 return;
80 }
81
82 if (!this.permissionCheck(interaction)) {
83 await this.error(interaction, "You don't have permission to run this command");
84 return;
85 }
86
87 if (!this.client.configManager.config[interaction.guildId!]?.message_reporting?.enabled) {
88 await interaction.editReply({
89 content: "This server has message reporting turned off. Please turn it on to use this command."
90 });
91
92 return;
93 }
94
95 const channelId =
96 this.client.configManager.config[interaction.guildId!]?.message_reporting?.channel ??
97 this.client.configManager.config[interaction.guildId!]?.logging?.primary_channel;
98
99 if (!channelId) {
100 await interaction.editReply({
101 content: "This server does not have report logging channel set up. Please set it up first."
102 });
103
104 return;
105 }
106
107 const channel = await safeChannelFetch(interaction.guild!, channelId!);
108
109 if (!channel?.isTextBased()) {
110 await interaction.editReply({
111 content: "Could not send the reported message to the log channel. This is probably due to a misconfiguration."
112 });
113 return;
114 }
115
116 let url = "";
117
118 try {
119 url = (
120 await channel.send({
121 embeds: [
122 new EmbedBuilder({
123 color: Colors.Red,
124 title: "Message reported",
125 author: {
126 name: targetMessage.author.username,
127 icon_url: targetMessage.author.displayAvatarURL()
128 },
129 description: targetMessage.content ?? "No content",
130 fields: [
131 {
132 name: "User",
133 value: userInfo(targetMessage.author),
134 inline: true
135 },
136 {
137 name: "Reported By",
138 value: userInfo(interaction.user),
139 inline: true
140 },
141 {
142 name: "Message",
143 value: messageInfo(targetMessage)
144 },
145 {
146 name: "Channel",
147 value: channelInfo(targetMessage.channel)
148 }
149 ],
150 footer: {
151 text: "Reported"
152 }
153 }).setTimestamp()
154 ],
155 files: targetMessage.attachments.toJSON()
156 })
157 ).url;
158 } catch (e) {
159 logError(e);
160 await this.error(
161 interaction,
162 "Could not send the reported message into the log channel! Make sure I have enough permissions."
163 );
164 return;
165 }
166
167 try {
168 await targetMessage.delete();
169 } catch (e) {
170 logError(e);
171 await this.error(interaction, "Could not remove the reported message! Make sure I have enough permissions.");
172 }
173
174 await this.success(
175 interaction,
176 `The message was reported and removed. [Click here](${url}) to nagivate to the reported message.`
177 );
178 }
179 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26