Publish Discord inbound replies on the post event bus (#19)
CI / test (push) Successful in 6m26s

Inbound publishes PostCreated after saving the Discord link. Outbound skips already-linked posts so the bot does not echo. Mail uses the existing subscriber.

Reviewed-on: #19
Co-authored-by: codegirl-007 <s.raide@gmail.com>
This commit was merged in pull request #19.
This commit is contained in:
2026-08-31 04:27:11 +00:00
committed by codegirl007
parent a1b0351048
commit e68afba6a5
5 changed files with 66 additions and 55 deletions
+11 -7
View File
@@ -13,7 +13,6 @@ import (
"github.com/bwmarrin/discordgo"
"plumber/internal/events"
"plumber/internal/mail"
"plumber/internal/store"
)
@@ -25,7 +24,7 @@ type Bot struct {
links store.DiscordLinkStore
api API
store store.Store
mail mail.Notifier
bus events.Publisher
admins map[string]string
botUserID string
}
@@ -36,7 +35,7 @@ func New(channelID string, links store.DiscordLinkStore, api API) *Bot {
}
// FromEnv builds a bot when Discord env is set. Missing config is a no-op.
func FromEnv(links store.DiscordLinkStore, bus *events.Bus, st store.Store, mailer mail.Notifier) (*Bot, error) {
func FromEnv(links store.DiscordLinkStore, bus *events.Bus, st store.Store) (*Bot, error) {
token := strings.TrimSpace(os.Getenv("DISCORD_BOT_TOKEN"))
channelID := strings.TrimSpace(os.Getenv("DISCORD_CHANNEL_ID"))
if token == "" && channelID == "" {
@@ -56,12 +55,9 @@ func FromEnv(links store.DiscordLinkStore, bus *events.Bus, st store.Store, mail
return nil, err
}
session.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentsMessageContent
if mailer == nil {
mailer = mail.Nop{}
}
bot := New(channelID, links, &sessionAPI{session: session})
bot.store = st
bot.mail = mailer
bot.bus = bus
bot.admins = parseAdminMap(os.Getenv("DISCORD_ADMIN_MAP"))
session.AddHandler(bot.onMessageCreate)
if bus != nil {
@@ -102,6 +98,14 @@ func (b *Bot) Handle(_ context.Context, ev any) {
}
func (b *Bot) onCreated(ctx context.Context, ev events.PostEvent) {
_, err := b.links.GetByPostID(ctx, ev.PostID)
if err == nil {
return
}
if !errors.Is(err, sql.ErrNoRows) {
log.Printf("discord: load link %s: %v", ev.PostID, err)
return
}
if isRoot(ev) {
b.createRoot(ctx, ev)
return