-
-
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.
feat: Implement commands in DSharpPlus sample
- Loading branch information
1 parent
175414c
commit f03a45b
Showing
3 changed files
with
144 additions
and
30 deletions.
There are no files selected for viewing
30 changes: 16 additions & 14 deletions
30
samples/Lavalink4NET.DSharpPlus.ExampleBot/Lavalink4NET.DSharpPlus.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 |
---|---|---|
@@ -1,19 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<RootNamespace>ExampleBot</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" /> | ||
<PackageReference Include="Emzi0767.Common" Version="2.6.2"/> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.4.6" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" /> | ||
<PackageReference Include="Emzi0767.Common" Version="2.6.2" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Lavalink4NET.DSharpPlus\Lavalink4NET.DSharpPlus.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Lavalink4NET.DSharpPlus\Lavalink4NET.DSharpPlus.csproj" /> | ||
</ItemGroup> | ||
</Project> |
106 changes: 106 additions & 0 deletions
106
samples/Lavalink4NET.DSharpPlus.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,106 @@ | ||
namespace ExampleBot; | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using DSharpPlus.Entities; | ||
using DSharpPlus.SlashCommands; | ||
using Lavalink4NET; | ||
using Lavalink4NET.Players; | ||
using Lavalink4NET.Players.Queued; | ||
using Lavalink4NET.Rest.Entities.Tracks; | ||
using Microsoft.Extensions.Options; | ||
|
||
public class MusicCommands : ApplicationCommandModule | ||
{ | ||
private readonly IAudioService _audioService; | ||
|
||
public MusicCommands(IAudioService audioService) | ||
{ | ||
ArgumentNullException.ThrowIfNull(audioService); | ||
|
||
_audioService = audioService; | ||
} | ||
|
||
[SlashCommand("play", description: "Plays music")] | ||
public async Task Play(InteractionContext interactionContext, [Option("query", "Track to play")] string query) | ||
{ | ||
await interactionContext.DeferAsync().ConfigureAwait(false); | ||
|
||
var player = await GetPlayerAsync(interactionContext, connectToVoiceChannel: true).ConfigureAwait(false); | ||
|
||
if (player is null) | ||
{ | ||
return; | ||
} | ||
|
||
var track = await _audioService.Tracks | ||
.LoadTrackAsync(query, TrackSearchMode.YouTube) | ||
.ConfigureAwait(false); | ||
|
||
if (track is null) | ||
{ | ||
var errorResponse = new DiscordFollowupMessageBuilder() | ||
.WithContent("😖 No results.") | ||
.AsEphemeral(); | ||
|
||
await interactionContext | ||
.FollowUpAsync(errorResponse) | ||
.ConfigureAwait(false); | ||
|
||
return; | ||
} | ||
|
||
var position = await player | ||
.PlayAsync(track) | ||
.ConfigureAwait(false); | ||
|
||
if (position is 0) | ||
{ | ||
await interactionContext | ||
.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"🔈 Playing: {track.Uri}")) | ||
.ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
await interactionContext | ||
.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"🔈 Added to queue: {track.Uri}")) | ||
.ConfigureAwait(false); | ||
} | ||
} | ||
|
||
private async ValueTask<QueuedLavalinkPlayer?> GetPlayerAsync(InteractionContext interactionContext, bool connectToVoiceChannel = true) | ||
{ | ||
ArgumentNullException.ThrowIfNull(interactionContext); | ||
|
||
var retrieveOptions = new PlayerRetrieveOptions( | ||
ChannelBehavior: connectToVoiceChannel ? PlayerChannelBehavior.Join : PlayerChannelBehavior.None); | ||
|
||
var playerOptions = new QueuedLavalinkPlayerOptions { HistoryCapacity = 10000 }; | ||
|
||
var result = await _audioService.Players | ||
.RetrieveAsync(interactionContext.Guild.Id, interactionContext.Member?.VoiceState.Channel.Id, 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 interactionContext | ||
.FollowUpAsync(errorResponse) | ||
.ConfigureAwait(false); | ||
|
||
return null; | ||
} | ||
|
||
return result.Player; | ||
} | ||
} |
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