- Notifications
You must be signed in to change notification settings - Fork3
disgoorg/disgolink
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
DisGoLink is aLavalink Client written inGolang which supports the latest Lavalink 4.0.0+ release and the new plugin system.
While DisGoLink can be used with anyDiscord LibraryDisGo is the best fit for it as usage with other Libraries can be a bit annoying due to differentSnowflake implementations.
This Library uses theDisgo Snowflake package like DisGo
go get github.com/disgoorg/disgolink/v3
First create a new lavalink instance. You can do this either with
import ("github.com/disgoorg/snowflake/v2""github.com/disgoorg/disgolink/v3/disgolink")varuserID=snowflake.ID(1234567890)lavalinkClient:=disgolink.New(userID)
You also need to forward theVOICE_STATE_UPDATE
andVOICE_SERVER_UPDATE
events to DisGoLink.Just register an event listener for those events with your library and calllavalinkClient.OnVoiceStateUpdate
(make sure to only forward your bots voice update event!) andlavalinkClient.OnVoiceServerUpdate
For DisGo this would look like this
client,err:=disgo.New(Token,bot.WithEventListenerFunc(b.onVoiceStateUpdate),bot.WithEventListenerFunc(b.onVoiceServerUpdate),)funconVoiceStateUpdate(event*events.GuildVoiceStateUpdate) {// filter all non bot voice state updates outifevent.VoiceState.UserID!=client.ApplicationID() {return }lavalinkClient.OnVoiceStateUpdate(context.TODO(),event.VoiceState.GuildID,event.VoiceState.ChannelID,event.VoiceState.SessionID)}funconVoiceServerUpdate(event*events.VoiceServerUpdate) {lavalinkClient.OnVoiceServerUpdate(context.TODO(),event.GuildID,event.Token,*event.Endpoint)}
Then you add your lavalink nodes. This directly connects to the nodes and is a blocking call
node,err:=lavalinkClient.AddNode(context.TODO(), lavalink.NodeConfig{Name:"test",// a unique node nameAddress:"localhost:2333",Password:"youshallnotpass",Secure:false,// ws or wssSessionID:"",// only needed if you want to resume a previous lavalink session})
after this you can play songs from lavalinks supported sources.
To play a track you first need to resolve the song. For this you need to call the Lavalink restloadtracks
endpoint which returns a result with various track instances. Those tracks can then be played.
query:="ytsearch:Rick Astley - Never Gonna Give You Up"vartoPlay*lavalink.TracklavalinkClient.BestNode().LoadTracksHandler(context.TODO(),query,disgolink.NewResultHandler(func(track lavalink.Track) {// Loaded a single tracktoPlay=&track},func(playlist lavalink.Playlist) {// Loaded a playlist},func(tracks []lavalink.Track) {// Loaded a search result},func() {// nothing matching the query found},func(errerror) {// something went wrong while loading the track},))
To play a track we first need to connect to the voice channel.Connecting to a voice channel differs with every lib but here are some quick usages with some
// DisGoerr:=client.UpdateVoiceState(context.TODO(),guildID,channelID,false,false)// DiscordGoerr:=session.ChannelVoiceJoinManual(guildID,channelID,false,false)
after this you can get/create your player and play the track
player:=lavalinkClient.Player("guild_id")// This will either return an existing or new player// toPlay is from result handler in the example aboveerr:=player.Update(context.TODO(),lavalink.WithTrack(*toPlay))
now audio should start playing
You can listen for following lavalink events
PlayerUpdateMessage
Emitted every x seconds (default 5) with the current player statePlayerPause
Emitted when the player is pausedPlayerResume
Emitted when the player is resumedTrackStart
Emitted when a track starts playingTrackEnd
Emitted when a track endsTrackException
Emitted when a track throws an exceptionTrackStuck
Emitted when a track gets stuckWebsocketClosed
Emitted when the voice gateway connection to lavalink is closed
for this add and event listener for each event to yourClient
instance when you create it or withClient.AddEventListener
lavalinkClient:=disgolink.New(userID,disgolink.WithListenerFunc(onPlayerUpdate),disgolink.WithListenerFunc(onPlayerPause),disgolink.WithListenerFunc(onPlayerResume),disgolink.WithListenerFunc(onTrackStart),disgolink.WithListenerFunc(onTrackEnd),disgolink.WithListenerFunc(onTrackException),disgolink.WithListenerFunc(onTrackStuck),disgolink.WithListenerFunc(onWebSocketClosed),)funconPlayerUpdate(player disgolink.Player,event lavalink.PlayerUpdateMessage) {// do something with the event}funconPlayerPause(player disgolink.Player,event lavalink.PlayerPauseEvent) {// do something with the event}funconPlayerResume(player disgolink.Player,event lavalink.PlayerResumeEvent) {// do something with the event}funconTrackStart(player disgolink.Player,event lavalink.TrackStartEvent) {// do something with the event}funconTrackEnd(player disgolink.Player,event lavalink.TrackEndEvent) {// do something with the event}funconTrackException(player disgolink.Player,event lavalink.TrackExceptionEvent) {// do something with the event}funconTrackStuck(player disgolink.Player,event lavalink.TrackStuckEvent) {// do something with the event}funconWebSocketClosed(player disgolink.Player,event lavalink.WebSocketClosedEvent) {// do something with the event}
Lavalink addedplugins inv3.5
. DisGoLink exposes a similar API for you to use. With that you can create plugins which require server & client work.To see what you can do with plugins seehere
You register plugins when creating the client instance like this
lavalinkClient:=disgolink.New(userID,disgolink.WithPlugins(yourPlugin))
Here is a list of plugins(you can pr your own to here):
- SponsorBlock support forLavalink Sponsorblock-Plugin
- LavaQueue support forLavalink LavaQueue-Plugin
- LavaSrc support forLavalink LavaSrc-Plugin
- LavaLyrics support forLavalink LavaLyrics-Plugin
- LavaSearch support forLavalink LavaSearch-Plugin
You can find examples under
For help feel free to open an issue or reach out onDiscord
Contributions are welcomed but for bigger changes please first reach out viaDiscord or create an issue to discuss your intentions and ideas.
About
A Golang Lavalink Client