fix(bots): auto-join server from channel_id so built-ins can post
Root cause: makeSender requires bot_servers membership, but create flow never auto-added bots when users only picked a channel. - Start() resolves config.channel_id → server and upserts bot_servers - Confess cursor uses (created_at,id) so deletes don't stall polling
This commit is contained in:
@@ -82,6 +82,10 @@ func (r *Runner) Start(botID, botType string, config json.RawMessage) {
|
||||
r.bots[botID] = cancel
|
||||
r.mu.Unlock()
|
||||
|
||||
// Built-in bots pick a channel in config but users often skip "Add to Server".
|
||||
// Resolve channel_id → server and ensure bot_servers so send() doesn't no-op.
|
||||
r.ensureBotServerFromConfig(botID, config)
|
||||
|
||||
send := r.makeSender(botID)
|
||||
r.logger.Info("starting built-in bot", "bot_id", botID, "type", botType)
|
||||
|
||||
@@ -96,6 +100,36 @@ func (r *Runner) Start(botID, botType string, config json.RawMessage) {
|
||||
}()
|
||||
}
|
||||
|
||||
// ensureBotServerFromConfig joins the bot to the server that owns config.channel_id.
|
||||
func (r *Runner) ensureBotServerFromConfig(botID string, config json.RawMessage) {
|
||||
var cfg struct {
|
||||
ChannelID string `json:"channel_id"`
|
||||
}
|
||||
if err := json.Unmarshal(config, &cfg); err != nil || cfg.ChannelID == "" {
|
||||
return
|
||||
}
|
||||
serverID, err := r.hub.ServerIDForChannel(context.Background(), cfg.ChannelID)
|
||||
if err != nil || serverID == "" {
|
||||
r.logger.Warn("bot start: channel not found for auto-join", "bot_id", botID, "channel_id", cfg.ChannelID, "error", err)
|
||||
return
|
||||
}
|
||||
var ownerID string
|
||||
if err := r.db.QueryRowContext(context.Background(),
|
||||
`SELECT owner_id FROM bots WHERE id = $1`, botID,
|
||||
).Scan(&ownerID); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err := r.db.ExecContext(context.Background(), `
|
||||
INSERT INTO bot_servers (bot_id, server_id, added_by)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT DO NOTHING
|
||||
`, botID, serverID, ownerID); err != nil {
|
||||
r.logger.Warn("bot start: auto-join server failed", "bot_id", botID, "server_id", serverID, "error", err)
|
||||
return
|
||||
}
|
||||
r.logger.Info("bot auto-joined server", "bot_id", botID, "server_id", serverID)
|
||||
}
|
||||
|
||||
// Stop stops a single bot by ID.
|
||||
func (r *Runner) Stop(botID string) {
|
||||
r.mu.Lock()
|
||||
|
||||
Reference in New Issue
Block a user