Pass CancellationTokens where possible

cancellationtokens all 'round
This commit is contained in:
Macintxsh 2024-06-15 00:22:38 +05:00
parent 34c39e8491
commit ea313bab7b
Signed by: mctaylors
GPG key ID: 4EEF4F949A266EE2
6 changed files with 94 additions and 76 deletions

View file

@ -30,7 +30,7 @@ public sealed class BotCommandGroup(
[UsedImplicitly]
public async Task<Result> AboutCommandAsync()
{
var botResult = await userApi.GetCurrentUserAsync();
var botResult = await userApi.GetCurrentUserAsync(CancellationToken);
if (!botResult.IsDefined(out var bot))
{
return Result.FromError(botResult);
@ -58,6 +58,6 @@ public sealed class BotCommandGroup(
new FeedbackMessageOptions(MessageComponents: new[]
{
new ActionRowComponent(new[] { button })
}));
}), CancellationToken);
}
}

View file

@ -34,7 +34,8 @@ public sealed class ControlsCommandGroup(
public async Task<Result> PlayCommandAsync(
[Description("URL or YouTube query")] string query)
{
var player = await LavalinkPlayer.GetPlayerAsync(commandContext, audioService, feedbackService, true);
var player = await LavalinkPlayer.GetPlayerAsync(
commandContext, audioService, feedbackService, true, CancellationToken);
if (player is null)
{
return Result.FromSuccess();
@ -42,52 +43,57 @@ public sealed class ControlsCommandGroup(
if (File.Exists(query))
{
var track = await audioService.Tracks.LoadTrackAsync(Path.GetFullPath(query), TrackSearchMode.None);
return await PlayTrackResultAsync(player, track);
var track = await audioService.Tracks.LoadTrackAsync(
Path.GetFullPath(query), TrackSearchMode.None, cancellationToken: CancellationToken);
return await PlayTrackResultAsync(player, track, CancellationToken);
}
var loadResult = await audioService.Tracks.LoadTracksAsync(query, TrackSearchMode.YouTube);
var loadResult = await audioService.Tracks.LoadTracksAsync(
query, TrackSearchMode.YouTube, cancellationToken: CancellationToken);
if (!loadResult.IsSuccess)
{
return await feedbackService.SendContextualMessageResult(
"Track loading error, try again later.", feedbackService.Theme.FaultOrDanger);
"Track loading error, try again later.",
feedbackService.Theme.FaultOrDanger, CancellationToken);
}
if (loadResult.IsPlaylist)
{
return await PlayListResultAsync(player, loadResult);
return await PlayListResultAsync(player, loadResult, CancellationToken);
}
return await PlayTrackResultAsync(player, loadResult.Track);
return await PlayTrackResultAsync(player, loadResult.Track, CancellationToken);
}
private async Task<Result> PlayTrackResultAsync(IQueuedLavalinkPlayer player, LavalinkTrack? track)
private async Task<Result> PlayTrackResultAsync(IQueuedLavalinkPlayer player,
LavalinkTrack? track, CancellationToken ct = default)
{
if (track is null)
{
return await feedbackService.SendContextualMessageResult(
"Not found.", feedbackService.Theme.FaultOrDanger);
"Not found.", feedbackService.Theme.FaultOrDanger, ct);
}
await player.PlayAsync(track);
await player.PlayAsync(track, cancellationToken: ct);
var message = new StringBuilder().Append($"Added {track.Display()}");
if (player.Queue.IsEmpty)
switch (player.Queue.IsEmpty)
{
message.Append(" to begin playing");
}
if (!player.Queue.IsEmpty)
{
message.Append(" to the queue at position ").Append(player.Queue.Count);
case true:
message.Append(" to begin playing");
break;
case false:
message.Append(" to the queue at position ").Append(player.Queue.Count);
break;
}
return await feedbackService.SendContextualMessageResult(
message.ToString(), feedbackService.Theme.Success);
message.ToString(), feedbackService.Theme.Success, ct);
}
private async Task<Result> PlayListResultAsync(IQueuedLavalinkPlayer player, TrackLoadResult loadResult)
private async Task<Result> PlayListResultAsync(IQueuedLavalinkPlayer player,
TrackLoadResult loadResult, CancellationToken ct = default)
{
var playlistInfo = loadResult.Playlist;
if (playlistInfo is null)
@ -97,11 +103,12 @@ public sealed class ControlsCommandGroup(
foreach (var track in loadResult.Tracks)
{
await player.PlayAsync(track);
await player.PlayAsync(track, cancellationToken: ct);
}
return await feedbackService.SendContextualMessageResult(
$"Added {Markdown.Bold(playlistInfo.Name)} playlist to the queue", feedbackService.Theme.Success);
$"Added {Markdown.Bold(playlistInfo.Name)} playlist to the queue",
feedbackService.Theme.Success, ct);
}
[Command("pause")]
@ -111,7 +118,7 @@ public sealed class ControlsCommandGroup(
public async Task<Result> PauseCommandAsync()
{
var player = await LavalinkPlayer.GetPlayerAsync(
commandContext, audioService, feedbackService);
commandContext, audioService, feedbackService, ct: CancellationToken);
if (player is null)
{
return Result.FromSuccess();
@ -121,20 +128,21 @@ public sealed class ControlsCommandGroup(
{
return await feedbackService.SendContextualMessageResult(
"There's nothing playing right now.",
feedbackService.Theme.FaultOrDanger);
feedbackService.Theme.FaultOrDanger, CancellationToken);
}
if (player.IsPaused)
{
return await feedbackService.SendContextualMessageResult(
"Player is currently paused",
feedbackService.Theme.FaultOrDanger);
feedbackService.Theme.FaultOrDanger, CancellationToken);
}
await player.PauseAsync();
await player.PauseAsync(CancellationToken);
return await feedbackService.SendContextualMessageResult(
$"Paused {player.CurrentTrack.Display()}", feedbackService.Theme.Success);
$"Paused {player.CurrentTrack.Display()}",
feedbackService.Theme.Success, CancellationToken);
}
[Command("resume")]
@ -144,7 +152,7 @@ public sealed class ControlsCommandGroup(
public async Task<Result> ResumeCommandAsync()
{
var player = await LavalinkPlayer.GetPlayerAsync(
commandContext, audioService, feedbackService);
commandContext, audioService, feedbackService, ct: CancellationToken);
if (player is null)
{
return Result.FromSuccess();
@ -154,20 +162,21 @@ public sealed class ControlsCommandGroup(
{
return await feedbackService.SendContextualMessageResult(
"There's nothing playing right now.",
feedbackService.Theme.FaultOrDanger);
feedbackService.Theme.FaultOrDanger, CancellationToken);
}
if (!player.IsPaused)
{
return await feedbackService.SendContextualMessageResult(
"Player is currently not paused.",
feedbackService.Theme.FaultOrDanger);
feedbackService.Theme.FaultOrDanger, CancellationToken);
}
await player.ResumeAsync();
await player.ResumeAsync(CancellationToken);
return await feedbackService.SendContextualMessageResult(
$"Resumed {player.CurrentTrack.Display()}", feedbackService.Theme.Success);
$"Resumed {player.CurrentTrack.Display()}",
feedbackService.Theme.Success, CancellationToken);
}
[Command("repeat")]
@ -178,7 +187,7 @@ public sealed class ControlsCommandGroup(
[Description("Track repeat mode")] TrackRepeatMode mode)
{
var player = await LavalinkPlayer.GetPlayerAsync(
commandContext, audioService, feedbackService);
commandContext, audioService, feedbackService, ct: CancellationToken);
if (player is null)
{
return Result.FromSuccess();
@ -188,21 +197,21 @@ public sealed class ControlsCommandGroup(
&& mode is not TrackRepeatMode.Queue)
{
var queue = player.Queue;
await queue.RemoveAsync(queue[^1]);
await queue.RemoveAsync(queue[^1], CancellationToken);
}
if (player.RepeatMode is not TrackRepeatMode.Queue
&& mode is TrackRepeatMode.Queue
&& player.CurrentTrack is not null)
{
await player.PlayAsync(player.CurrentTrack);
await player.PlayAsync(player.CurrentTrack, cancellationToken: CancellationToken);
}
player.RepeatMode = mode;
return await feedbackService.SendContextualMessageResult(
$"Repeat mode is now set to {Markdown.Bold(mode.ToString())}",
feedbackService.Theme.Success);
feedbackService.Theme.Success, CancellationToken);
}
[Command("shuffle")]
@ -213,7 +222,7 @@ public sealed class ControlsCommandGroup(
[Description("Enable shuffle mode")] bool enable)
{
var player = await LavalinkPlayer.GetPlayerAsync(
commandContext, audioService, feedbackService);
commandContext, audioService, feedbackService, ct: CancellationToken);
if (player is null)
{
return Result.FromSuccess();
@ -223,7 +232,7 @@ public sealed class ControlsCommandGroup(
return await feedbackService.SendContextualMessageResult(
$"Shuffle mode is now {Markdown.Bold(enable ? "enabled" : "disabled")}",
feedbackService.Theme.Success);
feedbackService.Theme.Success, CancellationToken);
}
[Command("seek")]
@ -234,7 +243,7 @@ public sealed class ControlsCommandGroup(
[Description("Position to rewind")] TimeSpan position)
{
var player = await LavalinkPlayer.GetPlayerAsync(
commandContext, audioService, feedbackService);
commandContext, audioService, feedbackService, ct: CancellationToken);
if (player is null)
{
return Result.FromSuccess();
@ -244,7 +253,7 @@ public sealed class ControlsCommandGroup(
{
return await feedbackService.SendContextualMessageResult(
"There's nothing playing right now.",
feedbackService.Theme.FaultOrDanger);
feedbackService.Theme.FaultOrDanger, CancellationToken);
}
var track = player.CurrentTrack;
@ -255,13 +264,14 @@ public sealed class ControlsCommandGroup(
return await feedbackService.SendContextualMessageResult(
$"Selected position ({position.ReadableDuration()}) is greater than " +
$"track's duration ({trackDuration.ReadableDuration()})",
feedbackService.Theme.FaultOrDanger);
feedbackService.Theme.FaultOrDanger, CancellationToken);
}
await player.SeekAsync(position);
await player.SeekAsync(position, CancellationToken);
return await feedbackService.SendContextualMessageResult(
$"{track.Display()} rewound to {position.ReadableDuration()}", feedbackService.Theme.Success);
$"{track.Display()} rewound to {position.ReadableDuration()}",
feedbackService.Theme.Success, CancellationToken);
}
[Command("skip")]
@ -273,7 +283,7 @@ public sealed class ControlsCommandGroup(
int? index = null)
{
var player = await LavalinkPlayer.GetPlayerAsync(
commandContext, audioService, feedbackService);
commandContext, audioService, feedbackService, ct: CancellationToken);
if (player is null)
{
return Result.FromSuccess();
@ -286,13 +296,13 @@ public sealed class ControlsCommandGroup(
{
return await feedbackService.SendContextualMessageResult(
"There's no track with that index.",
feedbackService.Theme.FaultOrDanger);
feedbackService.Theme.FaultOrDanger, CancellationToken);
}
var queueItem = queue[(int)index - 1];
var track = queueItem.Track;
await player.Queue.RemoveAsync(queueItem);
await player.Queue.RemoveAsync(queueItem, CancellationToken);
if (track is null)
{
@ -300,7 +310,8 @@ public sealed class ControlsCommandGroup(
}
return await feedbackService.SendContextualMessageResult(
$"Removed {track.Display()} from queue", feedbackService.Theme.Success);
$"Removed {track.Display()} from queue",
feedbackService.Theme.Success, CancellationToken);
}
var currentTrack = player.CurrentTrack;
@ -308,12 +319,13 @@ public sealed class ControlsCommandGroup(
{
return await feedbackService.SendContextualMessageResult(
"There's nothing playing right now.",
feedbackService.Theme.FaultOrDanger);
feedbackService.Theme.FaultOrDanger, CancellationToken);
}
await player.SkipAsync();
await player.SkipAsync(cancellationToken: CancellationToken);
return await feedbackService.SendContextualMessageResult(
$"Skipped {currentTrack.Display()}", feedbackService.Theme.Success);
$"Skipped {currentTrack.Display()}",
feedbackService.Theme.Success, CancellationToken);
}
}

