This commit is contained in:
+11
-7
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user