mirror of
https://github.com/nx-bat/discordbot-ng.git
synced 2026-11-11 07:13:37 -05:00
(modals): Implement edit-knowledgebase-item-modal modal.
This commit is contained in:
@@ -1,54 +1,42 @@
|
||||
import { Client, MessageFlags, ModalSubmitInteraction } from 'discord.js';
|
||||
import { Client, ModalSubmitInteraction } from 'discord.js';
|
||||
import { Database } from '../shared/Database';
|
||||
import { deferInteraction, logCustomEvent } from '../utils';
|
||||
import { Modal } from '../types';
|
||||
|
||||
export default {
|
||||
name: 'edit-knowledgebase-item-modal',
|
||||
handler: async function (client: Client, interaction: ModalSubmitInteraction, idString: string) {
|
||||
if (!interaction.guild) return interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Must be ran in guild.' });
|
||||
class EditKnowledgebaseItemModal implements Modal<Client> {
|
||||
id = 'edit-knowledgebase-item-modal';
|
||||
|
||||
async execute(context: Client<boolean>, interaction: ModalSubmitInteraction, idString: string): Promise<void> {
|
||||
if (!interaction.inGuild()) return;
|
||||
await deferInteraction(interaction);
|
||||
|
||||
try {
|
||||
const id = parseInt(idString);
|
||||
const existing = await Database.getFromKnowledgebase(id);
|
||||
if (!existing) throw new Error("A knowledgebase item with that ID doesn't exist.");
|
||||
|
||||
const content = interaction.fields.getTextInputValue('content');
|
||||
if (content.length > 2000) throw new Error('Content cannot be longer than 2000 characters.');
|
||||
|
||||
const existingItem = await Database.getFromKnowledgebase(id);
|
||||
|
||||
if (!existingItem) return interaction.editReply('Knowledgebase item not found.');
|
||||
|
||||
if (content.length > 2000) return interaction.editReply('Content cannot be over 2000 characters long.');
|
||||
|
||||
logCustomEvent(interaction.guild!, {
|
||||
await logCustomEvent(interaction.guild!, {
|
||||
title: 'Knowledgebase Item Edited',
|
||||
description: null,
|
||||
color: 0xFFFF00,
|
||||
timestamp: new Date(),
|
||||
fields: [
|
||||
{
|
||||
name: 'User',
|
||||
value: `<@${interaction.user.id}>\n${interaction.user.username}`,
|
||||
inline: true
|
||||
},
|
||||
{
|
||||
name: 'Name',
|
||||
value: existingItem.name,
|
||||
inline: true
|
||||
},
|
||||
{
|
||||
name: 'Old Content',
|
||||
value: existingItem.content,
|
||||
inline: true
|
||||
},
|
||||
{
|
||||
name: 'New Content',
|
||||
value: content,
|
||||
inline: true
|
||||
}
|
||||
]
|
||||
{ name: 'User', value: `${interaction.user} (${interaction.user.id})`, inline: false },
|
||||
{ name: 'Name', value: existing.name, inline: false },
|
||||
{ name: 'Old Content', value: existing.content, inline: false },
|
||||
{ name: 'New Content', value: content, inline: false },
|
||||
],
|
||||
});
|
||||
|
||||
await Database.editKnowledgebaseItem(id, content);
|
||||
|
||||
return interaction.editReply(`Edited knowledgebase entry \`${existingItem.name}\`.`);
|
||||
await interaction.editReply(`\`${existing.name}\` has been edited.`);
|
||||
} catch (error: Error | any) {
|
||||
await interaction.editReply({ content: error.message }).catch(() => {});
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default new EditKnowledgebaseItemModal();
|
||||
Reference in New Issue
Block a user