Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: solves the issue #480 deleting the question on discord #570

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 42 additions & 4 deletions apps/discord-bot-frontend/src/lib/discord/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] })
}
Expand All @@ -198,15 +198,15 @@ 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)
})

client.on('rateLimit', (info) => {
console.log(
`Rate limit hit ${info.timeout ? info.timeout : 'Unknown timeout '}`
`Rate limit hit ${info.timeout ? info.timeout : 'Unknown timeout '}`,
)
})
/**
Expand Down Expand Up @@ -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),
)
}

Expand Down Expand Up @@ -424,6 +424,44 @@ 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