1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-11 09:32:50 +00:00

$minor refactoring

This commit is contained in:
2018-02-07 18:27:49 +00:00
parent 5b6d74149d
commit 1fdeb3783a
2 changed files with 23 additions and 28 deletions

View File

@@ -3,7 +3,6 @@ package main
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/mgerb/go-discord-bot/server/bot" "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/config"
"github.com/mgerb/go-discord-bot/server/webserver" "github.com/mgerb/go-discord-bot/server/webserver"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@@ -34,14 +33,8 @@ func init() {
func main() { func main() {
//connect bot to account with token
bot.Connect(config.Config.Token)
//add handlers
bot.AddHandler(bothandlers.SoundsHandler)
// start the bot // start the bot
bot.Start() bot.Start(config.Config.Token)
// start the web server // start the web server
webserver.Start() webserver.Start()

View File

@@ -2,19 +2,28 @@ package bot
import ( import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/mgerb/go-discord-bot/server/bothandlers"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// Variables used for command line parameters func Start(token string) {
var ( // initialize connection
BotID string session := connect(token)
Session *discordgo.Session
)
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. // 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 { if err != nil {
log.Error(err) log.Error(err)
@@ -22,26 +31,23 @@ func Connect(token string) {
} }
// Get the account information. // Get the account information.
u, err := Session.User("@me") _, err = session.User("@me")
if err != nil { if err != nil {
log.Error("Error obtaining account details. Make sure you have the correct bot token.") log.Error("Error obtaining account details. Make sure you have the correct bot token.")
log.Fatal(err) log.Fatal(err)
} }
// Store the account ID for later use.
BotID = u.ID
log.Debug("Bot connected") log.Debug("Bot connected")
return session
} }
// Start - blocking function that starts a websocket listenting for discord callbacks func startListener(session *discordgo.Session) {
func Start() {
// start new non blocking go routine // start new non blocking go routine
go func() { go func() {
// Open the websocket and begin listening. // Open the websocket and begin listening.
err := Session.Open() err := session.Open()
if err != nil { if err != nil {
log.Error("error opening connection,", err) log.Error("error opening connection,", err)
return return
@@ -54,7 +60,3 @@ func Start() {
return return
}() }()
} }
func AddHandler(handler interface{}) {
Session.AddHandler(handler)
}