View file

@ -29,7 +29,7 @@ public sealed class InfoCommandGroup(
public async Task<Result> NowPlayingCommandAsync()
{
var player = await LavalinkPlayer.GetPlayerAsync(
commandContext, audioService, feedbackService);
commandContext, audioService, feedbackService, ct: CancellationToken);
if (player is null)
{
return Result.FromSuccess();
@ -40,11 +40,12 @@ public sealed class InfoCommandGroup(
{
return await feedbackService.SendContextualMessageResult(
"There's nothing playing right now.",
feedbackService.Theme.FaultOrDanger);
feedbackService.Theme.FaultOrDanger, CancellationToken);
}
return await feedbackService.SendContextualMessageResult(
$"You're listening to {currentTrack.Display(true)}", feedbackService.Theme.Text);
$"You're listening to {currentTrack.Display(true)}",
feedbackService.Theme.Text, CancellationToken);
}
[Command("queue")]
@ -54,7 +55,7 @@ public sealed class InfoCommandGroup(
public async Task<Result> QueueCommandAsync()
{
var player = await LavalinkPlayer.GetPlayerAsync(
commandContext, audioService, feedbackService);
commandContext, audioService, feedbackService, ct: CancellationToken);
if (player is null)
{
return Result.FromSuccess();
@ -62,23 +63,24 @@ public sealed class InfoCommandGroup(
var builder = new StringBuilder();
var getResult = await GetQueueAsync(player, builder);
var getResult = await GetQueueAsync(player, builder, CancellationToken);
if (!getResult.IsSuccess)
{
return Result.FromError(getResult.Error);
}
return await feedbackService.SendContextualMessageResult(builder.ToString());
return await feedbackService.SendContextualMessageResult(builder.ToString(), ct: CancellationToken);
}
private async Task<Result> GetQueueAsync(IQueuedLavalinkPlayer player, StringBuilder builder)
private async Task<Result> GetQueueAsync(IQueuedLavalinkPlayer player,
StringBuilder builder, CancellationToken ct = default)
{
var queue = player.Queue;
if (queue.IsEmpty)
{
return await feedbackService.SendContextualMessageResult(
"There's nothing in queue right now.",
feedbackService.Theme.FaultOrDanger);
feedbackService.Theme.FaultOrDanger, ct);
}
var repeatMode = player.RepeatMode;
@ -119,13 +121,14 @@ public sealed class InfoCommandGroup(
public async Task<Result> VolumeCommandAsync()
{
var player = await LavalinkPlayer.GetPlayerAsync(
commandContext, audioService, feedbackService);
commandContext, audioService, feedbackService, ct: CancellationToken);
if (player is null)
{
return Result.FromSuccess();
}
return await feedbackService.SendContextualMessageResult(
$"Current volume is {Markdown.Bold($"{player.Volume * 100}%")}", feedbackService.Theme.Text);
$"Current volume is {Markdown.Bold($"{player.Volume * 100}%")}",
feedbackService.Theme.Text, CancellationToken);
}
}

