1 |
rakinar2 |
577 |
/** |
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 |
|
|
APIEmbed, |
22 |
|
|
ActionRowBuilder, |
23 |
|
|
ApplicationCommandType, |
24 |
|
|
CacheType, |
25 |
|
|
Interaction, |
26 |
|
|
MessageContextMenuCommandInteraction, |
27 |
|
|
ModalBuilder, |
28 |
|
|
PermissionsBitField, |
29 |
|
|
TextInputBuilder, |
30 |
|
|
TextInputStyle |
31 |
|
|
} from "discord.js"; |
32 |
|
|
import Command, { CommandReturn, ValidationRule } from "../../core/Command"; |
33 |
|
|
import { GatewayEventListener } from "../../decorators/GatewayEventListener"; |
34 |
|
|
import { HasEventListeners } from "../../types/HasEventListeners"; |
35 |
|
|
import EmbedSchemaParser from "../../utils/EmbedSchemaParser"; |
36 |
|
|
import { logError } from "../../utils/logger"; |
37 |
|
|
|
38 |
|
|
export default class EditMessageCommand extends Command implements HasEventListeners { |
39 |
|
|
public readonly name = "Edit Message"; |
40 |
|
|
public readonly validationRules: ValidationRule[] = []; |
41 |
|
|
public readonly permissions = [PermissionsBitField.Flags.ManageMessages]; |
42 |
|
|
public readonly applicationCommandType = ApplicationCommandType.Message; |
43 |
|
|
public readonly supportsLegacy = false; |
44 |
|
|
|
45 |
|
|
public readonly description = "Edits messages sent by the bot."; |
46 |
|
|
|
47 |
|
|
@GatewayEventListener("interactionCreate") |
48 |
|
|
async onInteractionCreate(interaction: Interaction<CacheType>) { |
49 |
|
|
if (!interaction.isModalSubmit() || !interaction.customId.startsWith("editmsg__")) { |
50 |
|
|
return; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
const echoMentions = this.client.configManager.config[interaction.guildId!]?.commands?.echo_mentions ?? false; |
54 |
|
|
|
55 |
|
|
await interaction |
56 |
|
|
.deferReply({ |
57 |
|
|
ephemeral: true |
58 |
|
|
}) |
59 |
|
|
.catch(logError); |
60 |
|
|
|
61 |
|
|
const [, id] = interaction.customId.split("__"); |
62 |
|
|
|
63 |
|
|
const message = await interaction.channel?.messages.fetch(id).catch(logError); |
64 |
|
|
|
65 |
|
|
if (!message) { |
66 |
|
|
await interaction.editReply({ |
67 |
|
|
content: "Failed to edit message, maybe it was removed?" |
68 |
|
|
}); |
69 |
|
|
|
70 |
|
|
return; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
if (message.author.id !== this.client.user!.id) { |
74 |
|
|
await interaction.editReply({ |
75 |
|
|
content: "You cannot edit this message!" |
76 |
|
|
}); |
77 |
|
|
|
78 |
|
|
return; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
const options = { |
82 |
|
|
content: interaction.fields.getTextInputValue("content"), |
83 |
|
|
allowedMentions: (interaction.member?.permissions as Readonly<PermissionsBitField>)?.has("MentionEveryone", true) |
84 |
|
|
? undefined |
85 |
|
|
: echoMentions |
86 |
|
|
? undefined |
87 |
|
|
: { |
88 |
|
|
parse: ["users" as const] |
89 |
|
|
} |
90 |
|
|
}; |
91 |
|
|
|
92 |
|
|
const oldMessageOptions = { |
93 |
|
|
content: message.content, |
94 |
|
|
embeds: [...message.embeds], |
95 |
|
|
files: [...message.attachments.toJSON()] |
96 |
|
|
}; |
97 |
|
|
|
98 |
|
|
try { |
99 |
|
|
await EmbedSchemaParser.editMessage(message, options); |
100 |
|
|
} catch (error) { |
101 |
|
|
logError(error); |
102 |
|
|
await interaction.editReply({ |
103 |
|
|
content: `${this.emoji("error")} An error has occurred while trying to update the message.` |
104 |
|
|
}); |
105 |
|
|
return; |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
await interaction |
109 |
|
|
.editReply({ |
110 |
|
|
content: `${this.emoji("check")} Message updated successfully.` |
111 |
|
|
}) |
112 |
|
|
.catch(logError); |
113 |
|
|
|
114 |
|
|
const embed: APIEmbed = {}; |
115 |
|
|
|
116 |
|
|
await this.sendCommandRanLog(message, embed, { |
117 |
|
|
url: message.url, |
118 |
|
|
previews: [options], |
119 |
|
|
async before(channel, sentMessages) { |
120 |
|
|
const oldMessage = await channel.send(oldMessageOptions).catch(logError); |
121 |
|
|
embed.description = `The edited message preview is [above](${sentMessages[0]?.url}), and the old message is in the [middle](${oldMessage?.url}).`; |
122 |
|
|
} |
123 |
|
|
}); |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
async execute(interaction: MessageContextMenuCommandInteraction): Promise<CommandReturn> { |
127 |
|
|
if (interaction.targetMessage.author.id !== this.client.user!.id) { |
128 |
|
|
await interaction.reply({ |
129 |
|
|
content: `${this.emoji("error")} You cannot edit this message.`, |
130 |
|
|
ephemeral: true |
131 |
|
|
}); |
132 |
|
|
|
133 |
|
|
return; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
const input = new TextInputBuilder() |
137 |
|
|
.setCustomId("content") |
138 |
|
|
.setLabel("Content") |
139 |
|
|
.setPlaceholder("Type the message content here...") |
140 |
|
|
.setRequired(true) |
141 |
|
|
.setStyle(TextInputStyle.Paragraph); |
142 |
|
|
|
143 |
|
|
if (interaction.targetMessage.content !== "") { |
144 |
|
|
input.setValue(interaction.targetMessage.content); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
const modal = new ModalBuilder() |
148 |
|
|
.setCustomId(`editmsg__${interaction.targetMessage.id}`) |
149 |
|
|
.setTitle("Edit Message") |
150 |
|
|
.setComponents(new ActionRowBuilder<TextInputBuilder>().addComponents(input)); |
151 |
|
|
|
152 |
|
|
await interaction.showModal(modal).catch(logError); |
153 |
|
|
} |
154 |
|
|
} |