From 8ab87bda519a81f2f3b75ba0eacfd45df611d845 Mon Sep 17 00:00:00 2001 From: Sucukdeluxe Date: Tue, 9 Jun 2026 21:52:47 +0200 Subject: [PATCH] Feature: Discord-Ping in Benachrichtigungen (optionale Erwaehnung) Neues Settings-Feld "Discord-Ping" unter der Webhook-URL: User-ID, @everyone oder @here. Wird jeder Webhook-Nachricht vorangestellt, damit Discord wirklich pingt statt nur still in den Kanal zu schreiben. Eine nackte Zahl wird als <@id> verpackt (nur so pingt eine User-Erwaehnung); @everyone/@here und fertige <@...>-Mentions gehen unveraendert durch. Default leer = Verhalten wie bisher. 5 neue Tests (Normalisierung + Content-Prefix). --- src/main/constants.ts | 1 + src/main/download-manager.ts | 6 ++++-- src/main/notify.ts | 17 ++++++++++++++++- src/main/storage.ts | 1 + src/renderer/App.tsx | 5 ++++- src/shared/types.ts | 1 + tests/notify.test.ts | 29 ++++++++++++++++++++++++++++- 7 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/main/constants.ts b/src/main/constants.ts index 197522b..1c37493 100644 --- a/src/main/constants.ts +++ b/src/main/constants.ts @@ -108,6 +108,7 @@ export function defaultSettings(): AppSettings { confirmDeleteSelection: true, backupIncludeDownloads: false, notifyUrl: "", + notifyMention: "", notifyOnPackageCompleted: false, notifyOnPackageFailed: false, notifyOnRunFinished: false, diff --git a/src/main/download-manager.ts b/src/main/download-manager.ts index 7c9128f..5a696ac 100644 --- a/src/main/download-manager.ts +++ b/src/main/download-manager.ts @@ -10497,7 +10497,8 @@ export class DownloadManager extends EventEmitter { this.notifiedPackages.add(pkg.id); void sendNotification(url, { title: kind === "completed" ? "✅ Paket fertig" : "❌ Paket fehlgeschlagen", - message: `${pkg.name}\n${detail}` + message: `${pkg.name}\n${detail}`, + mention: this.settings.notifyMention }); } @@ -12121,7 +12122,8 @@ export class DownloadManager extends EventEmitter { if (this.settings.notifyOnRunFinished && total > 0) { void sendNotification(this.settings.notifyUrl, { title: failed > 0 ? "⚠️ Durchlauf beendet" : "🏁 Durchlauf beendet", - message: `${success}/${total} erfolgreich, ${failed} fehlgeschlagen, ${cancelled} abgebrochen\nDauer ${duration}s, Durchschnitt ${humanSize(avgSpeed)}/s` + message: `${success}/${total} erfolgreich, ${failed} fehlgeschlagen, ${cancelled} abgebrochen\nDauer ${duration}s, Durchschnitt ${humanSize(avgSpeed)}/s`, + mention: this.settings.notifyMention }); } this.runItemIds.clear(); diff --git a/src/main/notify.ts b/src/main/notify.ts index e9fc5a1..94ebda6 100644 --- a/src/main/notify.ts +++ b/src/main/notify.ts @@ -3,6 +3,7 @@ import { logger } from "./logger"; export interface NotifyPayload { title: string; message: string; + mention?: string; } const NOTIFY_TIMEOUT_MS = 5000; @@ -12,8 +13,22 @@ export function isNotifyUrlValid(url: string): boolean { return /^https?:\/\/\S+$/i.test(String(url || "").trim()); } +// Accepts a bare Discord user ID (wrapped as <@id> so it actually pings), +// @everyone/@here, or an already-formed <@...>/<@&...> mention as-is. +export function normalizeDiscordMention(raw: string): string { + const text = String(raw || "").trim(); + if (!text) { + return ""; + } + if (/^\d{5,}$/.test(text)) { + return `<@${text}>`; + } + return text; +} + export function buildNotifyRequest(url: string, payload: NotifyPayload): { url: string; init: RequestInit } { - const content = `**${payload.title}**\n${payload.message}`.slice(0, 2000); + const mention = normalizeDiscordMention(payload.mention || ""); + const content = `${mention ? `${mention} ` : ""}**${payload.title}**\n${payload.message}`.slice(0, 2000); return { url: String(url || "").trim(), init: { diff --git a/src/main/storage.ts b/src/main/storage.ts index aa1811f..f4e19db 100644 --- a/src/main/storage.ts +++ b/src/main/storage.ts @@ -460,6 +460,7 @@ export function normalizeSettings(settings: AppSettings): AppSettings { confirmDeleteSelection: settings.confirmDeleteSelection !== undefined ? Boolean(settings.confirmDeleteSelection) : defaults.confirmDeleteSelection, backupIncludeDownloads: settings.backupIncludeDownloads !== undefined ? Boolean(settings.backupIncludeDownloads) : defaults.backupIncludeDownloads, notifyUrl: asText(settings.notifyUrl) || defaults.notifyUrl, + notifyMention: asText(settings.notifyMention) || defaults.notifyMention, notifyOnPackageCompleted: settings.notifyOnPackageCompleted !== undefined ? Boolean(settings.notifyOnPackageCompleted) : defaults.notifyOnPackageCompleted, notifyOnPackageFailed: settings.notifyOnPackageFailed !== undefined ? Boolean(settings.notifyOnPackageFailed) : defaults.notifyOnPackageFailed, notifyOnRunFinished: settings.notifyOnRunFinished !== undefined ? Boolean(settings.notifyOnRunFinished) : defaults.notifyOnRunFinished, diff --git a/src/renderer/App.tsx b/src/renderer/App.tsx index 7da08be..7525a68 100644 --- a/src/renderer/App.tsx +++ b/src/renderer/App.tsx @@ -853,7 +853,7 @@ const emptySnapshot = (): UiSnapshot => ({ maxParallel: 4, maxParallelExtract: 2, extractCpuPriority: "high", retryLimit: 0, speedLimitEnabled: false, speedLimitKbps: 0, speedLimitMode: "global", updateRepo: "", autoUpdateCheck: true, clipboardWatch: false, minimizeToTray: false, theme: "dark", collapseNewPackages: true, historyRetentionMode: "permanent", autoSortPackagesByProgress: true, autoSkipExtracted: false, hideExtractedItems: true, confirmDeleteSelection: true, backupIncludeDownloads: false, - notifyUrl: "", notifyOnPackageCompleted: false, notifyOnPackageFailed: false, notifyOnRunFinished: false, + notifyUrl: "", notifyMention: "", notifyOnPackageCompleted: false, notifyOnPackageFailed: false, notifyOnRunFinished: false, accountListShowDetailedDebridLinkKeys: false, bandwidthSchedules: [], totalDownloadedAllTime: 0, totalCompletedFilesAllTime: 0, totalRuntimeAllTimeMs: 0, columnOrder: ["name", "size", "progress", "hoster", "account", "prio", "status", "speed"], @@ -4960,6 +4960,9 @@ export function App(): ReactElement { setText("notifyUrl", e.target.value)} />
In Discord: Servereinstellungen → Integrationen → Webhooks → Neuer Webhook → URL kopieren und hier eintragen. Die gewählten Ereignisse landen als Nachricht im Kanal.
+ + setText("notifyMention", e.target.value)} /> +
Wird jeder Nachricht vorangestellt, damit Discord dich pingt. Eigene ID: Discord-Einstellungen → Erweitert → Entwicklermodus an, dann Rechtsklick auf deinen Namen → "User-ID kopieren" und die Zahl hier eintragen.
diff --git a/src/shared/types.ts b/src/shared/types.ts index 70fa061..26312a1 100644 --- a/src/shared/types.ts +++ b/src/shared/types.ts @@ -133,6 +133,7 @@ export interface AppSettings { confirmDeleteSelection: boolean; backupIncludeDownloads: boolean; notifyUrl: string; + notifyMention: string; notifyOnPackageCompleted: boolean; notifyOnPackageFailed: boolean; notifyOnRunFinished: boolean; diff --git a/tests/notify.test.ts b/tests/notify.test.ts index 73f6724..42acb87 100644 --- a/tests/notify.test.ts +++ b/tests/notify.test.ts @@ -1,5 +1,22 @@ import { describe, expect, it, vi } from "vitest"; -import { buildNotifyRequest, isNotifyUrlValid, sendNotification } from "../src/main/notify"; +import { buildNotifyRequest, isNotifyUrlValid, normalizeDiscordMention, sendNotification } from "../src/main/notify"; + +describe("normalizeDiscordMention", () => { + it("wraps a bare user ID as a pinging mention", () => { + expect(normalizeDiscordMention("123456789012345678")).toBe("<@123456789012345678>"); + expect(normalizeDiscordMention(" 987654321 ")).toBe("<@987654321>"); + }); + it("passes @everyone/@here and formed mentions through", () => { + expect(normalizeDiscordMention("@everyone")).toBe("@everyone"); + expect(normalizeDiscordMention("@here")).toBe("@here"); + expect(normalizeDiscordMention("<@123456789>")).toBe("<@123456789>"); + expect(normalizeDiscordMention("<@&111222333>")).toBe("<@&111222333>"); + }); + it("returns empty for empty input", () => { + expect(normalizeDiscordMention("")).toBe(""); + expect(normalizeDiscordMention(" ")).toBe(""); + }); +}); describe("isNotifyUrlValid", () => { it("accepts http/https URLs", () => { @@ -30,6 +47,16 @@ describe("buildNotifyRequest", () => { const body = JSON.parse(String(req.init.body)); expect(body.content.length).toBe(2000); }); + it("prepends the mention so Discord pings (bare ID gets wrapped)", () => { + const req = buildNotifyRequest("https://discord.com/api/webhooks/123/abc", { title: "T", message: "M", mention: "123456789012345678" }); + const body = JSON.parse(String(req.init.body)); + expect(body.content).toBe("<@123456789012345678> **T**\nM"); + }); + it("sends no mention prefix when the field is empty", () => { + const req = buildNotifyRequest("https://discord.com/api/webhooks/123/abc", { title: "T", message: "M", mention: "" }); + const body = JSON.parse(String(req.init.body)); + expect(body.content).toBe("**T**\nM"); + }); }); describe("sendNotification", () => {