37 lines
858 B
TypeScript
37 lines
858 B
TypeScript
export default defineEventHandler(async (event) => {
|
|
const config = useRuntimeConfig(event)
|
|
const token = config.discordBotToken
|
|
const channel = config.discordChannelId
|
|
|
|
if (!token || !channel) {
|
|
throw createError({
|
|
statusCode: 503,
|
|
statusMessage: "Service indisponible"
|
|
})
|
|
}
|
|
|
|
try {
|
|
const messages = await $fetch(
|
|
`https://discord.com/api/v10/channels/${channel}/messages?limit=20`,
|
|
{
|
|
headers: {
|
|
Authorization: `Bot ${token}`
|
|
}
|
|
}
|
|
)
|
|
|
|
return (messages as any[]).map((m) => ({
|
|
id: m.id,
|
|
content: m.content,
|
|
author: { username: m.author?.username ?? "Inconnu" }
|
|
}))
|
|
} catch (error) {
|
|
console.error("Erreur Discord messages:", error)
|
|
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: "Erreur lors de l'opération"
|
|
})
|
|
}
|
|
})
|