dndmusicbot/discord.go

57 lines
1.2 KiB
Go
Raw Permalink Normal View History

2022-11-18 21:18:12 +00:00
package main
import (
2022-12-05 17:24:06 +00:00
"context"
2022-11-18 21:18:12 +00:00
discordspeaker "dndmusicbot/speaker"
"log"
2022-12-05 17:24:06 +00:00
"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"
2022-11-18 21:18:12 +00:00
)
2022-12-05 17:24:06 +00:00
var dgvc context.CancelFunc
2022-11-18 21:18:12 +00:00
func init() {
log.Println("discord.go loading..")
token := config.GetString("discord.token")
channel := config.GetString("discord.channel")
2022-12-05 17:24:06 +00:00
ctx, cancel := context.WithCancel(context.Background())
2022-11-18 21:18:12 +00:00
2022-12-05 17:24:06 +00:00
dgvc = cancel
s := state.New("Bot " + token)
2022-11-18 21:18:12 +00:00
2022-12-05 17:24:06 +00:00
// This is required for bots.
voice.AddIntents(s)
if err := s.Open(ctx); err != nil {
log.Fatalln("failed to open gateway:", err)
2022-11-18 21:18:12 +00:00
}
2022-12-05 17:24:06 +00:00
v, err := voice.NewSession(s)
2022-11-18 21:18:12 +00:00
if err != nil {
2022-12-05 17:24:06 +00:00
log.Fatalln("failed to create voice session:", err)
2022-11-18 21:18:12 +00:00
}
2022-12-05 17:24:06 +00:00
chsf, err := discord.ParseSnowflake(channel)
if err != nil {
2022-12-05 17:24:06 +00:00
log.Fatalln("failed to create snowflake:", err)
}
2022-12-05 17:24:06 +00:00
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)
2022-11-18 21:18:12 +00:00
log.Println("discord.go done.")
}