brainiac-discord 0.0.24 → 0.0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: db12217443184c5a320346202b5a0198d49c16bedd126fa803e6267fedd98442
|
|
4
|
+
data.tar.gz: 00011f60d94207ae5562f125b5733a330b1699d23404343ae67c3df4f37a5c4b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d7b3b73a0e90e08e95ed628b3d255e7debbe7ecf428ae445db363e646bb5b7c4a33714244dee5d00cdf6bfb68eec315799ffd9f951d27a2ba0de9e84a3bcb412
|
|
7
|
+
data.tar.gz: 06b9e071b586e338e6067ee04d633b1586f7fa5dc87b53188bdfbef36036564e563d89ff63d80cde02d61a58565e06f6fdb93e43a0ad9a34fecf594697f251c7
|
data/README.md
CHANGED
|
@@ -33,30 +33,52 @@ brainiac restart
|
|
|
33
33
|
|
|
34
34
|
## Setup
|
|
35
35
|
|
|
36
|
-
###
|
|
36
|
+
### Adding an agent (recommended)
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
brainiac agent create "Galen" --local --avatar galen.png --description-file galen.md
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Discord has no API for creating applications, so there's one manual step: the plugin opens the [developer portal](https://discord.com/developers/applications), you click **New Application** (name it after the agent) → **Bot** → **Reset Token**, and paste the token once. Everything else is automated:
|
|
43
|
+
|
|
44
|
+
- Bot username and avatar, app icon and description (from `--avatar` / `--description-file`)
|
|
45
|
+
- **Message Content** intent enabled
|
|
46
|
+
- `DISCORD_BOT_TOKEN` saved to the agent's `env` in `~/.brainiac/agents.json`
|
|
47
|
+
- The bot's user ID added to `user_mappings` in `~/.brainiac/discord.json`
|
|
48
|
+
- Server invite link printed and opened
|
|
49
|
+
|
|
50
|
+
A running server starts the new bot immediately — no restart needed.
|
|
51
|
+
|
|
52
|
+
For an existing agent, an interrupted create, or after installing this plugin, run `brainiac agent setup galen` (add `--only discord` to run just this plugin). It's safe to re-run: steps already done are skipped, and if the token is lost it sends you straight to the existing app's Bot tab. Use `--no-browser` to print links instead of opening them, or `--skip discord` to leave Discord out.
|
|
53
|
+
|
|
54
|
+
### Manual setup (alternative)
|
|
55
|
+
|
|
56
|
+
#### 1. Create Discord Applications
|
|
37
57
|
|
|
38
58
|
Create one Discord application per agent at https://discord.com/developers/applications:
|
|
39
59
|
|
|
40
60
|
1. Click "New Application", name it after the agent
|
|
41
61
|
2. Go to "Bot" tab → enable **Message Content Intent**
|
|
42
|
-
3.
|
|
62
|
+
3. Click "Reset Token" and copy the bot token
|
|
43
63
|
|
|
44
|
-
|
|
64
|
+
#### 2. Register Tokens
|
|
45
65
|
|
|
46
66
|
```bash
|
|
47
67
|
brainiac discord token galen "BOT_TOKEN_FOR_GALEN"
|
|
48
68
|
brainiac discord token glados "BOT_TOKEN_FOR_GLADOS"
|
|
49
69
|
```
|
|
50
70
|
|
|
51
|
-
|
|
71
|
+
This only sets `DISCORD_BOT_TOKEN`; add the bot's user ID to `user_mappings` (see [Configuration](#configuration)) so other agents can @mention it. Restart a running server (`brainiac restart`) to pick up new tokens.
|
|
72
|
+
|
|
73
|
+
#### 3. Invite Bots
|
|
52
74
|
|
|
53
75
|
Use the OAuth2 URL Generator with `bot` scope and these permissions:
|
|
54
|
-
- Send Messages,
|
|
55
|
-
- Add Reactions,
|
|
76
|
+
- View Channels, Send Messages, Embed Links, Attach Files, Read Message History
|
|
77
|
+
- Add Reactions, Manage Threads, Create Public Threads, Send Messages in Threads
|
|
56
78
|
|
|
57
|
-
Permission integer: `
|
|
79
|
+
Permission integer: `326417632320`
|
|
58
80
|
|
|
59
|
-
###
|
|
81
|
+
### Configure
|
|
60
82
|
|
|
61
83
|
Set a default project:
|
|
62
84
|
```bash
|
|
@@ -68,7 +90,7 @@ Map channels to projects:
|
|
|
68
90
|
brainiac discord map 1234567890 brainiac
|
|
69
91
|
```
|
|
70
92
|
|
|
71
|
-
###
|
|
93
|
+
### Start
|
|
72
94
|
|
|
73
95
|
```bash
|
|
74
96
|
brainiac server
|
|
@@ -76,6 +98,18 @@ brainiac server
|
|
|
76
98
|
|
|
77
99
|
All bots connect automatically as background threads.
|
|
78
100
|
|
|
101
|
+
## Commands
|
|
102
|
+
|
|
103
|
+
```
|
|
104
|
+
brainiac discord setup Check config and print next steps
|
|
105
|
+
brainiac discord config Show discord.json
|
|
106
|
+
brainiac discord default <project> Set default project for all channels
|
|
107
|
+
brainiac discord map <channel-id> <project> Map a specific channel to a project
|
|
108
|
+
brainiac discord owner [<discord-user-id>] Set/show machine owner
|
|
109
|
+
brainiac discord token <agent-key> <token> Set an agent's DISCORD_BOT_TOKEN
|
|
110
|
+
brainiac discord status Bot status (via the running server)
|
|
111
|
+
```
|
|
112
|
+
|
|
79
113
|
## Configuration
|
|
80
114
|
|
|
81
115
|
Stored in `~/.brainiac/discord.json`:
|
|
@@ -97,6 +131,13 @@ Stored in `~/.brainiac/discord.json`:
|
|
|
97
131
|
}
|
|
98
132
|
```
|
|
99
133
|
|
|
134
|
+
- `user_mappings` — display name → Discord user ID, for humans and agent bots (`agent create` adds the bot's entry)
|
|
135
|
+
- `authorized_user_ids` / `authorized_role_ids` — who may talk to the agents; both empty allows everyone
|
|
136
|
+
- `role_mappings` — optional agent display name → Discord role ID, so @mentioning a role reaches that agent (when set, these role IDs replace `authorized_role_ids`)
|
|
137
|
+
- `klipy_api_key` — enables GIF search (`giphy_api_key` is still read as a fallback)
|
|
138
|
+
|
|
139
|
+
Bot tokens live in `~/.brainiac/agents.json` as each agent's `env.DISCORD_BOT_TOKEN`, not in `discord.json`.
|
|
140
|
+
|
|
100
141
|
## Development
|
|
101
142
|
|
|
102
143
|
```bash
|
|
@@ -41,10 +41,12 @@ module Brainiac
|
|
|
41
41
|
|
|
42
42
|
if token && discord_config.dig("user_mappings", display_name)
|
|
43
43
|
puts " [Discord] ✓ #{display_name} already has a bot (skipping)"
|
|
44
|
+
sync_installed_apps(token)
|
|
44
45
|
return
|
|
45
46
|
end
|
|
46
47
|
|
|
47
|
-
|
|
48
|
+
existing_id = discord_config.dig("user_mappings", display_name)
|
|
49
|
+
token ||= prompt_for_token(display_name, entry["open_browser"], existing_id)
|
|
48
50
|
return unless token
|
|
49
51
|
|
|
50
52
|
me = request(:get, "/users/@me", token)
|
|
@@ -64,19 +66,79 @@ module Brainiac
|
|
|
64
66
|
puts " [Discord] Invite #{display_name} to your server:"
|
|
65
67
|
puts " #{invite_url(app["id"] || me["id"])}"
|
|
66
68
|
open_url(invite_url(app["id"] || me["id"])) if entry["open_browser"]
|
|
69
|
+
|
|
70
|
+
sync_installed_apps(token)
|
|
67
71
|
end
|
|
68
72
|
|
|
69
73
|
private
|
|
70
74
|
|
|
71
|
-
|
|
75
|
+
# Backfill user_mappings for every OTHER app/bot already installed in the
|
|
76
|
+
# guild — the ones this brainiac instance didn't create. Without this an
|
|
77
|
+
# agent only knows about bots it provisioned itself, so a tag like
|
|
78
|
+
# "@Judge Dredd" for an app from another instance renders as plain text
|
|
79
|
+
# with no ID to route to. Sweeping the guild's members (bots included)
|
|
80
|
+
# gives each installed app a name→id mapping so the mention roster and
|
|
81
|
+
# tag detection at least know it exists.
|
|
82
|
+
#
|
|
83
|
+
# Best-effort and non-fatal: needs the SERVER MEMBERS intent and a
|
|
84
|
+
# guild_id in discord.json. Only adds names not already mapped; never
|
|
85
|
+
# overwrites a hand-set entry.
|
|
86
|
+
def sync_installed_apps(token)
|
|
87
|
+
guild_id = discord_config["guild_id"]
|
|
88
|
+
return unless guild_id
|
|
89
|
+
|
|
90
|
+
bots = fetch_guild_bots(guild_id, token)
|
|
91
|
+
return if bots.nil?
|
|
92
|
+
|
|
93
|
+
existing = discord_config["user_mappings"] || {}
|
|
94
|
+
added = bots.reject { |name, _id| existing.key?(name) }
|
|
95
|
+
if added.empty?
|
|
96
|
+
puts " [Discord] ✓ Installed apps already mapped (#{bots.size} bot(s) in server)"
|
|
97
|
+
return
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
added.each { |name, id| save_user_mapping(name, id) }
|
|
101
|
+
puts " [Discord] ✓ Discovered #{added.size} installed app(s): #{added.keys.join(", ")}"
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Returns { bot_display_name => user_id } for every bot in the guild, or
|
|
105
|
+
# nil when the member list can't be fetched (missing intent, bad token,
|
|
106
|
+
# no permission). Humans are skipped — only user["bot"] members count.
|
|
107
|
+
def fetch_guild_bots(guild_id, token)
|
|
108
|
+
members = request(:get, "/guilds/#{guild_id}/members?limit=1000", token)
|
|
109
|
+
unless members.is_a?(Array)
|
|
110
|
+
puts " [Discord] ⚠ Couldn't list installed apps — enable the SERVER MEMBERS intent (Bot tab → Privileged Gateway Intents)"
|
|
111
|
+
return nil
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
members.each_with_object({}) do |member, acc|
|
|
115
|
+
user = member["user"]
|
|
116
|
+
next unless user && user["bot"]
|
|
117
|
+
|
|
118
|
+
name = user["global_name"] || user["username"]
|
|
119
|
+
id = user["id"]
|
|
120
|
+
acc[name] = id if name && id
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# With a user_mappings entry the app already exists (the token was lost
|
|
125
|
+
# or rotated), so send the user straight to its Bot tab instead of
|
|
126
|
+
# "New Application". A bot's user ID is its application ID.
|
|
127
|
+
def prompt_for_token(display_name, browser, existing_id = nil)
|
|
72
128
|
return nil unless $stdin.tty?
|
|
73
129
|
|
|
130
|
+
url = existing_id ? "#{PORTAL_URL}/#{existing_id}/bot" : PORTAL_URL
|
|
74
131
|
puts ""
|
|
75
|
-
puts " [Discord] Opening the developer portal in your browser…" if browser && open_url(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
132
|
+
puts " [Discord] Opening the developer portal in your browser…" if browser && open_url(url)
|
|
133
|
+
if existing_id
|
|
134
|
+
puts " [Discord] #{display_name} already has a Discord app, but its bot token isn't saved."
|
|
135
|
+
puts " #{url} → Reset Token → copy it"
|
|
136
|
+
else
|
|
137
|
+
puts " [Discord] Create a bot for #{display_name} (the only manual step):"
|
|
138
|
+
puts " 1. #{PORTAL_URL} → New Application → name it \"#{display_name}\""
|
|
139
|
+
puts " 2. Bot tab → Reset Token → copy it"
|
|
140
|
+
puts " Name, avatar, description and Message Content intent are set automatically."
|
|
141
|
+
end
|
|
80
142
|
print " Bot token (blank to skip): "
|
|
81
143
|
token = read_secret
|
|
82
144
|
puts ""
|
|
@@ -76,6 +76,9 @@ module Brainiac
|
|
|
76
76
|
content = msg["content"]&.strip || ""
|
|
77
77
|
next if content.empty?
|
|
78
78
|
|
|
79
|
+
# Tag bot authors so intent can see agents this machine doesn't run.
|
|
80
|
+
# Webhook posts (GitHub, CI) are bots too, but they aren't agents.
|
|
81
|
+
author = "#{author} [bot]" if msg.dig("author", "bot") && !msg["webhook_id"]
|
|
79
82
|
"#{author}: #{content}"
|
|
80
83
|
end
|
|
81
84
|
|
|
@@ -179,6 +179,8 @@ module Brainiac
|
|
|
179
179
|
# in this message. When a user @mentions someone specific, thread participants
|
|
180
180
|
# who weren't mentioned should stay silent — no intent check needed.
|
|
181
181
|
def other_agent_mentioned?(mentions, content, agent_key)
|
|
182
|
+
return true if remote_bot_mentioned?(mentions)
|
|
183
|
+
|
|
182
184
|
mention_roles = []
|
|
183
185
|
|
|
184
186
|
Gateway.each_bot do |key, info|
|
|
@@ -206,6 +208,13 @@ module Brainiac
|
|
|
206
208
|
false
|
|
207
209
|
end
|
|
208
210
|
|
|
211
|
+
# Any bot in the mentions array counts as another agent, even one this machine
|
|
212
|
+
# has never heard of (agents run on different machines, each with its own roster).
|
|
213
|
+
# Callers only get here when this bot wasn't mentioned, so self is never in the list.
|
|
214
|
+
def remote_bot_mentioned?(mentions)
|
|
215
|
+
mentions.any? { |m| m["bot"] }
|
|
216
|
+
end
|
|
217
|
+
|
|
209
218
|
def validate_cross_agent_dispatch(sender_agent_key, agent_key, mentioned, content, channel_id)
|
|
210
219
|
return false unless mentioned
|
|
211
220
|
|
|
@@ -262,15 +271,17 @@ module Brainiac
|
|
|
262
271
|
def should_stand_down?(in_own_thread, mentioned, is_reply_to_bot, is_bot, agent_key, mentions, content)
|
|
263
272
|
return false unless in_own_thread && !mentioned && !is_reply_to_bot && !is_bot
|
|
264
273
|
|
|
265
|
-
other_bot_mentioned =
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
274
|
+
other_bot_mentioned = remote_bot_mentioned?(mentions)
|
|
275
|
+
unless other_bot_mentioned
|
|
276
|
+
Gateway.each_bot do |key, info|
|
|
277
|
+
next if key == agent_key
|
|
278
|
+
next unless info[:user_id]
|
|
279
|
+
next unless mentions.any? { |m| m["id"].to_s == info[:user_id].to_s } ||
|
|
280
|
+
content.match?(/<@!?#{Regexp.escape(info[:user_id].to_s)}>/)
|
|
271
281
|
|
|
272
|
-
|
|
273
|
-
|
|
282
|
+
other_bot_mentioned = true
|
|
283
|
+
break
|
|
284
|
+
end
|
|
274
285
|
end
|
|
275
286
|
|
|
276
287
|
unless other_bot_mentioned
|
|
@@ -311,39 +322,18 @@ module Brainiac
|
|
|
311
322
|
|
|
312
323
|
# Check if this agent is the only bot in the thread (1-on-1 with the human).
|
|
313
324
|
# Uses the already-fetched channel_history to avoid extra API calls.
|
|
314
|
-
# channel_history is a formatted string ("Username: message\n...") from fetch_channel_history
|
|
315
|
-
#
|
|
316
|
-
#
|
|
325
|
+
# channel_history is a formatted string ("Username: message\n...") from fetch_channel_history,
|
|
326
|
+
# with bot authors tagged "Username [bot]: ". Any bot other than us counts, including
|
|
327
|
+
# agents running on another machine that this one doesn't know about.
|
|
317
328
|
def solo_in_thread?(channel_history, agent_key)
|
|
318
329
|
return true unless channel_history.is_a?(String) && !channel_history.empty?
|
|
319
330
|
|
|
320
|
-
|
|
321
|
-
Gateway.each_bot
|
|
322
|
-
next if key == agent_key
|
|
323
|
-
next unless info[:username]
|
|
324
|
-
|
|
325
|
-
other_bot_usernames << info[:username]
|
|
326
|
-
end
|
|
327
|
-
|
|
328
|
-
# Also check user_mappings for remote agent bots not connected locally
|
|
329
|
-
agent_keys = []
|
|
330
|
-
Gateway.each_bot { |key, _| agent_keys << key }
|
|
331
|
-
Config.user_mappings.each_key do |name|
|
|
332
|
-
normalized = name.downcase.gsub(/[^a-z0-9]/, "-")
|
|
333
|
-
next if normalized == agent_key
|
|
334
|
-
next if agent_keys.include?(normalized)
|
|
335
|
-
|
|
336
|
-
# Only include names that look like agent bots (present in agent registry)
|
|
337
|
-
next unless defined?(AGENT_REGISTRY) && AGENT_REGISTRY.key?(normalized)
|
|
338
|
-
|
|
339
|
-
other_bot_usernames << name
|
|
340
|
-
end
|
|
341
|
-
|
|
342
|
-
return true if other_bot_usernames.empty?
|
|
331
|
+
own_username = nil
|
|
332
|
+
Gateway.each_bot { |key, info| own_username = info[:username] if key == agent_key }
|
|
343
333
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
334
|
+
channel_history.split("\n").none? do |line|
|
|
335
|
+
author = line[/\A(.+?) \[bot\]: /, 1]
|
|
336
|
+
author && author != own_username
|
|
347
337
|
end
|
|
348
338
|
end
|
|
349
339
|
|