claptrap-go/discord/discord.go
2024-07-08 18:09:43 +02:00

43 lines
1.1 KiB
Go

package discord
import (
"sebclem/claptrapbot-go/config"
"github.com/bwmarrin/discordgo"
"go.uber.org/zap"
"gorm.io/gorm"
)
type Discord struct {
discordSession *discordgo.Session
db *gorm.DB
guildStatus map[string]*GuildStatus
}
var logger *zap.SugaredLogger
func NewDiscord(token string, db *gorm.DB) *Discord {
logger = config.Logger.Sugar()
dg, err := discordgo.New("Bot " + token)
if err != nil {
logger.Fatalw("Error creating Discord session", err)
}
discord := &Discord{discordSession: dg, db: db, guildStatus: map[string]*GuildStatus{}}
discord.initAllHandlers()
err = discord.discordSession.Open()
if err != nil {
logger.Fatalw("Error opening connection", err)
}
logger.Info("Bot is now running.")
logger.Infof("Connected to %d guilds\n", len(discord.discordSession.State.Guilds))
return discord
}
func (discord *Discord) initAllHandlers() {
discord.discordSession.AddHandler(guildCreateEvent)
discord.discordSession.AddHandler(func(s *discordgo.Session, r *discordgo.VoiceStateUpdate) {
VoiceStateUpdateEvent(s, r, discord.db, discord.guildStatus)
})
}