View file

@ -33,18 +33,18 @@ public sealed class TrustedCommandGroup(
[MinValue(1)] [MaxValue(200)] float percentage)
{
var player = await LavalinkPlayer.GetPlayerAsync(
commandContext, audioService, feedbackService);
commandContext, audioService, feedbackService, ct: CancellationToken);
if (player is null)
{
return Result.FromSuccess();
}
var previousVolume = player.Volume / 100;
await player.SetVolumeAsync(percentage / 100);
await player.SetVolumeAsync(percentage / 100, CancellationToken);
return await feedbackService.SendContextualMessageResult(
$"Volume changed {Markdown.Bold($"{previousVolume}%")} to {Markdown.Bold($"{percentage}%")}",
feedbackService.Theme.Success);
feedbackService.Theme.Success, CancellationToken);
}
[Command("stop")]
@ -55,7 +55,7 @@ public sealed class TrustedCommandGroup(
public async Task<Result> StopCommandAsync()
{
var player = await LavalinkPlayer.GetPlayerAsync(
commandContext, audioService, feedbackService);
commandContext, audioService, feedbackService, ct: CancellationToken);
if (player is null)
{
return Result.FromSuccess();
@ -65,15 +65,16 @@ public sealed class TrustedCommandGroup(
{
return await feedbackService.SendContextualMessageResult(
"There's nothing playing right now.",
feedbackService.Theme.FaultOrDanger);
feedbackService.Theme.FaultOrDanger, CancellationToken);
}
var track = player.CurrentTrack;
await player.StopAsync();
await player.StopAsync(CancellationToken);
return await feedbackService.SendContextualMessageResult(
$"Stopped {track.Display()} and cleared the queue.", feedbackService.Theme.Success);
$"Stopped {track.Display()} and cleared the queue.",
feedbackService.Theme.Success, CancellationToken);
}
[Command("disconnect")]
@ -84,15 +85,15 @@ public sealed class TrustedCommandGroup(
public async Task<Result> DisconnectCommandAsync()
{
var player = await LavalinkPlayer.GetPlayerAsync(
commandContext, audioService, feedbackService);
commandContext, audioService, feedbackService, ct: CancellationToken);
if (player is null)
{
return Result.FromSuccess();
}
await player.DisconnectAsync();
await player.DisconnectAsync(CancellationToken);
return await feedbackService.SendContextualMessageResult(
"Disconnected.", feedbackService.Theme.Success);
"Disconnected.", feedbackService.Theme.Success, CancellationToken);
}
}

