Skip to content

Commit

Permalink
🛠️ Implement template relation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashu11-A committed Jun 7, 2024
1 parent 72a160c commit d6d1a8c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
4 changes: 3 additions & 1 deletion plugins/tickets/src/class/TicketBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class TicketBuilder {
public embed!: EmbedBuilder | undefined
public buttons!: ActionRowBuilder<ButtonBuilder>[] | undefined
private user!: User
private templateId: number | undefined
private readonly interaction: Interaction
constructor ({ interaction }: { interaction: Interaction }) {
this.interaction = interaction
Expand All @@ -43,6 +44,7 @@ export class TicketBuilder {
}

setOwner (id: string) { this.options.ownerId = id; return this }
setTemplateId (id: number) { this.templateId = id; return this}
setTitle (content: string) { this.options.title = content; return this }
setDescription(content: string) { this.options.description = content; return this }
setClosed(isClosed: boolean) { this.options.closed = isClosed ?? false; return this }
Expand Down Expand Up @@ -183,7 +185,7 @@ export class TicketBuilder {
messageId: messageMain.id,
})

const ticketData = await ticket.create({ ...this.options, guild: { id: this.interaction.guildId } })
const ticketData = await ticket.create({ ...this.options, guild: { id: this.interaction.guildId }, template: { id: this.templateId } })
const result = await ticket.save(ticketData) as Ticket

if (result === null || result === undefined) {
Expand Down
22 changes: 15 additions & 7 deletions plugins/tickets/src/discord/components/Template/OpenTicket.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { TicketBuilder } from "@/class/TicketBuilder";
import { Database } from "@/controller/database";
import { Component } from "@/discord/base";
import { ButtonInteraction } from "discord.js";
import { Error } from "@/discord/base/CustomResponse";
import Template from "@/entity/Template.entry";

const template = new Database<Template>({ table: 'Template' })

new Component({
customId: 'Open',
type: 'Button',
cache: 'cached',
async run(interaction) {
const ticket = new TicketBuilder({ interaction: interaction as ButtonInteraction<'cached'> })
const result = await ticket.setOwner(interaction.user.id).render().create()
if (result === null || result === undefined) return

// await ticket.delete({ id: result.id })
},
if (!interaction.inCachedGuild()) return
await interaction.deferReply({ ephemeral: true })

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

const ticket = new TicketBuilder({ interaction: interaction })
await ticket.setOwner(user.id).setTemplateId(templateData.id).render().create()
}
})
6 changes: 5 additions & 1 deletion plugins/tickets/src/entity/Template.entry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { APIEmbed } from "discord.js";
import { BaseEntity, Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { BaseEntity, Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import Guild from "./Guild.entry";
import Ticket from "./Ticket.entry";

export interface Properties {
[key: string]: boolean | string
Expand Down Expand Up @@ -31,6 +32,9 @@ export default class Template extends BaseEntity {
@ManyToOne(() => Guild, (guild) => guild.templates)
guild!: Guild

@OneToMany(() => Ticket, (ticket) => ticket.template)
tickets!: Template[]

@Column({ type: 'text' })
messageId!: string

Expand Down
4 changes: 4 additions & 0 deletions plugins/tickets/src/entity/Ticket.entry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BaseEntity, Column, CreateDateColumn, Entity, JoinColumn, ManyToOne, OneToOne, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm'
import Guild from './Guild.entry'
import Claim from './Claim.entry'
import Template from './Template.entry'

export interface User {
name: string
Expand Down Expand Up @@ -71,6 +72,9 @@ export default class Ticket extends BaseEntity {
@ManyToOne(() => Guild, (guild) => guild.tickets)
guild!: Guild

@ManyToOne(() => Template, (template) => template.tickets)
template!: Template

@Column()
ownerId!: string

Expand Down

0 comments on commit d6d1a8c

Please sign in to comment.