-
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.
- Loading branch information
Showing
1 changed file
with
36 additions
and
15 deletions.
There are no files selected for viewing
51 changes: 36 additions & 15 deletions
51
plugins/tickets/src/discord/components/Ticket/ButtonSwitch.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,44 +1,65 @@ | ||
import { ClaimBuilder } from "@/class/ClaimBuilder.js"; | ||
import { TicketBuilder } from "@/class/TicketBuilder.js"; | ||
import { Database } from "@/controller/database.js"; | ||
import { Error } from "@/discord/base/CustomResponse.js"; | ||
import { Component } from "@/discord/base/index.js"; | ||
import Ticket from "@/entity/Ticket.entry.js"; | ||
import { claimDB, ticketDB } from "@/functions/database.js"; | ||
import { EmbedBuilder } from "discord.js"; | ||
const ticket = new Database<Ticket>({ table: 'Ticket' }) | ||
|
||
new Component({ | ||
customId: 'Switch', | ||
type: "Button", | ||
async run(interaction) { | ||
const { guild, channelId, channel, user } = interaction | ||
const { guild, channelId, channel, user, message } = interaction | ||
if (!interaction.inCachedGuild()) return | ||
interaction.deferReply({ ephemeral: true }) | ||
if (guild === null) return await new Error({ element: 'executar essa ação pois você teve estar em uma Guilda!', interaction }).notPossible().reply() | ||
|
||
const ticketData = (await ticket.findOne({ where: { channelId } })) | ||
if (ticketData === null) return await new Error({ element: `o ticket ${channelId}`, interaction }).notFound({ type: 'Database' }).reply() | ||
let ticketData: Ticket | null | ||
let claimId: string | undefined | ||
let ephemeral = false | ||
|
||
// Caso a interação venha do embed de claim, deve ocorrer este tipo de consulta | ||
ticketData = await ticketDB.findOne({ where: { channelId }, relations: { claim: true } }) | ||
if (ticketData === null) { | ||
ephemeral = true | ||
const claimData = await claimDB.findOne({ where: { messageId: message.id }, relations: { ticket: true } }) | ||
ticketData = claimData?.ticket ?? null | ||
claimId = claimData?.messageId | ||
} else { | ||
claimId = ticketData.claim?.messageId | ||
} | ||
if (ticketData === null) return await new Error({ element: `o ticket ${channelId}`, interaction }).notFound({ type: 'Database' }).reply() | ||
if (claimId === undefined) return await new Error({ element: 'claim', interaction }).notFound({ type: "Database" }).reply() | ||
|
||
const isClosed = !ticketData.closed | ||
const builder = new TicketBuilder({ interaction }) | ||
const claim = new ClaimBuilder({ interaction }) | ||
|
||
await builder | ||
const embed = new EmbedBuilder({ | ||
title: isClosed ? '🔒 Ticket fechado!' : '🔓 Ticket aberto!', | ||
footer: { text: `Por: ${user.displayName} | Id: ${user.id}`, iconURL: user?.avatarURL() ?? undefined } | ||
}).setColor(isClosed ? 'Red' : 'Green') | ||
|
||
const ticket = await (await builder | ||
.setData(ticketData) | ||
.setClosed(!ticketData.closed) | ||
.addEvent({ | ||
user: { id: user.id, name: user.displayName }, | ||
message: `Usuário ${user.displayName}(${user.id}), fechou o ticket!`, | ||
date: new Date(), | ||
}) | ||
.update() | ||
.update()) | ||
?.send([embed]) | ||
|
||
|
||
await claim.setData(ticket?.options as Ticket).edit({ messageId: claimId }) | ||
|
||
if (ephemeral) { | ||
interaction.editReply({ embeds: [embed] }) | ||
return | ||
} | ||
await interaction.deleteReply() | ||
await channel?.send({ | ||
embeds: [ | ||
new EmbedBuilder({ | ||
title: isClosed ? '🔒 Ticket fechado!' : '🔓 Ticket aberto!', | ||
footer: { text: `Por: ${user.displayName} | Id: ${user.id}`, iconURL: user?.avatarURL() ?? undefined } | ||
}).setColor(isClosed ? 'Red' : 'Green') | ||
] | ||
}) | ||
await channel?.send({ embeds: [embed] }) | ||
}, | ||
}) |