Skip to content

Commit

Permalink
🛑 Finalizing the ticket type selection system
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashu11-A committed Jun 15, 2024
1 parent 4f77dc2 commit fbaf176
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 93 deletions.
96 changes: 7 additions & 89 deletions plugins/tickets/src/discord/components/Template/AddElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,97 +107,15 @@ new Component({
})

new Component({
customId: 'EditSelectMenu',
type: 'StringSelect',
async run(interaction) {
// const buttonBuilder = new TemplateButtonBuilder()

const templateData = await templateDb.findOne({ where: { messageId: interaction.message.id } })
if (templateData === null) return

// const components = buttonBuilder
// .setMode('debug')
// .setProperties(templateData.properties)
// .setSelects(templateData.selects)
// .setType(templateData.type)
// .render()

},
})

const elementsCategory: APITextInputComponent[] = [
{
label: '❓| Qual será o Nome?',
placeholder: 'Ex: Parceria',
style: 1,
max_length: 25,
custom_id: "title",
type: ComponentType.TextInput
},
{
label: '❓| Qual será o Emoji? (somente um)',
placeholder: 'Ex: 🎟️🎫💰🎲💵🗂️.',
value: '🎟️',
style: 1,
max_length: 10,
custom_id: "emoji",
type: ComponentType.TextInput
}
]

