fix: minor fixes to make lavalink work
This commit is contained in:
parent
28ddb171ff
commit
3a9f33fc07
|
@ -0,0 +1,142 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"strconv"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/foxbot/gavalink"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var token string
|
||||||
|
var lavalink *gavalink.Lavalink
|
||||||
|
var player *gavalink.Player
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.StringVar(&token, "token", "", "token=unprefixed token")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if token == "" {
|
||||||
|
panic("no token specified!")
|
||||||
|
}
|
||||||
|
token = "Bot " + token
|
||||||
|
|
||||||
|
dg, err := discordgo.New(token)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dg.AddHandler(ready)
|
||||||
|
dg.AddHandler(messageCreate)
|
||||||
|
dg.AddHandler(voiceServerUpdate)
|
||||||
|
|
||||||
|
err = dg.Open()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sc := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
||||||
|
<-sc
|
||||||
|
|
||||||
|
dg.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func ready(s *discordgo.Session, event *discordgo.Ready) {
|
||||||
|
log.Println("discordgo ready!")
|
||||||
|
s.UpdateStatus(0, "gavalink")
|
||||||
|
|
||||||
|
lavalink = gavalink.NewLavalink("1", event.User.ID)
|
||||||
|
|
||||||
|
err := lavalink.AddNodes(gavalink.NodeConfig{
|
||||||
|
REST: "http://localhost:2333",
|
||||||
|
WebSocket: "ws://localhost:2334",
|
||||||
|
Password: "youshallnotpass",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
if m.Author.ID == s.State.User.ID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.Content == "~>>join" {
|
||||||
|
c, err := s.State.Channel(m.ChannelID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("fail find channel")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
g, err := s.State.Guild(c.GuildID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("fail find guild")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, vs := range g.VoiceStates {
|
||||||
|
if vs.UserID == m.Author.ID {
|
||||||
|
log.Println("trying to connect to channel")
|
||||||
|
_, err := s.ChannelVoiceJoin(c.GuildID, vs.ChannelID, false, false)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
} else {
|
||||||
|
log.Println("channel voice join succeeded")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if m.Content == "~>>playtest" {
|
||||||
|
node, err := lavalink.BestNode()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tracks, err := node.LoadTracks("https://www.youtube.com/watch?v=Wl2Q_MceIUc")
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if tracks.Type != gavalink.TrackLoaded {
|
||||||
|
log.Println("weird tracks type", tracks.Type)
|
||||||
|
}
|
||||||
|
track := tracks.Tracks[0].Data
|
||||||
|
err = player.Play(track)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func voiceServerUpdate(s *discordgo.Session, event *discordgo.VoiceServerUpdate) {
|
||||||
|
log.Println("received VSU")
|
||||||
|
node, err := lavalink.BestNode()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
gid, _ := strconv.Atoi(event.GuildID)
|
||||||
|
vsu := gavalink.VoiceServerUpdate{
|
||||||
|
Endpoint: event.Endpoint,
|
||||||
|
GuildID: gid,
|
||||||
|
Token: event.Token,
|
||||||
|
}
|
||||||
|
handler := new(gavalink.DummyEventHandler)
|
||||||
|
player, err = node.CreatePlayer(event.GuildID, s.State.SessionID, vsu, handler)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
panic("discordgo: cancel event")
|
||||||
|
}
|
24
lavalink.go
24
lavalink.go
|
@ -6,10 +6,8 @@ import (
|
||||||
|
|
||||||
// Lavalink manages a connection to Lavalink Nodes
|
// Lavalink manages a connection to Lavalink Nodes
|
||||||
type Lavalink struct {
|
type Lavalink struct {
|
||||||
// Shards is the total number of shards the bot is running
|
shards string
|
||||||
Shards int
|
userID string
|
||||||
// UserID is the Discord User ID of the bot
|
|
||||||
UserID int
|
|
||||||
|
|
||||||
nodes []Node
|
nodes []Node
|
||||||
players map[string]*Player
|
players map[string]*Player
|
||||||
|
@ -26,27 +24,31 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewLavalink creates a new Lavalink manager
|
// NewLavalink creates a new Lavalink manager
|
||||||
func NewLavalink(shards int, userID int) *Lavalink {
|
func NewLavalink(shards string, userID string) *Lavalink {
|
||||||
return &Lavalink{
|
return &Lavalink{
|
||||||
Shards: shards,
|
shards: shards,
|
||||||
UserID: userID,
|
userID: userID,
|
||||||
nodes: make([]Node, 1),
|
/* nodes: make([]Node, 1),*/
|
||||||
players: make(map[string]*Player),
|
players: make(map[string]*Player),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddNodes adds a node to the Lavalink manager
|
// AddNodes adds a node to the Lavalink manager
|
||||||
func (lavalink *Lavalink) AddNodes(nodeConfigs ...NodeConfig) {
|
func (lavalink *Lavalink) AddNodes(nodeConfigs ...NodeConfig) error {
|
||||||
nodes := make([]Node, len(nodeConfigs))
|
nodes := make([]Node, len(nodeConfigs))
|
||||||
for i, c := range nodeConfigs {
|
for i, c := range nodeConfigs {
|
||||||
n := Node{
|
n := Node{
|
||||||
config: c,
|
config: c,
|
||||||
shards: lavalink.Shards,
|
manager: lavalink,
|
||||||
userID: lavalink.UserID,
|
}
|
||||||
|
err := n.open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
nodes[i] = n
|
nodes[i] = n
|
||||||
}
|
}
|
||||||
lavalink.nodes = append(lavalink.nodes, nodes...)
|
lavalink.nodes = append(lavalink.nodes, nodes...)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveNode removes a node from the manager
|
// RemoveNode removes a node from the manager
|
||||||
|
|
5
node.go
5
node.go
|
@ -30,8 +30,6 @@ type NodeConfig struct {
|
||||||
// Node wraps a Lavalink Node
|
// Node wraps a Lavalink Node
|
||||||
type Node struct {
|
type Node struct {
|
||||||
config NodeConfig
|
config NodeConfig
|
||||||
shards int
|
|
||||||
userID int
|
|
||||||
manager *Lavalink
|
manager *Lavalink
|
||||||
wsConn *websocket.Conn
|
wsConn *websocket.Conn
|
||||||
}
|
}
|
||||||
|
@ -39,6 +37,8 @@ type Node struct {
|
||||||
func (node *Node) open() error {
|
func (node *Node) open() error {
|
||||||
header := http.Header{}
|
header := http.Header{}
|
||||||
header.Set("Authorization", node.config.Password)
|
header.Set("Authorization", node.config.Password)
|
||||||
|
header.Set("Num-Shards", node.manager.shards)
|
||||||
|
header.Set("User-Id", node.manager.userID)
|
||||||
|
|
||||||
ws, resp, err := websocket.DefaultDialer.Dial(node.config.WebSocket, header)
|
ws, resp, err := websocket.DefaultDialer.Dial(node.config.WebSocket, header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -73,6 +73,7 @@ func (node *Node) listen() {
|
||||||
for {
|
for {
|
||||||
msgType, msg, err := node.wsConn.ReadMessage()
|
msgType, msg, err := node.wsConn.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
// try to reconnect
|
// try to reconnect
|
||||||
oerr := node.open()
|
oerr := node.open()
|
||||||
if oerr != nil {
|
if oerr != nil {
|
||||||
|
|
Loading…
Reference in New Issue