From 5f153522e82f799590c7c479f970e8373f3bf614 Mon Sep 17 00:00:00 2001 From: Olaussen Date: Sat, 15 Jul 2023 19:13:44 +0200 Subject: [PATCH] fix: read env --- db/init.go | 12 +++++++++++ interactions/commands/play.go | 14 +++++++++++++ .../commands.go => interactions/loader.go | 20 ++++++++++++++++--- main.go | 12 +++++------ models/config.go | 1 + music/audio.go | 4 ---- utils/config.go | 10 +++++++--- 7 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 db/init.go create mode 100644 interactions/commands/play.go rename commands/commands.go => interactions/loader.go (54%) diff --git a/db/init.go b/db/init.go new file mode 100644 index 0000000..0bffe83 --- /dev/null +++ b/db/init.go @@ -0,0 +1,12 @@ +package db + +import ( + "fmt" + "juanitamusic/models" +) + +func Init(config *models.Config) { + connectionString := config.MONGOURL + + fmt.Println(connectionString) +} diff --git a/interactions/commands/play.go b/interactions/commands/play.go new file mode 100644 index 0000000..fa07d60 --- /dev/null +++ b/interactions/commands/play.go @@ -0,0 +1,14 @@ +package commands + +import ( + "github.com/bwmarrin/discordgo" +) + +func Play(session *discordgo.Session, interaction *discordgo.InteractionCreate) { + session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Pong!", + }, + }) +} diff --git a/commands/commands.go b/interactions/loader.go similarity index 54% rename from commands/commands.go rename to interactions/loader.go index 4b8d530..f931aea 100644 --- a/commands/commands.go +++ b/interactions/loader.go @@ -1,6 +1,12 @@ -package commands +package interactions -import "github.com/bwmarrin/discordgo" +import ( + "juanitamusic/db" + "juanitamusic/interactions/commands" + "juanitamusic/models" + + "github.com/bwmarrin/discordgo" +) func LoadCommands() []*discordgo.ApplicationCommand { return []*discordgo.ApplicationCommand{ @@ -8,10 +14,15 @@ func LoadCommands() []*discordgo.ApplicationCommand { Name: "ping", Description: "Will pong you back!", }, + { + Name: "play", + Description: "Will pong you back!", + }, } } -func LoadCommandHandlers() map[string]func(session *discordgo.Session, interaction *discordgo.InteractionCreate) { +func LoadCommandHandlers(config *models.Config) map[string]func(session *discordgo.Session, interaction *discordgo.InteractionCreate) { + db.Init(config) return map[string]func(session *discordgo.Session, interaction *discordgo.InteractionCreate){ "ping": func(session *discordgo.Session, interaction *discordgo.InteractionCreate) { session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ @@ -21,5 +32,8 @@ func LoadCommandHandlers() map[string]func(session *discordgo.Session, interacti }, }) }, + "play": func(session *discordgo.Session, interaction *discordgo.InteractionCreate) { + commands.Play(session, interaction) + }, } } diff --git a/main.go b/main.go index d9cd91d..c3f7ee4 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ package main import ( - "juanitamusic/commands" + "juanitamusic/interactions" "juanitamusic/utils" "log" "os" @@ -11,15 +11,15 @@ import ( ) var ( - config = utils.LoadConfig() + Config = utils.LoadConfig() session *discordgo.Session - cmds = commands.LoadCommands() - cmdHandlers = commands.LoadCommandHandlers() + cmds = interactions.LoadCommands() + cmdHandlers = interactions.LoadCommandHandlers(&Config) ) func init() { var err error - session, err = discordgo.New("Bot " + config.JUANITA_TOKEN) + session, err = discordgo.New("Bot " + Config.JUANITA_TOKEN) if err != nil { log.Fatalf("Failed to start Discord session! Error: %v", err) } @@ -48,7 +48,7 @@ func main() { log.Println("Registering commands...") regCmds := make([]*discordgo.ApplicationCommand, len(cmds)) for i, v := range cmds { - cmd, err := session.ApplicationCommandCreate(session.State.User.ID, config.TEST_GUILD, v) + cmd, err := session.ApplicationCommandCreate(session.State.User.ID, Config.TEST_GUILD, v) if err != nil { log.Fatalf("Failed to register command %v! Error: %v", v.Name, err) } diff --git a/models/config.go b/models/config.go index 4b0566a..083ea8b 100644 --- a/models/config.go +++ b/models/config.go @@ -5,4 +5,5 @@ type Config struct { JUANITA_ID string `json:"id"` TEST_GUILD string `json:"test_guild"` REMOVE_COMMANDS_ON_SHUTDOWN bool `json:"remove_commands_on_shutdown"` + MONGOURL string `json:"mongourl"` } diff --git a/music/audio.go b/music/audio.go index 77aa698..23a08ac 100644 --- a/music/audio.go +++ b/music/audio.go @@ -65,10 +65,8 @@ func GlobalPlay(songSig chan InstanceSong) { } func (v *VoiceInstance) PlayQueue(song Song) { - // add song to queue v.QueueAdd(song) if v.speaking { - // the bot is playing return } go func() { @@ -76,11 +74,9 @@ func (v *VoiceInstance) PlayQueue(song Song) { defer v.audioMutex.Unlock() for { if len(v.queue) == 0 { - //ChMessageSend(v.nowPlaying.ChannelID, "[**Music**] End of queue!") return } v.nowPlaying = v.QueueGetSong() - //go ChMessageSend(v.nowPlaying.ChannelID, "[**Music**] Playing, **`"+ v.nowPlaying.Title+"` - `("+v.nowPlaying.Duration+")` - **<@"+v.nowPlaying.ID+">\n") //*`"+ v.nowPlaying.User +"`***") v.stop = false v.skip = false diff --git a/utils/config.go b/utils/config.go index 4bacc5a..dd1d8dc 100644 --- a/utils/config.go +++ b/utils/config.go @@ -8,14 +8,18 @@ import ( ) func LoadConfig() models.Config { - godotenv.Load() + err := godotenv.Load() - config := models.Config{ + if err != nil { + panic(err) + } + + return models.Config{ JUANITA_TOKEN: os.Getenv("JUANITA_TOKEN"), JUANITA_ID: os.Getenv("JUANITA_ID"), TEST_GUILD: os.Getenv("TEST_GUILD"), REMOVE_COMMANDS_ON_SHUTDOWN: os.Getenv("TEST_GUILD") != "", + MONGOURL: os.Getenv("MONGOURL"), } - return config }