-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🛑 Finalizing the ticket type selection system
- Loading branch information
Showing
6 changed files
with
212 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 63 additions & 4 deletions
67
plugins/tickets/src/discord/components/Template/ButtonOpen.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
plugins/tickets/src/discord/components/Template/ModalOpen.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
plugins/tickets/src/discord/components/Template/SelectEditMenu.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
plugins/tickets/src/discord/components/Template/SelectMenu.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
53
plugins/tickets/src/discord/components/Template/SelectOpen.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
}, | ||
}) |