This commit is contained in:
+22
-2
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
"plumber/internal/events"
|
||||
"plumber/internal/mail"
|
||||
"plumber/internal/store"
|
||||
)
|
||||
|
||||
@@ -23,6 +24,10 @@ type Bot struct {
|
||||
channelID string
|
||||
links store.DiscordLinkStore
|
||||
api API
|
||||
store store.Store
|
||||
mail mail.Notifier
|
||||
admins map[string]string
|
||||
botUserID string
|
||||
}
|
||||
|
||||
// New constructs an outbound subscriber. Tests inject a fake API.
|
||||
@@ -31,7 +36,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) (*Bot, error) {
|
||||
func FromEnv(links store.DiscordLinkStore, bus *events.Bus, st store.Store, mailer mail.Notifier) (*Bot, error) {
|
||||
token := strings.TrimSpace(os.Getenv("DISCORD_BOT_TOKEN"))
|
||||
channelID := strings.TrimSpace(os.Getenv("DISCORD_CHANNEL_ID"))
|
||||
if token == "" && channelID == "" {
|
||||
@@ -50,11 +55,26 @@ func FromEnv(links store.DiscordLinkStore, bus *events.Bus) (*Bot, error) {
|
||||
if err != nil {
|
||||
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.admins = parseAdminMap(os.Getenv("DISCORD_ADMIN_MAP"))
|
||||
session.AddHandler(bot.onMessageCreate)
|
||||
if bus != nil {
|
||||
bus.Subscribe(bot.Handle)
|
||||
}
|
||||
log.Printf("discord: outbound subscriber enabled")
|
||||
if err := session.Open(); err != nil {
|
||||
_ = session.Close()
|
||||
return nil, fmt.Errorf("discord gateway: %w", err)
|
||||
}
|
||||
if session.State != nil && session.State.User != nil {
|
||||
bot.botUserID = session.State.User.ID
|
||||
}
|
||||
log.Printf("discord: subscriber enabled")
|
||||
return bot, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user