From 122b0633efc56ad28dbe590a0406540ba2ceddc0 Mon Sep 17 00:00:00 2001 From: SebClem Date: Mon, 8 Jul 2024 18:09:43 +0200 Subject: [PATCH] Init --- .gitignore | 26 +++++++ .vscode/launch.json | 15 ++++ config/database.go | 14 ++++ config/env.go | 53 +++++++++++++ config/logger.go | 16 ++++ discord/auto_voice.go | 142 +++++++++++++++++++++++++++++++++++ discord/discord.go | 42 +++++++++++ discord/guild_status.go | 27 +++++++ discord/handlers.go | 72 ++++++++++++++++++ go.mod | 60 +++++++++++++++ go.sum | 148 +++++++++++++++++++++++++++++++++++++ main.go | 41 ++++++++++ models/guild_preference.go | 11 +++ models/model.go | 8 ++ routers/router.go | 6 ++ utils/slices.go | 11 +++ 16 files changed, 692 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 config/database.go create mode 100644 config/env.go create mode 100644 config/logger.go create mode 100644 discord/auto_voice.go create mode 100644 discord/discord.go create mode 100644 discord/guild_status.go create mode 100644 discord/handlers.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 models/guild_preference.go create mode 100644 models/model.go create mode 100644 routers/router.go create mode 100644 utils/slices.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8c2a027 --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work +go.work.sum + +# env file +.env +__debug* diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..4debafe --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}/main.go", + } + ] +} diff --git a/config/database.go b/config/database.go new file mode 100644 index 0000000..d66f8bd --- /dev/null +++ b/config/database.go @@ -0,0 +1,14 @@ +package config + +import ( + "gorm.io/driver/postgres" + "gorm.io/gorm" +) + +func InitDB(config *Config) *gorm.DB { + db, err := gorm.Open(postgres.Open("host=" + config.DatabaseHost + " user=" + config.DatabaseUser + " password=" + config.DatabasePassword + " dbname=" + config.DatabaseName)) + if err != nil { + Logger.Sugar().Fatalw("Fail to connect to database", err) + } + return db +} diff --git a/config/env.go b/config/env.go new file mode 100644 index 0000000..f08ffd7 --- /dev/null +++ b/config/env.go @@ -0,0 +1,53 @@ +package config + +import ( + "os" + + "github.com/joho/godotenv" +) + +type Config struct { + DiscordToken string + DatabaseHost string + DatabasePort string + DatabaseName string + DatabaseUser string + DatabasePassword string +} + +func LoadEnv() *Config { + godotenv.Load() + missingEnv := make([]string, 0) + config := Config{} + + config.DiscordToken = os.Getenv("DISCORD_TOKEN") + if config.DiscordToken == "" { + missingEnv = append(missingEnv, "DISCORD_TOKEN") + } + + config.DatabaseHost = os.Getenv("DB_HOST") + if config.DatabaseHost == "" { + missingEnv = append(missingEnv, "DB_HOST") + } + config.DatabasePort = os.Getenv("DB_PORT") + if config.DatabasePort == "" { + config.DatabasePort = "5432" + } + config.DatabaseName = os.Getenv("DB_NAME") + if config.DatabaseName == "" { + missingEnv = append(missingEnv, "DB_NAME") + } + config.DatabaseUser = os.Getenv("DB_USER") + if config.DatabaseUser == "" { + missingEnv = append(missingEnv, "DB_USER") + } + config.DatabasePassword = os.Getenv("DB_PASSWORD") + if config.DatabasePassword == "" { + missingEnv = append(missingEnv, "DB_PASSWORD") + } + if len(missingEnv) != 0 { + InitLogger() + Logger.Sugar().Fatalw("Env var are missing, please add the fallowing missing env var", "MissingEnvVar", missingEnv) + } + return &config +} diff --git a/config/logger.go b/config/logger.go new file mode 100644 index 0000000..13761cd --- /dev/null +++ b/config/logger.go @@ -0,0 +1,16 @@ +package config + +import ( + "go.uber.org/zap" +) + +var Logger *zap.Logger + +func InitLogger() { + var err error + Logger, err = zap.NewDevelopment() + if err != nil { + panic(err) + } + defer Logger.Sync() +} diff --git a/discord/auto_voice.go b/discord/auto_voice.go new file mode 100644 index 0000000..d4c7c35 --- /dev/null +++ b/discord/auto_voice.go @@ -0,0 +1,142 @@ +package discord + +import ( + "fmt" + "sebclem/claptrapbot-go/utils" + "slices" + "sort" + "strings" + + "github.com/bwmarrin/discordgo" +) + +func FindNextVoiceIncrement(status *GuildStatus) int { + numbers := []int{} + for _, value := range status.AutoVoiceStatus.CreatedChannel { + numbers = append(numbers, value.Number) + } + sort.Ints(numbers) + next := 1 + + for _, value := range numbers { + if value == next { + next += 1 + } else { + break + } + } + return next +} + +func GetConnectedUserCountInVoiceChannel(s *discordgo.Session, guildID string, channelID string) (int, error) { + guildState, err := s.State.Guild(guildID) + if err != nil { + logger.Errorw("Fail retreve guild status", err, "GuildId", guildID) + return -1, err + } + count := 0 + for _, vs := range guildState.VoiceStates { + if vs.ChannelID == channelID { + count++ + } + } + return count, nil +} + +func CreateAutoVoiceChannel(s *discordgo.Session, guildStatus *GuildStatus, sourceChannelID string, channelNameTemplate string) (*discordgo.Channel, error) { + nextNumber := FindNextVoiceIncrement(guildStatus) + channel, err := s.Channel(sourceChannelID) + if err != nil { + logger.Errorw("Fail to retreve channel information", err, "ChannelID", sourceChannelID) + return nil, err + } + newPosition := channel.Position + nextNumber + channels, err := s.GuildChannels(channel.GuildID) + if err != nil { + logger.Errorw("Fail retrevie channel list", err) + return nil, err + } + // TODO Reoder with list index to prevent accumulated error + for _, thisChannel := range channels { + if thisChannel.ParentID == channel.ParentID { + logger.Debug(thisChannel.Position) + } + if thisChannel.ParentID == channel.ParentID && thisChannel.Position >= newPosition { + thisChannel.Position++ + } + } + err = s.GuildChannelsReorder(channel.GuildID, channels) + if err != nil { + logger.Errorw("Fail to reorde channels", err) + return nil, err + } + channelName := strings.ReplaceAll(channelNameTemplate, "@count", fmt.Sprint(nextNumber)) + created, err := s.GuildChannelCreateComplex(channel.GuildID, discordgo.GuildChannelCreateData{ + Bitrate: channel.Bitrate, + Name: channelName, + Position: newPosition, + PermissionOverwrites: channel.PermissionOverwrites, + ParentID: channel.ParentID, + RateLimitPerUser: channel.RateLimitPerUser, + Type: discordgo.ChannelTypeGuildVoice, + UserLimit: channel.UserLimit, + }) + if err != nil { + logger.Errorw("Fail to create Auto voice channel", err, "GuildId", channel.GuildID) + return nil, err + } + guildStatus.AutoVoiceStatus.CreatedChannel[created.ID] = &AutoVoiceChannelCreated{ID: created.ID, Number: nextNumber, Position: newPosition} + return created, nil +} + +func AutoVoiceChannelDisconect(s *discordgo.Session, guildStatus *GuildStatus, guildID string, channelID string) { + inVC, err := GetConnectedUserCountInVoiceChannel(s, guildID, channelID) + if err != nil { + return + } + if inVC == 0 { + channel, err := s.Channel(channelID) + if err != nil { + logger.Errorw("Fail to retreve channel information", err, "ChannelID", channelID) + return + } + + logger.Debug("Channel is empty, deleting it.") + _, err = s.ChannelDelete(channel.ID) + if err != nil { + logger.Errorw("Fail to delete auto voice channel", + err, "GuildId", channel.GuildID, + "ChannelID", channel.ID) + return + } + channels, err := s.GuildChannels(channel.GuildID) + if err != nil { + logger.Errorw("Fail retrevie channel list", err) + return + } + filtered := utils.Filter(channels, func(c *discordgo.Channel) bool { return c.ParentID == channel.ParentID }) + ReoderChannels(s, filtered, guildID) + delete(guildStatus.AutoVoiceStatus.CreatedChannel, channelID) + } +} + +func ReoderChannels(s *discordgo.Session, channels []*discordgo.Channel, guildID string) error { + slices.SortFunc(channels, func(a *discordgo.Channel, b *discordgo.Channel) int { + if a.Position < b.Position { + return -1 + } else if a.Position > b.Position { + return 1 + } else { + return 0 + } + }) + for i, filtered := range channels { + filtered.Position = i + } + err := s.GuildChannelsReorder(guildID, channels) + if err != nil { + logger.Errorw("Fail to reorde channels", err) + return err + } + return nil +} diff --git a/discord/discord.go b/discord/discord.go new file mode 100644 index 0000000..fcccefa --- /dev/null +++ b/discord/discord.go @@ -0,0 +1,42 @@ +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) + }) +} diff --git a/discord/guild_status.go b/discord/guild_status.go new file mode 100644 index 0000000..be3b870 --- /dev/null +++ b/discord/guild_status.go @@ -0,0 +1,27 @@ +package discord + +type GuildStatus struct { + AutoVoiceStatus *AutoVoiceGuildStatus +} + +func NewGuildStatus() *GuildStatus { + return &GuildStatus{ + AutoVoiceStatus: NewAutoVoiceGuildStatus(), + } +} + +type AutoVoiceGuildStatus struct { + CreatedChannel map[string]*AutoVoiceChannelCreated +} + +func NewAutoVoiceGuildStatus() *AutoVoiceGuildStatus { + return &AutoVoiceGuildStatus{ + CreatedChannel: map[string]*AutoVoiceChannelCreated{}, + } +} + +type AutoVoiceChannelCreated struct { + ID string + Number int + Position int +} diff --git a/discord/handlers.go b/discord/handlers.go new file mode 100644 index 0000000..b275ea1 --- /dev/null +++ b/discord/handlers.go @@ -0,0 +1,72 @@ +package discord + +import ( + "sebclem/claptrapbot-go/models" + "sebclem/claptrapbot-go/utils" + + "github.com/bwmarrin/discordgo" + "gorm.io/gorm" +) + +func guildCreateEvent(s *discordgo.Session, r *discordgo.GuildCreate) { + logger.Infow("New guild ready", "name", r.Name, "ID", r.ID) +} + +func VoiceStateUpdateEvent(s *discordgo.Session, r *discordgo.VoiceStateUpdate, db *gorm.DB, status map[string]*GuildStatus) { + var guildPreference models.GuildPreference + result := db.Where(&models.GuildPreference{ID: r.GuildID}).FirstOrCreate(&guildPreference) + if result.Error != nil { + logger.Debugw("Can't retreve guild preference", result.Error) + return + } + if guildPreference.AutoVoiceEnabled { + thisStatus, ok := status[r.GuildID] + if !ok { + thisStatus = NewGuildStatus() + status[r.GuildID] = thisStatus + + } + // Filter connect/disconned/move event + if r.BeforeUpdate == nil || r.BeforeUpdate.ChannelID != r.ChannelID { + // Connected or moved to new channel + if r.ChannelID != "" { + logger.Debugw("Someone joinded a channel", "GuildID", r.GuildID, "ChannelID", r.ChannelID) + if r.ChannelID == guildPreference.AutoVoiceSourceChannelId { + logger.Infow("Member join auto voice channel, creating new channel", + "user", r.Member.DisplayName(), + "channelID", r.ChannelID, + ) + + created, err := CreateAutoVoiceChannel(s, thisStatus, r.ChannelID, guildPreference.AutoVoiceCreatedChannelName) + if err == nil { + err = s.GuildMemberMove(created.GuildID, r.UserID, &created.ID) + if err != nil { + logger.Errorw("Fail to move user to created channel", err, + "GuildId", r.GuildID, + "SoucerChannelID", r.ChannelID, + "DestChannelID", created.ID) + } + channels, err := s.GuildChannels(r.GuildID) + if err != nil { + logger.Errorw("Fail retrevie channel list", err) + return + } + filtered := utils.Filter(channels, func(c *discordgo.Channel) bool { return c.ParentID == created.ParentID }) + ReoderChannels(s, filtered, r.GuildID) + } + + } + } + // Disconnected or moved to new channel + if r.BeforeUpdate != nil { + logger.Debugw("Someone leaved a channel", "GuildID", r.BeforeUpdate.GuildID, "ChannelID", r.BeforeUpdate.ChannelID) + + _, ok := thisStatus.AutoVoiceStatus.CreatedChannel[r.BeforeUpdate.ChannelID] + if ok { + logger.Debugw("This is a auto created channel", "ChannelID", r.BeforeUpdate.ChannelID) + AutoVoiceChannelDisconect(s, thisStatus, r.BeforeUpdate.GuildID, r.BeforeUpdate.ChannelID) + } + } + } + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e50aa09 --- /dev/null +++ b/go.mod @@ -0,0 +1,60 @@ +module sebclem/claptrapbot-go + +go 1.22.4 + +require ( + github.com/gin-gonic/gin v1.10.0 + github.com/joho/godotenv v1.5.1 +) + +require ( + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/google/go-cmp v0.5.9 // indirect + github.com/gorilla/websocket v1.4.2 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/pgx/v5 v5.5.5 // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/kr/pretty v0.3.1 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect + go.uber.org/multierr v1.11.0 // indirect + golang.org/x/sync v0.6.0 // indirect +) + +require ( + github.com/bwmarrin/discordgo v0.28.1 + github.com/bytedance/sonic v1.11.6 // indirect + github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/gin-contrib/zap v1.1.3 + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.20.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.7 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/rs/zerolog v1.33.0 + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + go.uber.org/zap v1.27.0 + golang.org/x/arch v0.8.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/text v0.15.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect + gorm.io/driver/postgres v1.5.9 + gorm.io/gorm v1.25.10 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ac8d975 --- /dev/null +++ b/go.sum @@ -0,0 +1,148 @@ +github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4= +github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= +github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= +github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= +github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-contrib/zap v1.1.3 h1:9e/U9fYd4/OBfmSEBs5hHZq114uACn7bpuzvCkcJySA= +github.com/gin-contrib/zap v1.1.3/go.mod h1:+BD/6NYZKJyUpqVoJEvgeq9GLz8pINEQvak9LHNOTSE= +github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= +github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= +github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw= +github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= +github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= +github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= +golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= +golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8= +gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI= +gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s= +gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/main.go b/main.go new file mode 100644 index 0000000..be5b565 --- /dev/null +++ b/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "flag" + "sebclem/claptrapbot-go/config" + "sebclem/claptrapbot-go/discord" + "sebclem/claptrapbot-go/models" + "time" + + ginzap "github.com/gin-contrib/zap" + "github.com/gin-gonic/gin" +) + +var ( + Token string +) + +func init() { + + flag.StringVar(&Token, "t", "", "Bot Token") + flag.Parse() +} + +func main() { + appConf := config.LoadEnv() + config.InitLogger() + + db := config.InitDB(appConf) + models.Init(db) + discord.NewDiscord(appConf.DiscordToken, db) + + r := gin.Default() + r.Use(ginzap.Ginzap(config.Logger, time.RFC3339, true)) + r.Use(ginzap.RecoveryWithZap(config.Logger, true)) + r.GET("/ping", func(c *gin.Context) { + c.JSON(200, gin.H{ + "message": "pong", + }) + }) + r.Run() // listen and serve on 0.0.0.0:8080 +} diff --git a/models/guild_preference.go b/models/guild_preference.go new file mode 100644 index 0000000..e4ea478 --- /dev/null +++ b/models/guild_preference.go @@ -0,0 +1,11 @@ +package models + +import "gorm.io/gorm" + +type GuildPreference struct { + gorm.Model + ID string + AutoVoiceEnabled bool `gorm:"default:false"` + AutoVoiceSourceChannelId string + AutoVoiceCreatedChannelName string +} diff --git a/models/model.go b/models/model.go new file mode 100644 index 0000000..6e9a722 --- /dev/null +++ b/models/model.go @@ -0,0 +1,8 @@ +package models + +import "gorm.io/gorm" + +func Init(db *gorm.DB) { + db.AutoMigrate(&GuildPreference{}) + +} diff --git a/routers/router.go b/routers/router.go new file mode 100644 index 0000000..a7aa209 --- /dev/null +++ b/routers/router.go @@ -0,0 +1,6 @@ +package routers + +import "github.com/gin-gonic/gin" + +func RegisterRoutes(router *gin.Engine) { +} diff --git a/utils/slices.go b/utils/slices.go new file mode 100644 index 0000000..a94d21d --- /dev/null +++ b/utils/slices.go @@ -0,0 +1,11 @@ +package utils + +func Filter[T any](s []T, f func(T) bool) []T { + var r []T + for _, v := range s { + if f(v) { + r = append(r, v) + } + } + return r +}