Skip to content

Commit

Permalink
FIX: solves the issue aws-amplify#480 deleting the question on discord
Browse files Browse the repository at this point in the history
  • Loading branch information
Raghav Gupta committed Aug 28, 2024
1 parent a24f14a commit e52feda
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions apps/discord-bot-frontend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
42 changes: 42 additions & 0 deletions apps/discord-bot-frontend/src/lib/discord/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e52feda

Please sign in to comment.