-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscord.py
More file actions
63 lines (50 loc) · 1.96 KB
/
discord.py
File metadata and controls
63 lines (50 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import annotations
from typing import Optional
import requests
from models import SnippetConfig
class DiscordNotifier:
def __init__(self, webhook_url: str):
self.webhook_url = webhook_url or ""
def notify_change(
self,
snippet: SnippetConfig,
diff_text: str,
diff_summary: Optional[str] = None,
diff_source: Optional[str] = None,
) -> None:
if not self.webhook_url:
print("No webhook URL configured; skipping Discord notification.")
return
title = f"Code change detected in {snippet.owner}/{snippet.repo}"
description = (
f"**File:** {snippet.file_url}\n"
f"**Lines monitored:** L{snippet.start_line}-L{snippet.end_line}\n"
f"**Note:** {snippet.note or '—'}"
)
fields = []
if diff_summary:
label = f"Diff summary ({diff_source})" if diff_source else "Diff summary"
trimmed = diff_summary.strip()
if len(trimmed) > 1900:
trimmed = trimmed[:1900] + "…"
fields.append({"name": label, "value": trimmed, "inline": False})
code_block = diff_text or "No diff text available"
if len(code_block) > 1800:
code_block = code_block[:1800] + "\n…"
fields.append({"name": "Code change diff", "value": f"```diff\n{code_block}\n```", "inline": False})
payload = {
"content": None,
"embeds": [
{
"title": title,
"description": description,
"fields": fields,
}
],
}
try:
resp = requests.post(self.webhook_url, json=payload, timeout=10)
if resp.status_code >= 400:
print(f"Failed to send Discord notification: {resp.status_code} {resp.text}")
except Exception as e:
print(f"Error sending Discord notification: {e}")