Skip to content

Commit

Permalink
fix: read env
Browse files Browse the repository at this point in the history
  • Loading branch information
Olaussen committed Jul 15, 2023
1 parent d4a777c commit 5f15352
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 16 deletions.
12 changes: 12 additions & 0 deletions db/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package db

import (
"fmt"
"juanitamusic/models"
)

func Init(config *models.Config) {
connectionString := config.MONGOURL

fmt.Println(connectionString)
}
14 changes: 14 additions & 0 deletions interactions/commands/play.go
Original file line number Diff line number Diff line change
@@ -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!",
},
})
}
20 changes: 17 additions & 3 deletions commands/commands.go → interactions/loader.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
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{
{
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{
Expand All @@ -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)
},
}
}
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"juanitamusic/commands"
"juanitamusic/interactions"
"juanitamusic/utils"
"log"
"os"
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
4 changes: 0 additions & 4 deletions music/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,18 @@ 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() {
v.audioMutex.Lock()
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
Expand Down
10 changes: 7 additions & 3 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 5f15352

Please sign in to comment.