https://github.com/devoxin/sharplink
Lavalink wrapper for Discord.Net written in C#
https://github.com/devoxin/sharplink
Last synced: 12 months ago
JSON representation
Lavalink wrapper for Discord.Net written in C#
- Host: GitHub
- URL: https://github.com/devoxin/sharplink
- Owner: devoxin
- License: mit
- Created: 2018-05-04T21:49:31.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2021-04-18T11:38:34.000Z (about 5 years ago)
- Last Synced: 2024-06-21T21:07:29.286Z (almost 2 years ago)
- Language: C#
- Size: 64.5 KB
- Stars: 27
- Watchers: 5
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SharpLink
A [Lavalink](https://github.com/freyacodes/Lavalink) wrapper for Discord.Net!
[](https://www.nuget.org/packages/SharpLink/)
## `Getting Started`
- Follow [these instructions](https://github.com/freyacodes/Lavalink#server-configuration) to setup Lavalink.
- Once Lavalink is up and running follow the code example below to setup Sharplink.
```cs
DiscordSocketClient client = new DiscordSocketClient();
LavalinkManager lavalinkManager = new LavalinkManager(client, new LavalinkManagerConfig
{
RESTHost = "localhost",
RESTPort = 2333,
WebSocketHost = "localhost",
WebSocketPort = 2333,
Authorization = "YOUR_SECRET_AUTHORIZATION_KEY",
TotalShards = 1
});
```
*Notes:*
> You don't have to pass a `LavalinkManagerConfig` since Sharplink uses the default config.
> As of Lavalink v3.1, in `LavalinkManagerConfig` your hosts, `RESTHost` and `WebSocketHost`, should be identical, and `RESTPort` should be identical to `WebSocketPort`.
> Set `TotalShards` to the total amount of shards your bot uses. If you don't understand what `TotalShards` is you are probably not sharding your bot and should set this value to `1`.
> Use only a single instance of `LavaLinkManager`. If possible add `LavalinkManager` to your DI (Dependency Injection).
Once a LavalinkManager is set up it will need to be started. It is recommended you put this in the ready event.
```csharp
client.Ready += async () =>
{
await lavalinkManager.StartAsync();
}
```
From there you can connect to audio channels, play music, and do whatever else you wish to do. Here is an example to connect and play music on a voice channel.
```cs
// Get LavalinkPlayer for our Guild and if it's null then join a voice channel.
LavalinkPlayer player = lavalinkManager.GetPlayer(GUILD_ID) ?? await lavalinkManager.JoinAsync(VOICE_CHANNEL);
// Now that we have a player we can go ahead and grab a track and play it
LoadTracksResponse response = await lavalinkManager.GetTracksAsync("IDENTIFIER");
// Gets the first track from the response
LavalinkTrack track = response.Tracks.First();
await player.PlayAsync(track);
// For legacy usage with GetTrackAsync() on pre beta-0005 (This is deprecated, please upgrade)
// The below note about search results applies to this method also
LavalinkTrack track = await lavalinkManager.GetTrackAsync("IDENTIFIER");
await player.PlayAsync(track);
```
*Notes:* To get a track from **Youtube** use `GetTracksAsync($"ytsearch:Query")`. To get a track from **SoundCloud** use `GetTracksAsync($"scsearch:Query")`.