new Component({
customId: 'AddCategory',
customId: 'Category',
type: "Button",
async run(interaction) {
const modal = new ModalBuilder({
title: 'Adicionar Categorias de Ticket',
customId: 'AddCategory',
await interaction.reply({
ephemeral: true,
embeds: [new EmbedBuilder({
title: 'Recurso movido!',
description: 'Use os comandos:\n`/ticket category add`\n`/ticket category rem`'
}).setColor('Orange')]
})

for (const element of elementsCategory) {
modal.addComponents(new ActionRowBuilder<TextInputBuilder>().addComponents(new TextInputBuilder(element)))
}

await interaction.showModal(modal)
},
})

new Component({
customId: 'AddCategory',
type: "Modal",
async run(interaction) {
const title = interaction.fields.getTextInputValue('title')
const emoji = interaction.fields.getTextInputValue('emoji')
const templateData = await templateDb.findOne({ where: { messageId: interaction.message?.id } })

if (templateData === null) {
await interaction.reply({ embeds: [notFound] })
return
}

templateData.categories = [ ...(templateData.categories ?? []), { emoji, title }]

await templateDb.save(templateData)
.then(async () => {
await interaction.reply({
ephemeral: true,
embeds: [
new EmbedBuilder({
title: '✅ Informações salvas com sucesso!'
}).setColor('Green')
]
})
})
.catch(async () => {
await interaction.editReply({
embeds: [
new EmbedBuilder({
title: '❌ Ocorreu um erro ao tentar salvar as informações no database'
}).setColor('Red')
]
})
})

setTimeout(() => interaction.deleteReply(), 2000)
}
})
67 changes: 63 additions & 4 deletions plugins/tickets/src/discord/components/Template/ButtonOpen.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,76 @@
import { TicketBuilder } from "@/class/TicketBuilder.js";
import { ModalBuilder, StringSelectMenuBuilder } from "@/discord/base/CustomIntetaction.js";
import { Error } from "@/discord/base/CustomResponse.js";
import { Component } from "@/discord/base/index.js";
import { TypeTemplate } from "@/entity/Template.entry.js";
import { ActionDrawer } from "@/functions/actionDrawer.js";
import { templateDB } from "@/functions/database.js";
import { SelectMenuComponentOptionData, TextInputBuilder, TextInputStyle } from "discord.js";

new Component({
customId: 'Open',
type: 'Button',
cache: 'cached',
async run(interaction) {
if (!interaction.inCachedGuild()) return
await interaction.deferReply({ ephemeral: true })

const { user } = interaction
const { user, message } = interaction
const ticket = new TicketBuilder({ interaction })

const templateData = await templateDB.findOne({ where: { messageId: message.id } })
if (templateData === null) throw await new Error({ element: 'template', interaction }).notFound({ type: 'Database' }).reply()

if (templateData.type === TypeTemplate.Button) await interaction.deferReply({ ephemeral: true })

if ((templateData.categories ?? []).length > 0) {
switch (templateData.type) {
case TypeTemplate.Button:
case TypeTemplate.Modal:
const options: SelectMenuComponentOptionData[] = []

for (const [index, category] of Object.entries(templateData.categories)) {
options.push({ label: category.title, emoji: category.emoji, value: `${message.id}_${index}` })
}
const select = ActionDrawer<StringSelectMenuBuilder>([new StringSelectMenuBuilder({
customId: 'SelectOpen',
placeholder: 'Selecione a categoria do seu Ticket',
minValues: 1,
maxValues: 1,
options
})], 1)
await interaction.editReply({ components: select })
}
} else {
switch (templateData.type) {
case TypeTemplate.Button: {
await ticket.setOwner(user.id).render().create()
break
}
case TypeTemplate.Modal: {
const modal = new ModalBuilder({ customId: 'ModalOpen', title: 'Abrir novo ticket' })
const rows = ActionDrawer<TextInputBuilder>([
new TextInputBuilder({
custom_id: 'title',
label: 'Qual é o motivo do ticket?',
required: true,
max_length: 150,
style: TextInputStyle.Short,
placeholder: 'Dúvida... Denúncia... Pedido...'
}),
new TextInputBuilder({
customId: 'description',
label: 'Qual a descrição?',
required: true,
maxLength: 255,
style: TextInputStyle.Paragraph,
placeholder: 'Queria saber mais informações sobre...'
})
], 1)

await ticket.setOwner(user.id).render().create()
modal.setComponents(rows)
await interaction.showModal(modal)
break
}
}
}
}
})
23 changes: 23 additions & 0 deletions plugins/tickets/src/discord/components/Template/ModalOpen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TicketBuilder } from "@/class/TicketBuilder.js";
import { Component } from "@/discord/base/Components.js";
import { userSelect } from "./SelectOpen.js";

new Component({
customId: 'ModalOpen',
type: "Modal",
async run(interaction) {
if (!interaction.inCachedGuild()) return
const { fields, user } = interaction
const title = fields.fields.find((field) => field.customId === 'title')?.value as string | undefined
const description = fields.fields.find((field) => field.customId === 'description')?.value as string

const cache = userSelect.get(user.id)

const builder = new TicketBuilder({ interaction })
if (title !== undefined) builder.setTitle(title)
if (cache !== undefined) { builder.setCategory(cache.category); builder.setTemplateId(cache.templateId) }
builder.setOwner(user.id)
builder.setDescription(description)
await builder.create()
},
})
27 changes: 27 additions & 0 deletions plugins/tickets/src/discord/components/Template/SelectEditMenu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { TemplateBuilder } from "@/class/TemplateBuilder.js";
import { Component } from "@/discord/base/Components.js";
import { Error } from "@/discord/base/CustomResponse.js";
import { templateDB } from "@/functions/database.js";
import { PermissionsBitField } from "discord.js";

new Component({
customId: 'EditSelectMenu',
type: "StringSelect",
async run(interaction) {
if (!interaction.inCachedGuild()) return
if (!interaction.memberPermissions.has(PermissionsBitField.Flags.Administrator)) throw await new Error({ element: 'você', interaction }).forbidden().reply()

const { message, values } = interaction
const position = Number(values[0])

const templateData = await templateDB.findOne({ where: { messageId: message.id } })
if (templateData === null) throw await new Error({ element: 'template', interaction }).notFound({ type: 'Database' }).reply()

templateData.selects.splice(position, 1)

await new TemplateBuilder({ interaction })
.setData(templateData)
.setMode('debug')
.edit({ messageId: message.id })
}
})
39 changes: 39 additions & 0 deletions plugins/tickets/src/discord/components/Template/SelectMenu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { TemplateBuilder } from "@/class/TemplateBuilder.js";
import { TicketBuilder } from "@/class/TicketBuilder.js";
import { Component } from "@/discord/base/Components.js";
import { Error } from "@/discord/base/CustomResponse.js";
import { templateDB } from "@/functions/database.js";
import { PermissionsBitField } from "discord.js";

new Component({
customId: 'SelectMenu',
type: 'StringSelect',
async run(interaction) {
if (!interaction.inCachedGuild()) return
await interaction.deferReply({ ephemeral: true })
const { values, message, user } = interaction
const select = values[0]

if (select === 'config') {
if (!(interaction.memberPermissions.has(PermissionsBitField.Flags.Administrator ?? false))){
throw await new Error({ element: 'você', interaction }).forbidden().reply()
}
await new TemplateBuilder({ interaction }).setMode('debug').edit({ messageId: message.id })
await interaction.deleteReply()
return
}

const templateData = await templateDB.findOne({ where: { messageId: message.id } })
if (templateData === null) throw await new Error({ element: 'template', interaction }).notFound({ type: "Database" }).reply()

const typeTicket = templateData?.selects[Number(select)]
if (typeTicket === undefined) throw await new Error({ element: 'select', interaction }).notFound({ type: "Database" }).reply()

await new TicketBuilder({ interaction })
.setOwner(user.id)
.setTitle(typeTicket.title)
.setDescription(typeTicket.description)
.setCategory({ emoji: typeTicket.emoji, title: typeTicket.title })
.create()
},
})
53 changes: 53 additions & 0 deletions plugins/tickets/src/discord/components/Template/SelectOpen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { TicketBuilder } from "@/class/TicketBuilder.js";
import { Component } from "@/discord/base/Components.js";
import { ModalBuilder } from "@/discord/base/CustomIntetaction.js";
import { Error } from "@/discord/base/CustomResponse.js";
import { Category, TypeTemplate } from "@/entity/Template.entry.js";
import { ActionDrawer } from "@/functions/actionDrawer.js";
import { templateDB } from "@/functions/database.js";
import { TextInputBuilder, TextInputStyle } from "discord.js";

export const userSelect = new Map<string, { category: Category, templateId: number }>()

new Component({
customId: 'SelectOpen',
type: 'StringSelect',
async run(interaction) {
if (!interaction.inCachedGuild()) return
const { values, user } = interaction
const [messageId, index] = values[0].split('_') as [string, string]

const templateData = await templateDB.findOne({ where: { messageId } })
if (templateData === null) throw await new Error({ element: 'template', interaction }).notFound({ type: "Database" }).reply()

const category = templateData.categories[Number(index)]
if (category === undefined) throw await new Error({ element: 'categoria', interaction }).notFound({ type: "Database" }).reply()

switch (templateData.type) {
case TypeTemplate.Button:
await new TicketBuilder({ interaction })
.setOwner(user.id)
.setTemplateId(templateData.id)
.setTitle(category.title)
.setCategory({ emoji: category.emoji, title: category.title })
.create()
break
case TypeTemplate.Modal:
const modal = new ModalBuilder({ customId: 'ModalOpen', title: 'Abrir novo ticket' })
const rows = ActionDrawer<TextInputBuilder>([
new TextInputBuilder({
customId: 'description',
label: 'Qual a descrição?',
required: true,
maxLength: 255,
style: TextInputStyle.Paragraph,
placeholder: 'Queria saber mais informações sobre...'
})
], 1)
userSelect.set(user.id, { category, templateId: templateData.id })
await interaction.showModal(modal.setComponents(rows))
break
}

},
})

0 comments on commit fbaf176

Please sign in to comment.