(modals): Implement report-message modal.

This commit is contained in:
2026-10-14 18:02:43 +08:00
parent c2c6d5f9f0
commit 99fd6403a2
2 changed files with 75 additions and 106 deletions
+4 -16
View File
@@ -1,5 +1,4 @@
import { ApplicationCommandType, ApplicationIntegrationType, Client, ContextMenuCommandBuilder, InteractionContextType, LabelBuilder, MessageContextMenuCommandInteraction, MessageFlags, ModalBuilder, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, TextInputBuilder, TextInputStyle } from 'discord.js'; import { ApplicationCommandType, ApplicationIntegrationType, CheckboxBuilder, Client, ContextMenuCommandBuilder, InteractionContextType, LabelBuilder, MessageContextMenuCommandInteraction, MessageFlags, ModalBuilder, TextInputBuilder, TextInputStyle } from 'discord.js';
import { Database } from '../shared/Database'; import { Database } from '../shared/Database';
export default { export default {
@@ -28,24 +27,13 @@ export default {
.setDescription('Any additional information? This will help staff understand your report. 1000 characters maximum.') .setDescription('Any additional information? This will help staff understand your report. 1000 characters maximum.')
.setTextInputComponent(additionalInfoInput); .setTextInputComponent(additionalInfoInput);
const yesNoMenu = new StringSelectMenuBuilder() const createTicketCheckbox = new CheckboxBuilder()
.setCustomId('create-private-help-ticket') .setCustomId('create-private-help-ticket');
.setRequired(false)
.addOptions(
new StringSelectMenuOptionBuilder()
.setLabel('No')
.setDefault(true)
.setValue('no'),
new StringSelectMenuOptionBuilder()
.setLabel('Yes')
.setDefault(false)
.setValue('yes')
);
const createPrivateTicket = new LabelBuilder() const createPrivateTicket = new LabelBuilder()
.setLabel('Create Private Help Ticket') .setLabel('Create Private Help Ticket')
.setDescription('Should a private help ticket be opened with this report? (Does not bypass ticket restrictions)') .setDescription('Should a private help ticket be opened with this report? (Does not bypass ticket restrictions)')
.setStringSelectMenuComponent(yesNoMenu); .setCheckboxComponent(createTicketCheckbox);
modal.addLabelComponents(additionalInfoLabel, createPrivateTicket); modal.addLabelComponents(additionalInfoLabel, createPrivateTicket);
+29 -48
View File
@@ -1,75 +1,52 @@
import { ActionRowBuilder, AttachmentBuilder, ButtonBuilder, ButtonStyle, Client, EmbedBuilder, GuildTextBasedChannel, MessageFlags, ModalSubmitInteraction } from 'discord.js'; import { ActionRowBuilder, AttachmentBuilder, ButtonBuilder, ButtonStyle, Client, EmbedBuilder, GuildTextBasedChannel, MessageFlags, ModalSubmitInteraction } from 'discord.js';
import { Database } from '../shared/Database'; import { Database } from '../shared/Database';
import { canOpenPrivateHelpTicket, createPrivateHelpTicketThread } from '../utils'; import { canOpenPrivateHelpTicket, createPrivateHelpTicketThread } from '../utils';
import { Modal } from '../types';
export default { class ReportMessageModal implements Modal<Client> {
name: 'report-message', id = 'report-message';
handler: async function (client: Client, interaction: ModalSubmitInteraction, channelId: string, messageId: string) {
async execute(context: Client<boolean>, interaction: ModalSubmitInteraction, channelId: string, messageId: string): Promise<void> {
if (!interaction.inGuild()) return;
await interaction.deferReply({ flags: [MessageFlags.Ephemeral] }); await interaction.deferReply({ flags: [MessageFlags.Ephemeral] });
const guild = await client.guilds.fetch(interaction.guildId!); try {
const guild = await context.guilds.fetch(interaction.guildId!);
const member = await guild.members.fetch(interaction.user.id); const member = await guild.members.fetch(interaction.user.id);
const reportedMessageChannel = await guild.channels.fetch(channelId) as GuildTextBasedChannel; const reportedMessageChannel = await guild.channels.fetch(channelId) as GuildTextBasedChannel;
const reportedMessage = await reportedMessageChannel?.messages.fetch(messageId); const reportedMessage = await reportedMessageChannel?.messages.fetch(messageId);
const additionalInfo = interaction.fields.getTextInputValue('additional-info'); const additionalInfo = interaction.fields.getTextInputValue('additional-info');
const createPrivateHelpTicket = interaction.fields.getStringSelectValues('create-private-help-ticket')[0] == 'yes'; const createPrivateHelpTicket = interaction.fields.getCheckbox('create-private-help-ticket');
if (!reportedMessageChannel || !reportedMessage) if (!reportedMessageChannel || !reportedMessage) throw new Error('Failed to submit report. Please report this to a staff member.');
return interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Failed to submit report. Please report this to a staff member.' });
const guildSettings = await Database.getOrCreateSettings(interaction.guildId!); const settings = await Database.getOrCreateSettings(interaction.guildId);
if (!settings.moderator_channel_id) throw new Error('Report channel missing. Unable to submit report.');
if (!guildSettings || !guildSettings.moderator_channel_id) const reportsChannel = await interaction.guild!.channels.fetch(settings.moderator_channel_id);
return interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Report channel missing. Unable to submit report.' }); if (!reportsChannel || !reportsChannel.isSendable()) throw new Error('Report channel missing. Unable to submit report.');
const reportsChannel = await interaction.guild!.channels.fetch(guildSettings.moderator_channel_id);
if (!reportsChannel || !reportsChannel.isSendable())
return interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Report channel missing. Unable to submit report.' });
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setTitle('New Message Report!') .setTitle('New Message Report!')
.setColor(0xFF0000) .setColor(0xFF0000)
.addFields( .addFields(
{ { name: 'Message', value: reportedMessage.url, inline: false },
name: 'Message', { name: 'Author', value: reportedMessage.author.toString(), inline: false },
value: reportedMessage.url, { name: 'Reporter', value: member.toString(), inline: false }
inline: false
},
{
name: 'Message Author',
value: reportedMessage.author.toString(),
inline: false
},
{
name: 'Reporter',
value: member.toString(),
inline: false
});
if (additionalInfo) {
embed.addFields(
{
name: 'Additional Information',
value: additionalInfo,
inline: false
}
); );
}
if (additionalInfo) embed.addFields({ name: 'Additional Information', value: additionalInfo, inline: false });
let replyContent = "Thanks for making a report! I've notified the moderators who can take further action."; let replyContent = "Thanks for making a report! I've notified the moderators who can take further action.";
const wantsTicketButCantOpen = createPrivateHelpTicket && !await canOpenPrivateHelpTicket(member.id); const wantsTicketButCantOpen = createPrivateHelpTicket && !await canOpenPrivateHelpTicket(member.id);
if (wantsTicketButCantOpen) { if (wantsTicketButCantOpen) {
embed.addFields( embed.addFields({
{
name: 'User Requested Private Ticket', name: 'User Requested Private Ticket',
value: 'The user requested a private ticket be opened, but already has an open ticket. If additional information is needed, press the button below to open a ticket with the user.', value: 'The user requested a private ticket be opened, but already has an open ticket. If additional information is needed, press the button below to open a ticket with the user.',
inline: false inline: false
} });
);
replyContent += " Could not open private help ticket since you already have one open. Staff have been notified that you'd like to have a ticket opened, and can make one for you if they deem it necessary."; replyContent += " Could not open private help ticket since you already have one open. Staff have been notified that you'd like to have a ticket opened, and can make one for you if they deem it necessary.";
} }
@@ -89,12 +66,13 @@ export default {
await reportsChannel.send({ files: [new AttachmentBuilder(Buffer.from(reportedMessage.content), { name: 'message-content.txt' })] }); await reportsChannel.send({ files: [new AttachmentBuilder(Buffer.from(reportedMessage.content), { name: 'message-content.txt' })] });
if (createPrivateHelpTicket && !wantsTicketButCantOpen) { if (createPrivateHelpTicket && !wantsTicketButCantOpen) {
if (!guildSettings.private_help_channel_id) { if (!settings.private_help_channel_id) {
replyContent += ' Could not open private help ticket. Private help channel not set. Please report this to a staff member.'; replyContent += ' Could not open private help ticket. Private help channel not set. Please report this to a staff member.';
} else { } else {
const thread = await createPrivateHelpTicketThread(client, guild, member, `Ticket created with message report (${reportMessage.url}). ${member}, use this channel to talk with staff privately about the reported message (${reportedMessage.url}). ${additionalInfo ? `\n\nAdditional information provided in report:\n${additionalInfo.split('\n').map(c => `> ${c}`).join('\n')}` : ''}`); const thread = await createPrivateHelpTicketThread(context, guild, member, `Ticket created with message report (${reportMessage.url}). ${member}, use this channel to talk with staff privately about the reported message (${reportedMessage.url}). ${additionalInfo ? `\n\nAdditional information provided in report:\n${additionalInfo.split('\n').map(c => `> ${c}`).join('\n')}` : ''}`);
if (thread) { if (thread) {
replyContent += ` Private help ticket created: ${thread}.`; replyContent += ` Private help ticket created: ${thread}.`;
embed.addFields({ embed.addFields({
name: 'Private Help Ticket', name: 'Private Help Ticket',
value: thread.url, value: thread.url,
@@ -102,9 +80,9 @@ export default {
}); });
await reportMessage.edit({ embeds: [embed] }); await reportMessage.edit({ embeds: [embed] });
} else { } else {
replyContent += ' There was an issue opening a private help ticket, please report this to a staff member.'; replyContent += ' There was an issue opening a private help ticket, please report this to a staff member.';
await reportMessage.edit({ await reportMessage.edit({
embeds: [embed], embeds: [embed],
components: [row] components: [row]
@@ -114,5 +92,8 @@ export default {
} }
await interaction.editReply(replyContent); await interaction.editReply(replyContent);
} catch (error: Error | any) {
await interaction.editReply({ content: error.message }).catch(() => {});
}
}
} }
};