Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.
Rust acpagent-client-protocolai-agentsai-assistantclaudeclaude-codecodexcoding-agentscursorgeminigemini-cligrok-bot-alternativeiosiphonelinuxmacosmcpopen-sourcerustself-hosted
feat(discord): celebrate every 100 GitHub stars in #milestones
leepokai5814366a4db3
diff --git a/discord/README.md b/discord/README.mdindex 250fca9f..64a0cf58 100644--- a/discord/README.md+++ b/discord/README.md@@ -15,6 +15,9 @@ the GitHub roles.5. `/mentions` takes release notes signed with `MENTIONS_SECRET` and turns each `@login` of a linked member intoa Discord mention. Auto Tag (`.github/workflows/auto-tag.yml`) uses it so the #releases post pings the peopleit thanks; the links stay private.+6. `/webhook` also takes the repo's `star` events: each 100th star posts once in #milestones, with progress+ toward `STAR_GOAL` and one-click links to share Codync on X, Threads, Reddit and LinkedIn. The last milestone+ posted is kept in KV (`stars:milestone`), so unstarring and starring again doesn't repeat it.`.github/workflows/contributor.yml` comments the link on a contributor's first merged pull request and firstissue. The onboarding Server Guide has a "Link your GitHub" to-do that leads to the link in #welcome.@@ -23,12 +26,12 @@ issue. The onboarding Server Guide has a "Link your GitHub" to-do that leads to- Discord application **Codync** (Developer Portal): OAuth2 redirect `https://discord.codync.dev/callback`; botnot public. In the server, its role keeps Manage Roles and Create Invite (`guilds.join` needs it), sits above- Contributor and Reporter, and can view and send in #moderator-only.+ Contributor and Reporter, and can view and send in #moderator-only and #milestones (read-only for everyone else).- GitHub OAuth app **Codync Discord** (leepokai's developer settings): callback`https://discord.codync.dev/github/callback`, no scopes requested.-- Repo webhook: `https://discord.codync.dev/webhook`, JSON, events `pull_request` and `issues`, secret =+- Repo webhook: `https://discord.codync.dev/webhook`, JSON, events `pull_request`, `issues` and `star`, secret =`GITHUB_WEBHOOK_SECRET`.-- `wrangler.toml` holds the public values (client ids, guild, role and channel ids, repo) and the `LINKS` KV+- `wrangler.toml` holds the public values (client ids, guild, role and channel ids, star goal, repo) and the `LINKS` KVnamespace.```bashdiff --git a/discord/src/guild.ts b/discord/src/guild.tsindex 4f51a375..3f3541a3 100644--- a/discord/src/guild.ts+++ b/discord/src/guild.ts@@ -1,4 +1,4 @@-// The Codync server: adding members, the GitHub roles they earn, and the staff channel notice.+// The Codync server: adding members, the GitHub roles they earn, and posting to its channels.import { json } from "./http.ts";import type { Env } from "./index.ts";@@ -71,13 +71,19 @@ export async function grant(env: Env, discordId: string, earned: Role[], reason:return missing;}-/** Posts to the staff channel. A failure is logged and never fails the member's flow. */-export async function notify(env: Env, content: string): Promise<void> {- const res = await fetch(`${DISCORD}/channels/${env.STAFF_CHANNEL_ID}/messages`, {+/** Posts to a channel and says whether it went out. A failure is logged and never fails the caller's flow. */+export async function send(env: Env, channelId: string, content: string): Promise<boolean> {+ const res = await fetch(`${DISCORD}/channels/${channelId}/messages`, {method: "POST",headers: { ...bot(env), "Content-Type": "application/json" },// No pings; flag 4 drops link previews.body: JSON.stringify({ content, allowed_mentions: { parse: [] }, flags: 4 }),});- if (!res.ok) console.error("notify failed", { status: res.status, body: await res.text() });+ if (!res.ok) console.error("send failed", { channel: channelId, status: res.status, body: await res.text() });+ return res.ok;+}++/** Posts to the staff channel. */+export async function notify(env: Env, content: string): Promise<void> {+ await send(env, env.STAFF_CHANNEL_ID, content);}diff --git a/discord/src/index.ts b/discord/src/index.tsindex 217f333d..03b02b0b 100644--- a/discord/src/index.ts+++ b/discord/src/index.ts@@ -25,8 +25,11 @@ export interface Env {CONTRIBUTOR_ROLE_ID: string;REPORTER_ROLE_ID: string;STAFF_CHANNEL_ID: string;+ MILESTONES_CHANNEL_ID: string;+ /** The star count the #milestones posts count toward. */+ STAR_GOAL: string;REPO: string;- /** `github:<GitHub user id>` → Discord user id. */+ /** `github:<GitHub user id>` → Discord user id; `stars:milestone` → the last star milestone posted. */LINKS: KVNamespace;}diff --git a/discord/src/stars.ts b/discord/src/stars.tsnew file mode 100644index 00000000..b090ece8--- /dev/null+++ b/discord/src/stars.ts@@ -0,0 +1,45 @@+// Star milestones: every 100th star on the repo gets one post in #milestones, with the way left to the goal.++import { send } from "./guild.ts";+import type { Env } from "./index.ts";++const STEP = 100;+const BAR = 20;+const KEY = "stars:milestone";++/** Posts the milestone the star count has reached, once: unstarring and starring again doesn't repeat it. */+export async function celebrate(env: Env, stars: number): Promise<void> {+ const milestone = Math.floor(stars / STEP) * STEP;+ const last = Number((await env.LINKS.get(KEY)) ?? 0);+ if (milestone <= last) return;+ // ponytail: KV isn't atomic, so two stars delivered at the same instant on a new milestone could post it twice.+ if (await send(env, env.MILESTONES_CHANNEL_ID, milestoneMessage(milestone, Number(env.STAR_GOAL), env.REPO))) {+ await env.LINKS.put(KEY, String(milestone));+ console.log("star milestone", { stars: milestone });+ }+}++const PITCH = "Codync: your coding agents, as teammates you can message. Free and open source.";++/** One-click posts about Codync on the usual social platforms, linking the repo. */+function shareLinks(url: string): string {+ const q = (params: Record<string, string>) => new URLSearchParams(params).toString().replaceAll("+", "%20");+ return [+ `[X](https://x.com/intent/post?${q({ text: PITCH, url })})`,+ `[Threads](https://www.threads.net/intent/post?${q({ text: `${PITCH} ${url}` })})`,+ `[Reddit](https://www.reddit.com/submit?${q({ url, title: PITCH })})`,+ `[LinkedIn](https://www.linkedin.com/sharing/share-offsite/?${q({ url })})`,+ ].join(" · ");+}++export function milestoneMessage(stars: number, goal: number, repo: string): string {+ const n = (x: number) => x.toLocaleString("en-US");+ const url = `https://github.com/${repo}`;+ const head = stars >= goal && stars - STEP < goal+ ? `🎉 **Codync hit ${n(stars)} stars on GitHub, goal reached!**`+ : `⭐ **Codync just hit ${n(stars)} stars on GitHub!**`;+ const filled = Math.round((stars / goal) * BAR);+ const progress = stars < goal ? `\n\`${"█".repeat(filled)}${"░".repeat(BAR - filled)}\` ${n(stars)} / ${n(goal)}` : "";+ const ask = stars < goal ? `Help us reach ${n(goal)}: tell people about Codync` : "Keep spreading the word";+ return `${head}${progress}\nThanks to everyone who starred [${repo}](${url}) 💛\n\n📣 ${ask} on ${shareLinks(url)}, or wherever you hang out.`;+}diff --git a/discord/src/webhook.ts b/discord/src/webhook.tsindex 6ad04f7d..56bed2a9 100644--- a/discord/src/webhook.ts+++ b/discord/src/webhook.ts@@ -1,9 +1,10 @@-// GitHub webhook (pull_request, issues): a linked member earns a role as soon as their pull request merges or-// their issue opens, without opening the link again.+// GitHub webhook (pull_request, issues, star): a linked member earns a role as soon as their pull request merges+// or their issue opens, without opening the link again; each 100th star is celebrated in #milestones.import { grant, notify, profile, roles, type Role } from "./guild.ts";import { verify } from "./hmac.ts";import type { Env } from "./index.ts";+import { celebrate } from "./stars.ts";interface Item {number: number;@@ -15,6 +16,7 @@ interface Payload {action: string;pull_request?: Item & { merged: boolean };issue?: Item;+ repository?: { stargazers_count: number };}const done = () => new Response(null, { status: 204 });@@ -25,7 +27,13 @@ export async function webhook(req: Request, env: Env): Promise<Response> {if (req.method !== "POST" || !(await verify(env.GITHUB_WEBHOOK_SECRET, body, signature))) {return new Response("Bad signature", { status: 401 });}- const earned = earnedBy(req.headers.get("X-GitHub-Event"), JSON.parse(body) as Payload, env);+ const event = req.headers.get("X-GitHub-Event");+ const payload = JSON.parse(body) as Payload;+ if (event === "star" && payload.action === "created" && payload.repository) {+ await celebrate(env, payload.repository.stargazers_count);+ return done();+ }+ const earned = earnedBy(event, payload, env);if (!earned) return done();const { role, item, what } = earned;const discordId = await env.LINKS.get(`github:${item.user.id}`);diff --git a/discord/test/claim.test.ts b/discord/test/claim.test.tsindex 21b015ed..9b351881 100644--- a/discord/test/claim.test.ts+++ b/discord/test/claim.test.ts@@ -10,7 +10,7 @@ const env = {DISCORD_CLIENT_ID: "app", DISCORD_CLIENT_SECRET: "secret", DISCORD_BOT_TOKEN: "bot",GITHUB_CLIENT_ID: "ghapp", GITHUB_CLIENT_SECRET: "ghsecret", GITHUB_TOKEN: "gh", GITHUB_WEBHOOK_SECRET: "hook",SESSION_SECRET: "session", MENTIONS_SECRET: "notes", GUILD_ID: "guild", CONTRIBUTOR_ROLE_ID: "contributor", REPORTER_ROLE_ID: "reporter",- STAFF_CHANNEL_ID: "staff", REPO: "leepokai/Codync",+ STAFF_CHANNEL_ID: "staff", MILESTONES_CHANNEL_ID: "milestones", STAR_GOAL: "1000", REPO: "leepokai/Codync",LINKS: { get: async (k: string) => links.get(k) ?? null, put: async (k: string, v: string) => void links.set(k, v) },} as never;@@ -173,4 +173,29 @@ assert.deepEqual(await notes("- Fast mode. Thanks @octo (#101).\n@Octo, @other,text: "- Fast mode. Thanks <@42> (#101).\n<@42>, @other, @nobody, [email protected], `@octo_x`",users: ["42"],});+// Stars: each 100th star posts once in #milestones, with progress toward the goal until it's reached.+const posts: string[] = [];+let up = true;+globalThis.fetch = (async (input: string, init?: RequestInit) => {+ assert.equal(String(input), "https://discord.com/api/v10/channels/milestones/messages");+ if (!up) return new Response("down", { status: 503 });+ posts.push(JSON.parse(String(init?.body)).content);+ return Response.json({});+}) as typeof fetch;+const star = (count: number, action = "created") => hook("star", { action, repository: { stargazers_count: count } });+assert.equal(await star(99), 204);+assert.equal(await star(402, "deleted"), 204);+assert.deepEqual(posts, []);+up = false;+assert.equal(await star(400), 204);+assert.equal(links.get("stars:milestone"), undefined);+up = true;+for (const count of [402, 401, 400, 450, 1000, 1000, 1104]) assert.equal(await star(count), 204);+const thanks = "Thanks to everyone who starred [leepokai/Codync](https://github.com/leepokai/Codync) 💛\n\n📣 ";+assert.equal(posts.length, 3);+assert.ok(posts[0].startsWith(`⭐ **Codync just hit 400 stars on GitHub!**\n\`████████░░░░░░░░░░░░\` 400 / 1,000\n${thanks}Help us reach 1,000: tell people about Codync on [X](https://x.com/intent/post?text=Codync%3A%20`));+assert.ok(posts[1].startsWith(`🎉 **Codync hit 1,000 stars on GitHub, goal reached!**\n${thanks}Keep spreading the word on [X](`));+assert.ok(posts[2].startsWith(`⭐ **Codync just hit 1,100 stars on GitHub!**\n${thanks}Keep spreading the word on [X](`));+assert.match(posts[0], /\[Threads\]\(.+\) · \[Reddit\]\(.+url=https%3A%2F%2Fgithub.com%2Fleepokai%2FCodync.+\) · \[LinkedIn\]\(.+\), or wherever you hang out\.$/);+assert.equal(links.get("stars:milestone"), "1100");console.log("claim tests ok");diff --git a/discord/wrangler.toml b/discord/wrangler.tomlindex fdec2552..9bf10df0 100644--- a/discord/wrangler.toml+++ b/discord/wrangler.toml@@ -12,6 +12,8 @@ GUILD_ID = "1559449720315973682"CONTRIBUTOR_ROLE_ID = "1559453466894663680"REPORTER_ROLE_ID = "1559885097535148133"STAFF_CHANNEL_ID = "1559451211860672512"+MILESTONES_CHANNEL_ID = "1560254402168758403"+STAR_GOAL = "1000"REPO = "leepokai/Codync"[[kv_namespaces]]