From e52fedab7998d121558c1049e6d750d2636bdb53 Mon Sep 17 00:00:00 2001 From: Raghav Gupta Date: Wed, 28 Aug 2024 12:37:28 -0700 Subject: [PATCH 1/2] FIX: solves the issue #480 deleting the question on discord --- .../discord-bot-frontend/prisma/schema.prisma | 4 +- .../src/lib/discord/client.ts | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/apps/discord-bot-frontend/prisma/schema.prisma b/apps/discord-bot-frontend/prisma/schema.prisma index d05ab063..e8fd13c3 100644 --- a/apps/discord-bot-frontend/prisma/schema.prisma +++ b/apps/discord-bot-frontend/prisma/schema.prisma @@ -51,9 +51,9 @@ model QuestionTag { model Participation { id String @id @default(cuid()) - question Question @relation(fields: [questionId], references: [id]) + question Question @relation(fields: [questionId], references: [id], onDelete: Cascade) questionId String - participant DiscordUser @relation(fields: [participantId], references: [id]) + participant DiscordUser @relation(fields: [participantId], references: [id], onDelete: NoAction) // @TODO - track role at the time of participation, accounts for users that _leave_ "staff" participantId String participantRoles DiscordRole[] diff --git a/apps/discord-bot-frontend/src/lib/discord/client.ts b/apps/discord-bot-frontend/src/lib/discord/client.ts index b1115163..aead76f6 100644 --- a/apps/discord-bot-frontend/src/lib/discord/client.ts +++ b/apps/discord-bot-frontend/src/lib/discord/client.ts @@ -424,6 +424,48 @@ client.on(Events.ThreadUpdate, async (oldThread, newThread) => { console.debug('[client:events:ThreadUpdate] finished') } }) +client.on(Events.ThreadDelete, async(thread) => { + console.debug('[client:events: ThreadDelete] Thread Deleted started'); + + if(!thread.id){ + console.error("No thread id found"); return; + } + + try { + + //Updating the flag but in this logic we will need to change the database field. Can be done later + + // if(question){ + // await prisma.question.update({ + // where: { + // id: question.id, + // }, + // data: { + // isDeleted: true, + // }, + // }); + // } + + + // Alternate logic to deleting the question completely from the database. Easier for us to do this because we don't + // have to update the database field. This is the same as the update above. + const deletedQuestion = await prisma.question.delete({ + where: { + threadId: thread.id, + }, + }); + if(deletedQuestion) { + console.log(`Deleted question from the database`); + } + else{ + console.error(`Unable to find question in the database`); + } + } catch (error) { + console.error('Unable to delete question', error); + } +}) + + export function createBot(token = process.env.DISCORD_BOT_TOKEN) { return client.login(token) From de9a03a0e011790d14fd386f21ce78eb16630b9d Mon Sep 17 00:00:00 2001 From: Raghav Gupta Date: Fri, 6 Sep 2024 09:57:00 -0700 Subject: [PATCH 2/2] Solves formatting issues. --- .../src/lib/discord/client.ts | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/apps/discord-bot-frontend/src/lib/discord/client.ts b/apps/discord-bot-frontend/src/lib/discord/client.ts index aead76f6..10146912 100644 --- a/apps/discord-bot-frontend/src/lib/discord/client.ts +++ b/apps/discord-bot-frontend/src/lib/discord/client.ts @@ -178,7 +178,7 @@ client.on(Events.MessageCreate, async (message: Message) => { embed.setColor('#ff9900') // TODO: add more info on /thread command embed.setDescription( - "Hey there! :wave: we've created a thread for you!\n\nUse `/thread rename` to change the title.\n\nUse `/thread solved` to mark this thread as solved." + "Hey there! :wave: we've created a thread for you!\n\nUse `/thread rename` to change the title.\n\nUse `/thread solved` to mark this thread as solved.", ) thread.send({ embeds: [embed] }) } @@ -198,7 +198,7 @@ client.on(Events.InteractionCreate, async (interaction: Interaction) => { } console.log( - `Handling command "${command?.name}" for ${interaction.user.username}#${interaction.user.discriminator}` + `Handling command "${command?.name}" for ${interaction.user.username}#${interaction.user.discriminator}`, ) await command.handle(interaction) @@ -206,7 +206,7 @@ client.on(Events.InteractionCreate, async (interaction: Interaction) => { client.on('rateLimit', (info) => { console.log( - `Rate limit hit ${info.timeout ? info.timeout : 'Unknown timeout '}` + `Rate limit hit ${info.timeout ? info.timeout : 'Unknown timeout '}`, ) }) /** @@ -327,7 +327,7 @@ client.on(Events.ThreadUpdate, async (oldThread, newThread) => { .map(({ id, name }) => ({ id, name })) // get the tags that were removed tagsRemoved = oldThread.appliedTags.filter( - (id) => !appliedTagIds.includes(id) + (id) => !appliedTagIds.includes(id), ) } @@ -424,17 +424,17 @@ client.on(Events.ThreadUpdate, async (oldThread, newThread) => { console.debug('[client:events:ThreadUpdate] finished') } }) -client.on(Events.ThreadDelete, async(thread) => { - console.debug('[client:events: ThreadDelete] Thread Deleted started'); +client.on(Events.ThreadDelete, async (thread) => { + console.debug('[client:events: ThreadDelete] Thread Deleted started') - if(!thread.id){ - console.error("No thread id found"); return; + if (!thread.id) { + console.error('No thread id found') + return } try { - //Updating the flag but in this logic we will need to change the database field. Can be done later - + // if(question){ // await prisma.question.update({ // where: { @@ -446,27 +446,23 @@ client.on(Events.ThreadDelete, async(thread) => { // }); // } - - // Alternate logic to deleting the question completely from the database. Easier for us to do this because we don't + // Alternate logic to deleting the question completely from the database. Easier for us to do this because we don't // have to update the database field. This is the same as the update above. - const deletedQuestion = await prisma.question.delete({ - where: { - threadId: thread.id, - }, - }); - if(deletedQuestion) { - console.log(`Deleted question from the database`); - } - else{ - console.error(`Unable to find question in the database`); - } + const deletedQuestion = await prisma.question.delete({ + where: { + threadId: thread.id, + }, + }) + if (deletedQuestion) { + console.log(`Deleted question from the database`) + } else { + console.error(`Unable to find question in the database`) + } } catch (error) { - console.error('Unable to delete question', error); + console.error('Unable to delete question', error) } }) - - export function createBot(token = process.env.DISCORD_BOT_TOKEN) { return client.login(token) }