Skip to content

Commit

Permalink
📉 Reduce unnecessary requests to the database
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashu11-A committed Jun 14, 2024
1 parent 2758708 commit 9ce1c99
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
7 changes: 4 additions & 3 deletions plugins/tickets/src/discord/events/messageCreate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { TicketBuilder } from "@/class/TicketBuilder.js"
import { Database } from "@/controller/database.js"
import { Event } from "../base/Event.js"
import Ticket from "@/entity/Ticket.entry.js"
import { TicketBuilder } from "@/class/TicketBuilder.js"
import { MessageFlagsBitField } from "discord.js"
import { Event } from "../base/Event.js"

const ticket = new Database<Ticket>({ table: 'Ticket' })

Expand All @@ -11,7 +12,7 @@ const ticket = new Database<Ticket>({ table: 'Ticket' })
new Event({
name: 'messageCreate',
async run(message) {
if (!message.inGuild()) return
if (message.flags.has(MessageFlagsBitField.Flags.Ephemeral) || !message.inGuild()) return
const { channelId, author, content, id, client } = message
if (author.id === client.user.id) return
const ticketData = await ticket.findOne({ where: { channelId: channelId } })
Expand Down
16 changes: 8 additions & 8 deletions plugins/tickets/src/discord/events/messageDelete.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ClaimBuilder } from "@/class/ClaimBuilder.js"
import { TicketBuilder } from "@/class/TicketBuilder.js"
import { Event } from "../base/Event.js"
import { AuditLogEvent, EmbedBuilder, Message } from "discord.js"
import { Database } from "@/controller/database.js"
import Ticket from "@/entity/Ticket.entry.js"
import Claim from "@/entity/Claim.entry.js"
import { ClaimBuilder } from "@/class/ClaimBuilder.js"
import Ticket from "@/entity/Ticket.entry.js"
import { AuditLogEvent, EmbedBuilder, Message, MessageFlagsBitField } from "discord.js"
import { Event } from "../base/Event.js"

const ticket = new Database<Ticket>({ table: 'Ticket' })
const claim = new Database<Claim>({ table: 'Claim' })
Expand All @@ -15,7 +15,7 @@ const claim = new Database<Claim>({ table: 'Claim' })
new Event({
name: 'messageDelete',
async run(message) {
if (!(message instanceof Message) || !message.inGuild()) return
if (!(message instanceof Message) || !message.inGuild() || message.author?.bot || message.flags.has(MessageFlagsBitField.Flags.Ephemeral)) return
const { channelId, id } = message
const ticketData = await ticket.findOne({ where: { channelId } })
if (ticketData === null) return
Expand All @@ -35,12 +35,12 @@ new Event({
})

/**
* Se a mensagem inicial do ticket for apagada )isso irá recriar ela).
* Se a mensagem inicial do ticket for apagada (isso irá recriar ela).
*/
new Event({
name: 'messageDelete',
async run(message) {
if (!message.inGuild() || await message.fetch().catch(() => null) !== null) return
if (!message.author?.bot || message.flags.has(MessageFlagsBitField.Flags.Ephemeral) || !message.inGuild() || await message.fetch().catch(() => null) !== null) return
const { id } = message
const ticketData = await ticket.findOne({ where: { messageId: id } })
if (ticketData === null) return
Expand Down Expand Up @@ -73,7 +73,7 @@ new Event({
new Event({
name: 'messageDelete',
async run(message) {
if (!message.inGuild()) return
if (!message.author?.bot || message.flags.has(MessageFlagsBitField.Flags.Ephemeral) || !message.inGuild()) return
const { id } = message
const claimData = await claim.findOne({ where: { messageId: id }, relations: { ticket: true } })
if (claimData === null || claimData?.ticket?.id === undefined) return
Expand Down

0 comments on commit 9ce1c99

Please sign in to comment.