Files
Stock-trading-programming/.claude/discord_notify.py
T
whdwo798 9ea0a5101d [2026-05-15] Discord 알림 - 이번 세션 신규 커밋만 전송
- SessionStart 훅: git HEAD를 session_start_sha.txt에 저장
- discord_notify.py: 세션 시작 이후 커밋만 표시 (누적 방지)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 13:59:30 +09:00

47 lines
1.6 KiB
Python

import subprocess, json, urllib.request, datetime, sys
WEBHOOK = "https://discord.com/api/webhooks/1504705352166543370/H3dhs_4LaxnaFj_mhUUi02qpk3JegE_Ji9C6rNpEqRNv_iW6aqvso8VyPw17048Nt0oF"
def run(cmd):
return subprocess.run(cmd, capture_output=True, text=True, encoding='utf-8', errors='replace').stdout.strip()
import pathlib
SESSION_SHA_FILE = pathlib.Path('.claude/session_start_sha.txt')
# 세션 시작 시 저장된 HEAD와 비교해 이번 세션 커밋만 표시
start_sha = SESSION_SHA_FILE.read_text().strip() if SESSION_SHA_FILE.exists() else ''
if start_sha:
log = run(['git', 'log', '--oneline', f'{start_sha}..HEAD'])
else:
log = run(['git', 'log', '--oneline', '-5'])
status = run(['git', 'status', '--short'])
diff_stat = run(['git', 'diff', '--stat'])
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
parts = [f"**[Claude Code 작업완료] {now}**"]
if log:
parts.append(f"```\n{log}\n```")
elif not status and not diff_stat:
parts.append("_이번 세션 변경사항 없음_")
if status or diff_stat:
combined = '\n'.join(filter(None, [status, diff_stat]))
parts.append(f"**미커밋 변경:**\n```\n{combined}\n```")
if len(parts) == 1:
parts.append("_변경사항 없음_")
msg = '\n'.join(parts)
if len(msg) > 1990:
msg = msg[:1987] + '...'
data = json.dumps({'content': msg}).encode('utf-8')
req = urllib.request.Request(WEBHOOK, data=data, headers={'Content-Type': 'application/json', 'User-Agent': 'DiscordBot (stockbot, 1.0)'}, method='POST')
try:
urllib.request.urlopen(req)
except Exception as e:
print(f"Discord notify failed: {e}", file=sys.stderr)