Add discord webhook message

This commit is contained in:
2024-02-18 21:46:58 +01:00
parent 522ff05151
commit fecade9bf9
3 changed files with 43 additions and 0 deletions

View File

@@ -7,6 +7,13 @@ const MessageValidator = z.object({
export default defineEventHandler(async (event) => {
const { message } = await readValidatedBody(event, MessageValidator)
const { user } = await requireUserSession(event)
await sendDiscordWebhookMessage({
title: 'New guestbook message ✨',
description: `**${user.username}** as signed the book : "*${message}*"`,
color: 15893567,
})
return await usePrisma().guestbookMessage.upsert({
where: {
email: user.email,

View File

@@ -7,6 +7,13 @@ const SuggestionValidator = z.object({
export default defineEventHandler(async (event) => {
const { content } = await readValidatedBody(event, SuggestionValidator)
const { user } = await requireUserSession(event)
await sendDiscordWebhookMessage({
title: 'New suggestion ✨',
description: `**${user.username}** as requested **${content}** for the talents page.`,
color: 15237114,
})
return await usePrisma().suggestion.upsert({
where: {
email: user.email,

View File

@@ -0,0 +1,29 @@
/* eslint-disable node/prefer-global/process */
interface WebhookContent {
title: string
description: string
color: number
}
export async function sendDiscordWebhookMessage(content: WebhookContent) {
const id = process.env.NUXT_DISCORD_ID
const token = process.env.NUXT_DISCORD_TOKEN
await $fetch(`https://discordapp.com/api/webhooks/${id}/${token}`, {
method: 'POST',
body: {
embeds: [
{
title: content.title,
description: content.description,
color: content.color,
url: 'https://arthurdanjou.fr/talents',
footer: {
text: 'Powered by Nuxt',
},
timestamp: new Date().toISOString(),
},
],
username: 'ArtDanjRobot - Website',
},
})
}