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 { ApplicationCommandType, EmbedBuilder, MessageContextMenuCommandInteraction, PermissionsBitField } from "discord.js"; |
21 |
import Command, { CommandReturn, ValidationRule } from "../../core/Command"; |
22 |
import { ContextMenuCommandContext } from "../../services/CommandManager"; |
23 |
import { channelInfo, messageInfo, userInfo } from "../../utils/embed"; |
24 |
import { safeChannelFetch } from "../../utils/fetch"; |
25 |
import { logError } from "../../utils/logger"; |
26 |
|
27 |
export default class SaveMessageCommand extends Command { |
28 |
public readonly name = "Save Message"; |
29 |
public readonly validationRules: ValidationRule[] = []; |
30 |
public readonly permissions = [PermissionsBitField.Flags.ManageMessages]; |
31 |
public readonly applicationCommandType = ApplicationCommandType.Message; |
32 |
|
33 |
public readonly supportsLegacy = false; |
34 |
public readonly description = "Saves the target message to the message log channel"; |
35 |
|
36 |
async execute(interaction: MessageContextMenuCommandInteraction, context: ContextMenuCommandContext): Promise<CommandReturn> { |
37 |
const { targetMessage } = interaction; |
38 |
|
39 |
await interaction.deferReply({ ephemeral: true }); |
40 |
|
41 |
if (!this.client.configManager.config[interaction.guildId!]?.logging?.enabled) { |
42 |
await interaction.editReply({ |
43 |
content: "This server has logging turned off. Please turn it on to use this command." |
44 |
}); |
45 |
|
46 |
return; |
47 |
} |
48 |
|
49 |
const channelId = |
50 |
this.client.configManager.config[interaction.guildId!]?.logging?.saved_messages_channel ?? |
51 |
this.client.configManager.config[interaction.guildId!]?.logging?.primary_channel; |
52 |
|
53 |
if (!channelId) { |
54 |
await interaction.editReply({ |
55 |
content: "This server does not have logging channel set up. Please set it up first." |
56 |
}); |
57 |
|
58 |
return; |
59 |
} |
60 |
|
61 |
const channel = await safeChannelFetch(interaction.guild!, channelId!); |
62 |
|
63 |
if (!channel?.isTextBased()) { |
64 |
await interaction.editReply({ |
65 |
content: "Could not send the saved message to the log channel. This is probably due to a misconfiguration." |
66 |
}); |
67 |
return; |
68 |
} |
69 |
|
70 |
let url = ""; |
71 |
|
72 |
try { |
73 |
url = ( |
74 |
await channel.send({ |
75 |
embeds: [ |
76 |
new EmbedBuilder({ |
77 |
color: 0x007bff, |
78 |
title: "Message saved", |
79 |
author: { |
80 |
name: targetMessage.author.username, |
81 |
icon_url: targetMessage.author.displayAvatarURL() |
82 |
}, |
83 |
description: targetMessage.content ?? "No content", |
84 |
fields: [ |
85 |
{ |
86 |
name: "User", |
87 |
value: userInfo(targetMessage.author), |
88 |
inline: true |
89 |
}, |
90 |
{ |
91 |
name: "Saved By", |
92 |
value: userInfo(interaction.user), |
93 |
inline: true |
94 |
}, |
95 |
{ |
96 |
name: "Message", |
97 |
value: messageInfo(targetMessage) |
98 |
}, |
99 |
{ |
100 |
name: "Channel", |
101 |
value: channelInfo(targetMessage.channel) |
102 |
} |
103 |
], |
104 |
footer: { |
105 |
text: "Saved" |
106 |
} |
107 |
}).setTimestamp() |
108 |
], |
109 |
files: targetMessage.attachments.toJSON() |
110 |
}) |
111 |
).url; |
112 |
} catch (e) { |
113 |
logError(e); |
114 |
await this.error( |
115 |
interaction, |
116 |
"Could not send the saved message into the log channel! Make sure I have enough permissions." |
117 |
); |
118 |
return; |
119 |
} |
120 |
|
121 |
await this.success(interaction, `The message was saved. [Click here](${url}) to nagivate to the saved message.`); |
122 |
} |
123 |
} |