diff --git a/main.go b/main.go index 8f00f1c..f5f43f9 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "github.com/gin-gonic/gin" "github.com/mgerb/go-discord-bot/server/bot" - "github.com/mgerb/go-discord-bot/server/bothandlers" "github.com/mgerb/go-discord-bot/server/config" "github.com/mgerb/go-discord-bot/server/webserver" log "github.com/sirupsen/logrus" @@ -34,14 +33,8 @@ func init() { func main() { - //connect bot to account with token - bot.Connect(config.Config.Token) - - //add handlers - bot.AddHandler(bothandlers.SoundsHandler) - // start the bot - bot.Start() + bot.Start(config.Config.Token) // start the web server webserver.Start() diff --git a/server/bot/bot.go b/server/bot/bot.go index 8dee48d..1e45f9c 100644 --- a/server/bot/bot.go +++ b/server/bot/bot.go @@ -2,19 +2,28 @@ package bot import ( "github.com/bwmarrin/discordgo" + "github.com/mgerb/go-discord-bot/server/bothandlers" log "github.com/sirupsen/logrus" ) -// Variables used for command line parameters -var ( - BotID string - Session *discordgo.Session -) +func Start(token string) { + // initialize connection + session := connect(token) -func Connect(token string) { + // add bot handlers + addHandler(session, bothandlers.SoundsHandler) + + // start listening for commands + startListener(session) +} + +func addHandler(session *discordgo.Session, handler interface{}) { + session.AddHandler(handler) +} + +func connect(token string) *discordgo.Session { // Create a new Discord session using the provided bot token. - var err error - Session, err = discordgo.New("Bot " + token) + session, err := discordgo.New("Bot " + token) if err != nil { log.Error(err) @@ -22,26 +31,23 @@ func Connect(token string) { } // Get the account information. - u, err := Session.User("@me") + _, err = session.User("@me") if err != nil { log.Error("Error obtaining account details. Make sure you have the correct bot token.") log.Fatal(err) } - // Store the account ID for later use. - BotID = u.ID - log.Debug("Bot connected") + + return session } -// Start - blocking function that starts a websocket listenting for discord callbacks -func Start() { - +func startListener(session *discordgo.Session) { // start new non blocking go routine go func() { // Open the websocket and begin listening. - err := Session.Open() + err := session.Open() if err != nil { log.Error("error opening connection,", err) return @@ -54,7 +60,3 @@ func Start() { return }() } - -func AddHandler(handler interface{}) { - Session.AddHandler(handler) -}