mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-10 17:12:48 +00:00
work in progress - about to refactor how sounds are played
This commit is contained in:
54
server/bot/bot.go
Normal file
54
server/bot/bot.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
// Variables used for command line parameters
|
||||
var (
|
||||
BotID string
|
||||
Session *discordgo.Session
|
||||
)
|
||||
|
||||
func Connect(token string) {
|
||||
// Create a new Discord session using the provided bot token.
|
||||
var err error
|
||||
Session, err = discordgo.New("Bot " + token)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("error creating Discord session,", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Get the account information.
|
||||
u, err := Session.User("@me")
|
||||
if err != nil {
|
||||
fmt.Println("error obtaining account details,", err)
|
||||
}
|
||||
|
||||
// Store the account ID for later use.
|
||||
BotID = u.ID
|
||||
|
||||
fmt.Println("Bot connected")
|
||||
}
|
||||
|
||||
// Start - blocking function that starts a websocket listenting for discord callbacks
|
||||
func Start() {
|
||||
// Open the websocket and begin listening.
|
||||
err := Session.Open()
|
||||
if err != nil {
|
||||
fmt.Println("error opening connection,", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Bot is now running...")
|
||||
|
||||
// Simple way to keep program running until CTRL-C is pressed.
|
||||
<-make(chan struct{})
|
||||
return
|
||||
}
|
||||
|
||||
func AddHandler(handler interface{}) {
|
||||
Session.AddHandler(handler)
|
||||
}
|
||||
Reference in New Issue
Block a user