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 { Database } from '../shared/Database';
|
||||||
import { deferInteraction, logCustomEvent } from '../utils';
|
import { deferInteraction, logCustomEvent } from '../utils';
|
||||||
|
import { Modal } from '../types';
|
||||||
|
|
||||||
export default {
|
class EditKnowledgebaseItemModal implements Modal<Client> {
|
||||||
name: 'edit-knowledgebase-item-modal',
|
id = '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.' });
|
|
||||||
|
|
||||||
|
async execute(context: Client<boolean>, interaction: ModalSubmitInteraction, idString: string): Promise<void> {
|
||||||
|
if (!interaction.inGuild()) return;
|
||||||
await deferInteraction(interaction);
|
await deferInteraction(interaction);
|
||||||
|
|
||||||
const id = parseInt(idString);
|
try {
|
||||||
const content = interaction.fields.getTextInputValue('content');
|
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 existingItem = await Database.getFromKnowledgebase(id);
|
const content = interaction.fields.getTextInputValue('content');
|
||||||
|
if (content.length > 2000) throw new Error('Content cannot be longer than 2000 characters.');
|
||||||
|
|
||||||
if (!existingItem) return interaction.editReply('Knowledgebase item not found.');
|
await logCustomEvent(interaction.guild!, {
|
||||||
|
title: 'Knowledgebase Item Edited',
|
||||||
|
description: null,
|
||||||
|
color: 0xFFFF00,
|
||||||
|
timestamp: new Date(),
|
||||||
|
fields: [
|
||||||
|
{ 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 },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
if (content.length > 2000) return interaction.editReply('Content cannot be over 2000 characters long.');
|
await Database.editKnowledgebaseItem(id, content);
|
||||||
|
await interaction.editReply(`\`${existing.name}\` has been edited.`);
|
||||||
logCustomEvent(interaction.guild!, {
|
} catch (error: Error | any) {
|
||||||
title: 'Knowledgebase Item Edited',
|
await interaction.editReply({ content: error.message }).catch(() => {});
|
||||||
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
|
|
||||||
}
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
await Database.editKnowledgebaseItem(id, content);
|
|
||||||
|
|
||||||
return interaction.editReply(`Edited knowledgebase entry \`${existingItem.name}\`.`);
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
export default new EditKnowledgebaseItemModal();
|
||||||
Reference in New Issue
Block a user