View file

@ -9,10 +9,11 @@ namespace Cassette.Extensions;
public static class FeedbackServiceExtensions
{
public static async Task<Result> SendContextualMessageResult(
this IFeedbackService feedbackService, string message, Color? color = null)
this IFeedbackService feedbackService, string message, Color? color = null,
CancellationToken ct = default)
{
return (Result)await feedbackService.SendContextualMessageAsync(
new FeedbackMessage(message, color ?? feedbackService.Theme.Secondary));
new FeedbackMessage(message, color ?? feedbackService.Theme.Secondary), ct: ct);
}
public static async Task<Result> SendContextualEmbedResult(

View file

@ -12,13 +12,14 @@ public abstract class LavalinkPlayer
{
public static async ValueTask<QueuedLavalinkPlayer?> GetPlayerAsync(
ICommandContext commandContext, IAudioService audioService,
FeedbackService feedbackService, bool connectToVoiceChannel = false)
FeedbackService feedbackService, bool connectToVoiceChannel = false,
CancellationToken ct = default)
{
var retrieveOptions = new PlayerRetrieveOptions(
ChannelBehavior: connectToVoiceChannel ? PlayerChannelBehavior.Join : PlayerChannelBehavior.None);
var result = await audioService.Players
.RetrieveAsync(commandContext, PlayerFactory.Queued, retrieveOptions);
.RetrieveAsync(commandContext, PlayerFactory.Queued, retrieveOptions, ct);
if (result.IsSuccess)
{
@ -33,7 +34,7 @@ public abstract class LavalinkPlayer
};
await feedbackService.SendContextualMessageResult(errorMessage,
feedbackService.Theme.FaultOrDanger);
feedbackService.Theme.FaultOrDanger, ct);
return null;
}