(modals): Implement add-knowledgebase-item-modal modal.

This commit is contained in:
2026-10-14 13:36:48 +08:00
parent 9d31d5a3e0
commit 920fc347fb
2 changed files with 32 additions and 40 deletions
+31 -39
View File
@@ -1,49 +1,41 @@
import { Client, MessageFlags, ModalSubmitInteraction } from 'discord.js'; import { Client, ModalSubmitInteraction } from 'discord.js';
import { Database } from '../shared/Database'; import { Database } from '../shared/Database';
import { Modal } from '../types';
import { deferInteraction, logCustomEvent } from '../utils'; import { deferInteraction, logCustomEvent } from '../utils';
export default { class AddKnowledgebaseItemModal implements Modal<Client> {
name: 'add-knowledgebase-item-modal', id = 'add-knowledgebase-item-modal';
handler: async function (client: Client, interaction: ModalSubmitInteraction) {
if (!interaction.guild) return interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Must be ran in guild.' });
async execute(context: Client<boolean>, interaction: ModalSubmitInteraction): Promise<void> {
if (!interaction.inGuild()) return;
await deferInteraction(interaction); await deferInteraction(interaction);
const name = interaction.fields.getTextInputValue('name'); try {
const content = interaction.fields.getTextInputValue('content'); const name = interaction.fields.getTextInputValue('name');
const content = interaction.fields.getTextInputValue('content');
if (content.length > 2000) throw new Error('Content cannot be longer than 2000 characters.');
if (content.length > 2000) return interaction.editReply('Content cannot be over 2000 characters long.'); const existing = await Database.getFromKnowledgebaseByName(interaction.guildId, name);
if (existing) throw new Error(`A knowledgebase item under \`${name}\` already exists.`);
const existingItem = await Database.getFromKnowledgebaseByName(interaction.guild.id, name); await logCustomEvent(interaction.guild!, {
title: 'Knowledgebase Item Added',
description: null,
color: 0x00FF00,
timestamp: new Date(),
fields: [
{ name: 'User', value: `${interaction.user} (${interaction.user.id})`, inline: false },
{ name: 'Name', value: name, inline: false },
{ name: 'Content', value: content, inline: false },
],
});
if (existingItem) return interaction.editReply(`Knowledgebase item ${name} already exists!`); await Database.addToKnowledgebase(interaction.guildId, name, content);
await interaction.editReply(`\`${name}\` has been added to the knowledgebase.`);
logCustomEvent(interaction.guild!, { } catch (error: Error | any) {
title: 'Knowledgebase Item Added', await interaction.editReply({ content: error.message }).catch(() => {});
description: null, }
color: 0x00FF00,
timestamp: new Date(),
fields: [
{
name: 'User',
value: `<@${interaction.user.id}>\n${interaction.user.username}`,
inline: true
},
{
name: 'Name',
value: name,
inline: true
},
{
name: 'Content',
value: content,
inline: true
}
]
});
await Database.addToKnowledgebase(interaction.guild.id, name, content);
return interaction.editReply(`Entry \`${name}\` added to knowledgebase.`);
} }
}; }
export default new AddKnowledgebaseItemModal();
+1 -1
View File
@@ -1,5 +1,5 @@
import { ChatInputCommandInteraction, ContextMenuCommandInteraction, GuildBasedChannel, MessageFlags, ModalSubmitInteraction } from 'discord.js'; import { ChatInputCommandInteraction, ContextMenuCommandInteraction, GuildBasedChannel, MessageFlags, ModalSubmitInteraction } from 'discord.js';
import { channelIsInStaffCategory } from './channel-utils'; import { channelIsInStaffCategory } from '.';
export async function deferInteraction(interaction: ChatInputCommandInteraction | ContextMenuCommandInteraction | ModalSubmitInteraction) { export async function deferInteraction(interaction: ChatInputCommandInteraction | ContextMenuCommandInteraction | ModalSubmitInteraction) {
const isStaffChannel = await channelIsInStaffCategory(interaction.channel as GuildBasedChannel); const isStaffChannel = await channelIsInStaffCategory(interaction.channel as GuildBasedChannel);