discord-gateway-resume-dedup
Skill0 starsChanged 5 months agoMITView on GitHub
自作Discord gateway bot(WebSocket直叩き)が「前の指令を実行中」等を指令してないのに勝手に出す/同じ指令が二重実行される時。原因はRESUME再配信の未dedup。
In this repository99 other files it gives its agents.
- agents-tools-quoted-jsonSkill
- asc-api-app-store-submitSkill
- asd-ste100Skill
- automation-bool-selector-pitfallsSkill
- automation-stale-cooldown-flagSkill
- autonomous-build-verifySkill
- book-to-skillSkill
- ccusage-cli-cost-trackingSkill
- chrome-ext-mv3-programmatic-injectSkill
- chrome-ext-store-assetsSkill
- claude-headless-p-invocationSkill
- claude-plugin-context-auditSkill
- claude-session-log-to-kbSkill
- cloudflare-workers-do-deploySkill
- codebase-to-courseSkill
- codesign-keychain-prompt-fixSkill
- codex-cli-setupSkill
- codex-delegation-handoffSkill
- content-factory-grounding-dedupSkill
- conversation-log-append-patternSkill
- cron-to-launchd-macosSkill
- cross-automation-shared-resource-killSkill
- design-token-migration-rnSkill
- discord-bot-image-to-claudeSkill
- excel-filtered-url-http-auditSkill
- execution-budget-vs-capsSkill
- expo-free-local-ios-testflightSkill
- ga4-connect-projectsSkill
- gcal-oauth-desktop-setupSkill
- git-commit-helperSkill
- git-prepush-secret-remote-checkSkill
- gsc-cli-automationSkill
- hatena-blogsync-autopostSkill
- hook-latency-wrapSkill
- hook-log-append-patternSkill
- html-to-pdf-slide-deckSkill
- humanizerSkill
- human-review-gate-shellSkill
- implementation-model-routingSkill
- insights-to-claude-mdSkill
- ios-permission-key-security-matrixSkill
- ios-sim-store-screenshotsSkill
- launchd-exit78-exconfig-debugSkill
- llm-content-pipeline-quality-gateSkill
- local-ai-video-pipelineSkill
- local-flux-mflux-image-genSkill
- local-llm-slow-diagnosisSkill
- local-music-generationSkill
- lottie-skottie-authoring-pitfallsSkill
- machine-wide-api-ratelimit-latchSkill
- macos-hook-path-timeoutSkill
- md-to-docx-reportSkill
- memory-md-inject-bloat-auditSkill
- message-display-secrets-maskSkill
- metric-source-attributionSkill
- migrate-icloud-desktop-assetsSkill
- nextauth-local-e2e-sessionSkill
- nextjs-rsc-payload-slimSkill
- nothing-designSkill
- openclaw-ollama-setupSkill
- The first 60 shown.
The file
Files
Procedure
Discord gateway を WebSocket で直接叩く自作bot(例 ~/.discord/bridge.mjs)で、
指令を出してないのに過去の応答/⏳busyメッセージが湧く、または1回の指令が二重実行される時。
原因
Discord gateway はハートビート切れ→自動RESUME(op 6)のたびに、
前回ack済み seq 以降のイベントを再配信する。MESSAGE_CREATE の重複排除が無いと、
過去メッセージのリプレイで handleMessage が再実行され、亡霊応答や二重実行になる。
ゾンビ再接続(if (!acked) ws.close(4000))を持つbotは特に頻発する。
修正(2点)
グローバルに起動時刻と既処理IDセットを置く:
const BOOT_TIME = Date.now(); // これより前のメッセージはリプレイ
const seenMsgs = new Set(); // 処理済みID(再配信の重複排除)
const snowflakeTime = (id) => Number((BigInt(id) >> 22n)) + 1420070400000;メッセージハンドラ冒頭(オーナー/ch判定の直後)でガード:
if (m.id) {
if (snowflakeTime(m.id) < BOOT_TIME) return; // 起動前=RESUMEリプレイ。無視
if (seenMsgs.has(m.id)) return; // 同一メッセージ再配信。dedup
seenMsgs.add(m.id);
if (seenMsgs.size > 500) seenMsgs.delete(seenMsgs.values().next().value);
}反映
launchd管理なら kickstart で再起動(killでもKeepAliveが拾う):
launchctl kickstart -k gui/$(id -u)/<label> # 例 com.lily.discord-bridgePitfalls
- snowflake→ms変換は
(BigInt(id) >> 22n) + Discord epoch(1420070400000)。22bitシフト必須。 seenMsgsは無限に貯めない。上限を切ってFIFO削除(上の500件ローテ)。- busy/in-flightフラグの解除は必ず
finallyに置く。runClaude等がハングするとフラグが残り、 dedupとは別経路で「実行中」が居座る。dedup入れても解除漏れは別途潰す。 - close時の再接続は resume可能コード(4000,4001,1006等)で resume、不可なら fresh。 dedupはどちらの経路でも効く。
Verification
- 修正後
node --check bridge.mjsで構文OK。 - 再起動しログに
READY as <bot>が出る(bridge.out.log)。 - 一定時間放置→再接続が起きても、過去指令が再生されないこと。
- 1指令=1応答(▶️受付→✅完了が1回ずつ)で二重化しないこと。
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

