dndmusicbot/discord.go

57 lines
1.2 KiB
Go

package main
import (
"context"
discordspeaker "dndmusicbot/speaker"
"log"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/state"
"github.com/diamondburned/arikawa/v3/voice"
"github.com/diamondburned/arikawa/v3/voice/voicegateway"
)
var dgvc context.CancelFunc
func init() {
log.Println("discord.go loading..")
token := config.GetString("discord.token")
channel := config.GetString("discord.channel")
ctx, cancel := context.WithCancel(context.Background())
dgvc = cancel
s := state.New("Bot " + token)
// This is required for bots.
voice.AddIntents(s)
if err := s.Open(ctx); err != nil {
log.Fatalln("failed to open gateway:", err)
}
v, err := voice.NewSession(s)
if err != nil {
log.Fatalln("failed to create voice session:", err)
}
chsf, err := discord.ParseSnowflake(channel)
if err != nil {
log.Fatalln("failed to create snowflake:", err)
}
if err := v.JoinChannelAndSpeak(ctx, discord.ChannelID(chsf), false, true); err != nil {
log.Fatalln("failed to join voice channel:", err)
}
v.Speaking(ctx, voicegateway.NotSpeaking)
app.discord = s
app.voice = v
discordspeaker.Init(v, config.GetInt("discord.bitrate"))
log.Println("discord.go done.")
}