Skip to content

Commit

Permalink
Don't use exceptions in voice commands
Browse files Browse the repository at this point in the history
  • Loading branch information
KubaZ2 committed Sep 13, 2024
1 parent 45c6537 commit d7ddef0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
18 changes: 12 additions & 6 deletions Documentation/guides/advanced/Voice/VoiceModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ public async Task PlayAsync(string track)
{
// Check if the specified track is a well formed uri
if (!Uri.IsWellFormedUriString(track, UriKind.Absolute))
throw new("Invalid track!");
{
await RespondAsync(InteractionCallback.Message("Invalid track!"));
return;
}

var guild = Context.Guild!;

// Get the user voice state
if (!guild.VoiceStates.TryGetValue(Context.User.Id, out var voiceState))
throw new("You are not connected to any voice channel!");
{
await RespondAsync(InteractionCallback.Message("You are not connected to any voice channel!"));
return;
}

var client = Context.Client;

Expand Down Expand Up @@ -99,14 +105,14 @@ public async Task PlayAsync(string track)
}

[SlashCommand("echo", "Creates echo", Contexts = [InteractionContextType.Guild])]
public async Task EchoAsync()
public async Task<string> EchoAsync()
{
var guild = Context.Guild!;
var userId = Context.User.Id;

// Get the user voice state
if (!guild.VoiceStates.TryGetValue(userId, out var voiceState))
throw new("You are not connected to any voice channel!");
return "You are not connected to any voice channel!";

var client = Context.Client;

Expand Down Expand Up @@ -135,7 +141,7 @@ public async Task EchoAsync()
return default;
};

// Respond to the interaction
await RespondAsync(InteractionCallback.Message("Echo!"));
// Return the response
return "Echo!";
}
}
4 changes: 2 additions & 2 deletions Documentation/guides/advanced/voice.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Follow the [installation guide](installing-native-dependencies.md) to install th
> In the following examples streams and @NetCord.Gateway.Voice.VoiceClient instances are not disposed because they should be stored somewhere and disposed later.
### Sending Voice
[!code-cs[VoiceModule.cs](Voice/VoiceModule.cs#L12-L99)]
[!code-cs[VoiceModule.cs](Voice/VoiceModule.cs#L12-L105)]

### Receiving Voice
[!code-cs[VoiceModule.cs](Voice/VoiceModule.cs#L101-L140)]
[!code-cs[VoiceModule.cs](Voice/VoiceModule.cs#L107-L146)]

0 comments on commit d7ddef0

Please sign in to comment.