Skip to content

Commit

Permalink
🆕 Add setData and edit function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashu11-A committed Jun 14, 2024
1 parent 64405ce commit 0f6c31c
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions plugins/tickets/src/class/ClaimBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ButtonBuilder } from "@/discord/base/CustomIntetaction.js"
import { Error } from "@/discord/base/CustomResponse.js"
import Claim from "@/entity/Claim.entry.js"
import Config, { Roles } from "@/entity/Config.entry.js"
import Ticket from "@/entity/Ticket.entry.js"
import { ActionDrawer } from "@/functions/actionDrawer.js"
import { claimDB, configDB, ticketDB } from "@/functions/database.js"
import { ActionRowBuilder, ButtonInteraction, ButtonStyle, ChannelType, codeBlock, CommandInteraction, EmbedBuilder, Message, ModalSubmitInteraction, OverwriteResolvable, PermissionsBitField, StringSelectMenuInteraction } from "discord.js"
Expand All @@ -17,6 +18,7 @@ type Interaction = CommandInteraction<'cached'> | ModalSubmitInteraction<'cached
export class ClaimBuilder {
private readonly interaction: Interaction
private options!: ClaimOptions
private ticketData: Ticket | undefined
public embed!: EmbedBuilder | undefined
public buttons!: ActionRowBuilder<ButtonBuilder>[] | undefined

Expand All @@ -29,6 +31,7 @@ export class ClaimBuilder {
}

setTicketId(ticketId: number) { this.options.ticketId = ticketId ; return this }
setData (ticket: Ticket) { this.ticketData = ticket; return this }

private permissions (roles: Roles[]): OverwriteResolvable[] {
const permissionOverwrites: OverwriteResolvable[] = []
Expand All @@ -55,8 +58,8 @@ export class ClaimBuilder {

async render() {
const { guild } = this.interaction
const ticketData = await ticketDB.findOne({ where: { id: this.options.ticketId } })
if (ticketData === null) { await new Error({ element: 'ticket', interaction: this.interaction }).notFound({ type: 'Database' }).reply(); return }
const ticketData = this.ticketData !== null ? this.ticketData : await ticketDB.findOne({ where: { id: this.options.ticketId } })
if (ticketData === null || ticketData === undefined) throw await new Error({ element: 'ticket', interaction: this.interaction }).notFound({ type: 'Database' }).reply()
const { team, ownerId, category: { emoji, title }, description, createAt } = ticketData
const user = (await guild.members.fetch()).find((user) => user.id === ownerId)

Expand Down Expand Up @@ -146,6 +149,24 @@ export class ClaimBuilder {
return
}

async edit ({ messageId }: { messageId: string }) {
const claimData = await claimDB.findOne({ where: { messageId } })
if (claimData === null) throw await new Error({ element: 'claim', interaction: this.interaction }).notFound({ type: 'Database' }).reply()

const channel = await this.interaction.client.channels.fetch(claimData.channelId).catch(async() => null)
if (channel === null) throw await new Error({ element: 'do claim', interaction: this.interaction }).notFound({ type: "Channel" }).reply()
if (!channel.isTextBased()) throw await new Error({ element: 'concluir a ação, pois o channel não é um TextBased', interaction: this.interaction }).notPossible().reply()

if (this.embed === undefined || this.buttons) await this.render()

const message = await channel.messages.fetch(claimData.messageId).catch(() => null)
if (message === null) throw await new Error({ element: 'message do claim', interaction: this.interaction }).notFound({ type: 'Database' }).reply()
await message.edit({
embeds: [this.embed as EmbedBuilder],
components: this.buttons
})
}

async delete (id: number) {
const claimData = await claimDB.findOne({ where: { id } })
if (claimData === null) return await new Error({ element: 'claim', interaction: this.interaction }).notFound({ type: 'Database' }).reply()
Expand Down

0 comments on commit 0f6c31c

Please sign in to comment.