Skip to content

Commit

Permalink
✨ Implement AddCategory and AddSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashu11-A committed May 24, 2024
1 parent 85aadc1 commit 82301f5
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 3 deletions.
6 changes: 3 additions & 3 deletions plugins/tickets/src/class/Ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ export class Ticket {
emoji: { name: '📄' }
}),
new ButtonBuilder({
customId: 'EmbedCategory',
label: 'Panel Category',
emoji: { name: '🖥️' },
customId: 'AddCategory',
label: 'Add Categoria',
emoji: { name: '🔖' },
style: ButtonStyle.Secondary
}),
new ButtonBuilder({
Expand Down
173 changes: 173 additions & 0 deletions plugins/tickets/src/discord/components/Template/AddElements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { Database } from "@/controller/database";
import { Component } from "@/discord/base";
import { ModalBuilder } from "@/discord/base/CustomIntetaction";
import Template from "@/entity/Template.entry";
import { ActionRowBuilder, APITextInputComponent, ComponentType, EmbedBuilder, TextInputBuilder } from "discord.js";

const template = new Database<Template>({ table: 'Template' })
const notFound = new EmbedBuilder({
title: '❌ Não encontrei o template no database!'
}).setColor('Red')

const elementsSelect: 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: 'AddSelect',
type: "Button",
async run(interaction) {
const modal = new ModalBuilder({
title: 'Adicionar opções do SelectMenu',
customId: 'AddSelect',
})

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

await interaction.showModal(modal)
},
})

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

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

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

await template.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)
}
})

const elementsCategory: APITextInputComponent[] = [
{
label: '❓| Qual será o Título?',
placeholder: 'Ex: Parceria',
style: 1,
max_length: 256,
custom_id: 'title',
type: ComponentType.TextInput
},
{
label: '❓| Qual será a Descrição?',
placeholder: 'Ex: Quero me tornar um parceiro.',
style: 1,
max_length: 256,
custom_id: 'description',
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',
type: "Button",
async run(interaction) {
const modal = new ModalBuilder({
title: 'Adicionar Categorias de Ticket',
customId: 'AddCategory',
})

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 description = interaction.fields.getTextInputValue('description')
const templateData = await template.findOne({ where: { messageId: interaction.message?.id } })

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

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

await template.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)
}
})
38 changes: 38 additions & 0 deletions plugins/tickets/src/entity/Template.entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ export enum TypeTemplate {
Modal = 'modal'
}

interface Select {
title: string
emoji: string
}

interface Category {
title: string
description: string
emoji: string
}

@Entity({ name: 'tickets_templates' })
export default class Template extends BaseEntity {
@PrimaryGeneratedColumn()
Expand All @@ -30,6 +41,33 @@ export default class Template extends BaseEntity {
type!: TypeTemplate

@Column('json', {
nullable: true,
transformer: {
to(value: string): string {
return JSON.stringify(value)
},
from(value: string): Select[] {
return JSON.parse(value)
},
}
})
selects!: Select[]

@Column('json', {
nullable: true,
transformer: {
to(value: string): string {
return JSON.stringify(value)
},
from(value: string): Category[] {
return JSON.parse(value)
},
}
})
categories!: Category[]

@Column('json', {
nullable: true,
transformer: {
to(value: string): string {
return JSON.stringify(value)
Expand Down

0 comments on commit 82301f5

Please sign in to comment.