This commit is contained in:
1 parent
e85e4d74ea
commit
2e4ed76121
378 files changed
+22404
-20686
No files matched your search
+40
-40
@@ -1,16 +1,16 @@
|
||||
import { env } from '@/env'
|
||||
import { prisma } from '../prisma'
|
||||
import { env } from "@/env";
|
||||
import { prisma } from "../prisma";
|
||||
|
||||
export type { WebhookAction } from '@/types/admin'
|
||||
export type { WebhookAction } from "@/types/admin";
|
||||
|
||||
import type { WebhookAction } from '@/types/admin'
|
||||
import type { WebhookAction } from "@/types/admin";
|
||||
|
||||
interface WebhookPayload {
|
||||
action: WebhookAction
|
||||
actor: string
|
||||
target: string
|
||||
targetId?: number
|
||||
details?: string
|
||||
action: WebhookAction;
|
||||
actor: string;
|
||||
target: string;
|
||||
targetId?: number;
|
||||
details?: string;
|
||||
}
|
||||
|
||||
const ACTION_COLORS: Record<WebhookAction, number> = {
|
||||
@@ -31,82 +31,82 @@ const ACTION_COLORS: Record<WebhookAction, number> = {
|
||||
event_cancel: 0xf87272,
|
||||
poll_create: 0x36d399,
|
||||
poll_close: 0xa78bfa,
|
||||
}
|
||||
};
|
||||
|
||||
async function getSetting(cmsKey: string, envValue?: string): Promise<string | null> {
|
||||
if (envValue) return envValue
|
||||
if (envValue) return envValue;
|
||||
try {
|
||||
const setting = await prisma.websiteSetting.findUnique({ where: { key: cmsKey } })
|
||||
return setting?.value || null
|
||||
const setting = await prisma.websiteSetting.findUnique({ where: { key: cmsKey } });
|
||||
return setting?.value || null;
|
||||
} catch {
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function sendDiscord(payload: WebhookPayload): Promise<void> {
|
||||
const url = await getSetting('webhook_discord_url', env.DISCORD_WEBHOOK_URL)
|
||||
if (!url) return
|
||||
const url = await getSetting("webhook_discord_url", env.DISCORD_WEBHOOK_URL);
|
||||
if (!url) return;
|
||||
|
||||
const embed = {
|
||||
title: `[${payload.action.toUpperCase().replace('_', ' ')}]`,
|
||||
title: `[${payload.action.toUpperCase().replace("_", " ")}]`,
|
||||
color: ACTION_COLORS[payload.action] ?? 0x666666,
|
||||
fields: [
|
||||
{ name: 'Staff', value: payload.actor, inline: true },
|
||||
{ name: 'Target', value: payload.target, inline: true },
|
||||
{ name: "Staff", value: payload.actor, inline: true },
|
||||
{ name: "Target", value: payload.target, inline: true },
|
||||
],
|
||||
timestamp: new Date().toISOString(),
|
||||
}
|
||||
};
|
||||
|
||||
if (payload.details) {
|
||||
embed.fields.push({ name: 'Details', value: payload.details, inline: false })
|
||||
embed.fields.push({ name: "Details", value: payload.details, inline: false });
|
||||
}
|
||||
|
||||
try {
|
||||
await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ embeds: [embed] }),
|
||||
signal: AbortSignal.timeout(5000),
|
||||
})
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('[Webhook] Discord send failed:', {
|
||||
console.error("[Webhook] Discord send failed:", {
|
||||
action: payload.action,
|
||||
error: (err as Error).message,
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function sendTelegram(payload: WebhookPayload): Promise<void> {
|
||||
const token = await getSetting('webhook_telegram_bot_token', env.TELEGRAM_BOT_TOKEN)
|
||||
const chatId = await getSetting('webhook_telegram_chat_id', env.TELEGRAM_CHAT_ID)
|
||||
if (!token || !chatId) return
|
||||
const token = await getSetting("webhook_telegram_bot_token", env.TELEGRAM_BOT_TOKEN);
|
||||
const chatId = await getSetting("webhook_telegram_chat_id", env.TELEGRAM_CHAT_ID);
|
||||
if (!token || !chatId) return;
|
||||
|
||||
const text = [
|
||||
`<b>[${payload.action.toUpperCase().replace('_', ' ')}]</b>`,
|
||||
`<b>[${payload.action.toUpperCase().replace("_", " ")}]</b>`,
|
||||
`<b>Staff:</b> ${payload.actor}`,
|
||||
`<b>Target:</b> ${payload.target}`,
|
||||
payload.details ? `<b>Details:</b> ${payload.details}` : '',
|
||||
payload.details ? `<b>Details:</b> ${payload.details}` : "",
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join('\n')
|
||||
.join("\n");
|
||||
|
||||
try {
|
||||
await fetch(`https://api.telegram.org/bot${token}/sendMessage`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ chat_id: chatId, text, parse_mode: 'HTML' }),
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ chat_id: chatId, text, parse_mode: "HTML" }),
|
||||
signal: AbortSignal.timeout(5000),
|
||||
})
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('[Webhook] Telegram send failed:', {
|
||||
console.error("[Webhook] Telegram send failed:", {
|
||||
action: payload.action,
|
||||
error: (err as Error).message,
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** Fire-and-forget notification to Discord + Telegram */
|
||||
export function notify(payload: WebhookPayload): void {
|
||||
sendDiscord(payload).catch(() => {})
|
||||
sendTelegram(payload).catch(() => {})
|
||||
sendDiscord(payload).catch(() => {});
|
||||
sendTelegram(payload).catch(() => {});
|
||||
}
|
||||
Reference in new issue
Block a user