-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...alink4NET.DSharpPlus.Nightly.ExampleBot/Lavalink4NET.DSharpPlus.Nightly.ExampleBot.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="DSharpPlus.Commands" Version="5.0.0-nightly-02355" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Lavalink4NET.DSharpPlus.Nightly\Lavalink4NET.DSharpPlus.Nightly.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
121 changes: 121 additions & 0 deletions
121
samples/Lavalink4NET.DSharpPlus.Nightly.ExampleBot/MusicCommands.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
namespace ExampleBot; | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using DSharpPlus.Entities; | ||
using DSharpPlus.Commands; | ||
using Lavalink4NET; | ||
using Lavalink4NET.Players; | ||
using Lavalink4NET.Players.Queued; | ||
using Lavalink4NET.Rest.Entities.Tracks; | ||
using Microsoft.Extensions.Options; | ||
using System.ComponentModel; | ||
using DSharpPlus.Commands.ContextChecks; | ||
|
||
public class MusicCommands | ||
{ | ||
private readonly IAudioService _audioService; | ||
|
||
public MusicCommands(IAudioService audioService) | ||
{ | ||
ArgumentNullException.ThrowIfNull(audioService); | ||
_audioService = audioService; | ||
} | ||
|
||
[Command("play")] | ||
[Description("Plays music")] | ||
[DirectMessageUsage(DirectMessageUsage.DenyDMs)] | ||
public async Task Play(CommandContext context, | ||
|
||
[Parameter("query")] | ||
[Description("Track to play")] | ||
string query) | ||
{ | ||
// This operation could take a while - deferring the interaction lets Discord know we've | ||
// received it and lets us update it later. Users see a "thinking..." state. | ||
await context.DeferResponseAsync().ConfigureAwait(false); | ||
|
||
// Attempt to get the player | ||
var player = await GetPlayerAsync(context, connectToVoiceChannel: true).ConfigureAwait(false); | ||
|
||
// If something went wrong getting the player, don't attempt to play any tracks | ||
if (player is null) | ||
return; | ||
|
||
// Fetch the tracks | ||
var track = await _audioService.Tracks | ||
.LoadTrackAsync(query, TrackSearchMode.YouTube) | ||
.ConfigureAwait(false); | ||
|
||
// If no results were found | ||
if (track is null) | ||
{ | ||
var errorResponse = new DiscordFollowupMessageBuilder() | ||
.WithContent("😖 No results.") | ||
.AsEphemeral(); | ||
|
||
await context | ||
.EditResponseAsync(errorResponse) | ||
.ConfigureAwait(false); | ||
|
||
return; | ||
} | ||
|
||
// Play the track | ||
var position = await player | ||
.PlayAsync(track) | ||
.ConfigureAwait(false); | ||
|
||
// If it was added to the queue | ||
if (position is 0) | ||
{ | ||
await context | ||
.FollowupAsync(new DiscordFollowupMessageBuilder().WithContent($"🔈 Playing: {track.Uri}")) | ||
.ConfigureAwait(false); | ||
} | ||
|
||
// If it was played directly | ||
else | ||
{ | ||
await context | ||
.FollowupAsync(new DiscordFollowupMessageBuilder().WithContent($"🔈 Added to queue: {track.Uri}")) | ||
.ConfigureAwait(false); | ||
} | ||
} | ||
|
||
private async ValueTask<QueuedLavalinkPlayer?> GetPlayerAsync(CommandContext commandContext, bool connectToVoiceChannel = true) | ||
{ | ||
ArgumentNullException.ThrowIfNull(commandContext); | ||
|
||
var retrieveOptions = new PlayerRetrieveOptions( | ||
ChannelBehavior: connectToVoiceChannel ? PlayerChannelBehavior.Join : PlayerChannelBehavior.None); | ||
|
||
var playerOptions = new QueuedLavalinkPlayerOptions { HistoryCapacity = 10000 }; | ||
|
||
var result = await _audioService.Players | ||
.RetrieveAsync(commandContext.Guild!.Id, commandContext.Member?.VoiceState?.Channel?.Id ?? null, playerFactory: PlayerFactory.Queued, Options.Create(playerOptions), retrieveOptions) | ||
.ConfigureAwait(false); | ||
|
||
if (!result.IsSuccess) | ||
{ | ||
var errorMessage = result.Status switch | ||
{ | ||
PlayerRetrieveStatus.UserNotInVoiceChannel => "You are not connected to a voice channel.", | ||
PlayerRetrieveStatus.BotNotConnected => "The bot is currently not connected.", | ||
_ => "Unknown error.", | ||
}; | ||
|
||
var errorResponse = new DiscordFollowupMessageBuilder() | ||
.WithContent(errorMessage) | ||
.AsEphemeral(); | ||
|
||
await commandContext | ||
.FollowupAsync(errorResponse) | ||
.ConfigureAwait(false); | ||
|
||
return null; | ||
} | ||
|
||
return result.Player; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
samples/Lavalink4NET.DSharpPlus.Nightly.ExampleBot/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using DSharpPlus; | ||
using DSharpPlus.Commands; | ||
using DSharpPlus.Extensions; | ||
using ExampleBot; | ||
using Lavalink4NET.Extensions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
var builder = new HostApplicationBuilder(args); | ||
|
||
// Your host service | ||
builder.Services.AddHostedService<ApplicationHost>(); | ||
|
||
|
||
// DSharpPlus | ||
builder.Services.AddDiscordClient("Your token here", DiscordIntents.AllUnprivileged); | ||
builder.Services.AddCommandsExtension(extension => extension.AddCommands(typeof(MusicCommands).Assembly)); | ||
|
||
|
||
// Lavalink4NET | ||
builder.Services.AddLavalink(); | ||
|
||
|
||
// Logging | ||
builder.Services.AddLogging(s => s.AddConsole().SetMinimumLevel(LogLevel.Debug)); | ||
|
||
|
||
// Start the host | ||
builder.Build().Run(); | ||
|
||
|
||
file sealed class ApplicationHost : BackgroundService | ||
{ | ||
private readonly DiscordClient _discordClient; | ||
|
||
public ApplicationHost(DiscordClient discordClient) | ||
{ | ||
ArgumentNullException.ThrowIfNull(discordClient); | ||
_discordClient = discordClient; | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
// Connect to discord gateway and initialize node connection | ||
await _discordClient | ||
.ConnectAsync() | ||
.ConfigureAwait(false); | ||
|
||
await Task | ||
.Delay(-1, stoppingToken) | ||
.ConfigureAwait(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters