mirror of
https://github.com/nx-bat/discordbot-ng.git
synced 2026-11-11 07:13:37 -05:00
(modals): Implement add-knowledgebase-item-modal modal.
This commit is contained in:
@@ -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);
|
||||||
|
|
||||||
|
try {
|
||||||
const name = interaction.fields.getTextInputValue('name');
|
const name = interaction.fields.getTextInputValue('name');
|
||||||
const content = interaction.fields.getTextInputValue('content');
|
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!, {
|
||||||
|
|
||||||
if (existingItem) return interaction.editReply(`Knowledgebase item ${name} already exists!`);
|
|
||||||
|
|
||||||
logCustomEvent(interaction.guild!, {
|
|
||||||
title: 'Knowledgebase Item Added',
|
title: 'Knowledgebase Item Added',
|
||||||
description: null,
|
description: null,
|
||||||
color: 0x00FF00,
|
color: 0x00FF00,
|
||||||
timestamp: new Date(),
|
timestamp: new Date(),
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{ name: 'User', value: `${interaction.user} (${interaction.user.id})`, inline: false },
|
||||||
name: 'User',
|
{ name: 'Name', value: name, inline: false },
|
||||||
value: `<@${interaction.user.id}>\n${interaction.user.username}`,
|
{ name: 'Content', value: content, inline: false },
|
||||||
inline: true
|
],
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'Name',
|
|
||||||
value: name,
|
|
||||||
inline: true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'Content',
|
|
||||||
value: content,
|
|
||||||
inline: true
|
|
||||||
}
|
|
||||||
]
|
|
||||||
});
|
});
|
||||||
|
|
||||||
await Database.addToKnowledgebase(interaction.guild.id, name, content);
|
await Database.addToKnowledgebase(interaction.guildId, name, content);
|
||||||
|
await interaction.editReply(`\`${name}\` has been added to the knowledgebase.`);
|
||||||
return interaction.editReply(`Entry \`${name}\` added to knowledgebase.`);
|
} catch (error: Error | any) {
|
||||||
|
await interaction.editReply({ content: error.message }).catch(() => {});
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default new AddKnowledgebaseItemModal();
|
||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user