1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 00:19:00 +03:00

Always default cancellation tokens (#319)

This PR makes sure that a cancellation token is never *required* to use
an `async` method. This does not affect user experience in any way, only
code quality.

---------

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2024-06-25 15:09:45 +05:00 committed by GitHub
parent a953053f1d
commit a0e7b3a611
Signed by: GitHub
GPG key ID: B5690EEEBB952194
10 changed files with 26 additions and 26 deletions

View file

@ -288,7 +288,7 @@ public sealed class InfoCommandGroup : CommandGroup
return await ShowGuildInfoAsync(bot, guild, CancellationToken); return await ShowGuildInfoAsync(bot, guild, CancellationToken);
} }
private Task<Result> ShowGuildInfoAsync(IUser bot, IGuild guild, CancellationToken ct) private Task<Result> ShowGuildInfoAsync(IUser bot, IGuild guild, CancellationToken ct = default)
{ {
var description = new StringBuilder().AppendLine($"## {guild.Name}"); var description = new StringBuilder().AppendLine($"## {guild.Name}");

View file

@ -170,7 +170,7 @@ public sealed class MuteCommandGroup : CommandGroup
private async Task<Result> SelectMuteMethodAsync( private async Task<Result> SelectMuteMethodAsync(
IUser executor, IUser target, string reason, TimeSpan duration, Snowflake guildId, GuildData data, IUser executor, IUser target, string reason, TimeSpan duration, Snowflake guildId, GuildData data,
IUser bot, DateTimeOffset until, CancellationToken ct) IUser bot, DateTimeOffset until, CancellationToken ct = default)
{ {
var muteRole = GuildSettings.MuteRole.Get(data.Settings); var muteRole = GuildSettings.MuteRole.Get(data.Settings);
@ -186,7 +186,7 @@ public sealed class MuteCommandGroup : CommandGroup
private async Task<Result> RoleMuteUserAsync( private async Task<Result> RoleMuteUserAsync(
IUser executor, IUser target, string reason, Snowflake guildId, GuildData data, IUser executor, IUser target, string reason, Snowflake guildId, GuildData data,
DateTimeOffset until, Snowflake muteRole, CancellationToken ct) DateTimeOffset until, Snowflake muteRole, CancellationToken ct = default)
{ {
var assignRoles = new List<Snowflake> { muteRole }; var assignRoles = new List<Snowflake> { muteRole };
var memberData = data.GetOrCreateMemberData(target.ID); var memberData = data.GetOrCreateMemberData(target.ID);
@ -208,7 +208,7 @@ public sealed class MuteCommandGroup : CommandGroup
private async Task<Result> TimeoutUserAsync( private async Task<Result> TimeoutUserAsync(
IUser executor, IUser target, string reason, TimeSpan duration, Snowflake guildId, IUser executor, IUser target, string reason, TimeSpan duration, Snowflake guildId,
IUser bot, DateTimeOffset until, CancellationToken ct) IUser bot, DateTimeOffset until, CancellationToken ct = default)
{ {
if (duration.TotalDays >= 28) if (duration.TotalDays >= 28)
{ {

View file

@ -78,7 +78,7 @@ public sealed class RemindCommandGroup : CommandGroup
return await ListRemindersAsync(data.GetOrCreateMemberData(executorId), guildId, executor, bot, CancellationToken); return await ListRemindersAsync(data.GetOrCreateMemberData(executorId), guildId, executor, bot, CancellationToken);
} }
private Task<Result> ListRemindersAsync(MemberData data, Snowflake guildId, IUser executor, IUser bot, CancellationToken ct) private Task<Result> ListRemindersAsync(MemberData data, Snowflake guildId, IUser executor, IUser bot, CancellationToken ct = default)
{ {
if (data.Reminders.Count == 0) if (data.Reminders.Count == 0)
{ {
@ -353,7 +353,7 @@ public sealed class RemindCommandGroup : CommandGroup
} }
private Task<Result> DeleteReminderAsync(MemberData data, int index, IUser bot, private Task<Result> DeleteReminderAsync(MemberData data, int index, IUser bot,
CancellationToken ct) CancellationToken ct = default)
{ {
if (index >= data.Reminders.Count) if (index >= data.Reminders.Count)
{ {

View file

@ -90,7 +90,7 @@ public sealed class ToolsCommandGroup : CommandGroup
} }
private Task<Result> SendRandomNumberAsync(long first, long? secondNullable, private Task<Result> SendRandomNumberAsync(long first, long? secondNullable,
IUser executor, CancellationToken ct) IUser executor, CancellationToken ct = default)
{ {
const long secondDefault = 0; const long secondDefault = 0;
var second = secondNullable ?? secondDefault; var second = secondNullable ?? secondDefault;
@ -187,7 +187,7 @@ public sealed class ToolsCommandGroup : CommandGroup
return await SendTimestampAsync(offset, executor, CancellationToken); return await SendTimestampAsync(offset, executor, CancellationToken);
} }
private Task<Result> SendTimestampAsync(TimeSpan? offset, IUser executor, CancellationToken ct) private Task<Result> SendTimestampAsync(TimeSpan? offset, IUser executor, CancellationToken ct = default)
{ {
var timestamp = DateTimeOffset.UtcNow.Add(offset ?? TimeSpan.Zero).ToUnixTimeSeconds(); var timestamp = DateTimeOffset.UtcNow.Add(offset ?? TimeSpan.Zero).ToUnixTimeSeconds();
@ -249,7 +249,7 @@ public sealed class ToolsCommandGroup : CommandGroup
return await AnswerEightBallAsync(bot, CancellationToken); return await AnswerEightBallAsync(bot, CancellationToken);
} }
private Task<Result> AnswerEightBallAsync(IUser bot, CancellationToken ct) private Task<Result> AnswerEightBallAsync(IUser bot, CancellationToken ct = default)
{ {
var typeNumber = Random.Shared.Next(0, 4); var typeNumber = Random.Shared.Next(0, 4);
var embedColor = typeNumber switch var embedColor = typeNumber switch

View file

@ -94,7 +94,7 @@ public sealed class GuildLoadedResponder : IResponder<IGuildCreate>
GuildSettings.PrivateFeedbackChannel.Get(cfg), embedResult: embed, ct: ct); GuildSettings.PrivateFeedbackChannel.Get(cfg), embedResult: embed, ct: ct);
} }
private async Task<Result> SendDataLoadFailed(IGuild guild, GuildData data, IUser bot, CancellationToken ct) private async Task<Result> SendDataLoadFailed(IGuild guild, GuildData data, IUser bot, CancellationToken ct = default)
{ {
var channelResult = await _utility.GetEmergencyFeedbackChannel(guild, data, ct); var channelResult = await _utility.GetEmergencyFeedbackChannel(guild, data, ct);
if (!channelResult.IsDefined(out var channel)) if (!channelResult.IsDefined(out var channel))

View file

@ -81,7 +81,7 @@ public sealed class GuildMemberJoinedResponder : IResponder<IGuildMemberAdd>
} }
private async Task<Result> TryReturnRolesAsync( private async Task<Result> TryReturnRolesAsync(
JsonNode cfg, MemberData memberData, Snowflake guildId, Snowflake userId, CancellationToken ct) JsonNode cfg, MemberData memberData, Snowflake guildId, Snowflake userId, CancellationToken ct = default)
{ {
if (!GuildSettings.ReturnRolesOnRejoin.Get(cfg)) if (!GuildSettings.ReturnRolesOnRejoin.Get(cfg))
{ {

View file

@ -27,7 +27,7 @@ public sealed class GuildDataService : BackgroundService
return SaveAsync(ct); return SaveAsync(ct);
} }
private Task SaveAsync(CancellationToken ct) private Task SaveAsync(CancellationToken ct = default)
{ {
var tasks = new List<Task>(); var tasks = new List<Task>();
var datas = _datas.Values.ToArray(); var datas = _datas.Values.ToArray();
@ -44,7 +44,7 @@ public sealed class GuildDataService : BackgroundService
return Task.WhenAll(tasks); return Task.WhenAll(tasks);
} }
private static async Task SerializeObjectSafelyAsync<T>(T obj, string path, CancellationToken ct) private static async Task SerializeObjectSafelyAsync<T>(T obj, string path, CancellationToken ct = default)
{ {
var tempFilePath = path + ".tmp"; var tempFilePath = path + ".tmp";
await using (var tempFileStream = File.Create(tempFilePath)) await using (var tempFileStream = File.Create(tempFilePath))

View file

@ -62,7 +62,7 @@ public sealed partial class MemberUpdateService : BackgroundService
} }
} }
private async Task<Result> TickMemberDatasAsync(Snowflake guildId, CancellationToken ct) private async Task<Result> TickMemberDatasAsync(Snowflake guildId, CancellationToken ct = default)
{ {
var guildData = await _guildData.GetData(guildId, ct); var guildData = await _guildData.GetData(guildId, ct);
var defaultRole = GuildSettings.DefaultRole.Get(guildData.Settings); var defaultRole = GuildSettings.DefaultRole.Get(guildData.Settings);
@ -79,7 +79,7 @@ public sealed partial class MemberUpdateService : BackgroundService
private async Task<Result> TickMemberDataAsync(Snowflake guildId, GuildData guildData, Snowflake defaultRole, private async Task<Result> TickMemberDataAsync(Snowflake guildId, GuildData guildData, Snowflake defaultRole,
MemberData data, MemberData data,
CancellationToken ct) CancellationToken ct = default)
{ {
var failedResults = new List<Result>(); var failedResults = new List<Result>();
var id = data.Id.ToSnowflake(); var id = data.Id.ToSnowflake();
@ -144,7 +144,7 @@ public sealed partial class MemberUpdateService : BackgroundService
} }
private async Task<Result> TryAutoUnbanAsync( private async Task<Result> TryAutoUnbanAsync(
Snowflake guildId, Snowflake id, MemberData data, CancellationToken ct) Snowflake guildId, Snowflake id, MemberData data, CancellationToken ct = default)
{ {
if (data.BannedUntil is null || DateTimeOffset.UtcNow <= data.BannedUntil) if (data.BannedUntil is null || DateTimeOffset.UtcNow <= data.BannedUntil)
{ {
@ -169,7 +169,7 @@ public sealed partial class MemberUpdateService : BackgroundService
} }
private async Task<Result> TryAutoUnmuteAsync( private async Task<Result> TryAutoUnmuteAsync(
Snowflake guildId, Snowflake id, MemberData data, CancellationToken ct) Snowflake guildId, Snowflake id, MemberData data, CancellationToken ct = default)
{ {
if (data.MutedUntil is null || DateTimeOffset.UtcNow <= data.MutedUntil) if (data.MutedUntil is null || DateTimeOffset.UtcNow <= data.MutedUntil)
{ {
@ -188,7 +188,7 @@ public sealed partial class MemberUpdateService : BackgroundService
} }
private async Task<Result> FilterNicknameAsync(Snowflake guildId, IUser user, IGuildMember member, private async Task<Result> FilterNicknameAsync(Snowflake guildId, IUser user, IGuildMember member,
CancellationToken ct) CancellationToken ct = default)
{ {
var currentNickname = member.Nickname.IsDefined(out var nickname) var currentNickname = member.Nickname.IsDefined(out var nickname)
? nickname ? nickname
@ -226,7 +226,7 @@ public sealed partial class MemberUpdateService : BackgroundService
private static partial Regex IllegalChars(); private static partial Regex IllegalChars();
private async Task<Result> TickReminderAsync(Reminder reminder, IUser user, MemberData data, Snowflake guildId, private async Task<Result> TickReminderAsync(Reminder reminder, IUser user, MemberData data, Snowflake guildId,
CancellationToken ct) CancellationToken ct = default)
{ {
if (DateTimeOffset.UtcNow < reminder.At) if (DateTimeOffset.UtcNow < reminder.At)
{ {

View file

@ -46,7 +46,7 @@ public sealed class ScheduledEventUpdateService : BackgroundService
} }
} }
private async Task<Result> TickScheduledEventsAsync(Snowflake guildId, CancellationToken ct) private async Task<Result> TickScheduledEventsAsync(Snowflake guildId, CancellationToken ct = default)
{ {
var failedResults = new List<Result>(); var failedResults = new List<Result>();
var data = await _guildData.GetData(guildId, ct); var data = await _guildData.GetData(guildId, ct);
@ -133,7 +133,7 @@ public sealed class ScheduledEventUpdateService : BackgroundService
private async Task<Result> TickScheduledEventAsync( private async Task<Result> TickScheduledEventAsync(
Snowflake guildId, GuildData data, IGuildScheduledEvent scheduledEvent, ScheduledEventData eventData, Snowflake guildId, GuildData data, IGuildScheduledEvent scheduledEvent, ScheduledEventData eventData,
CancellationToken ct) CancellationToken ct = default)
{ {
if (GuildSettings.AutoStartEvents.Get(data.Settings) if (GuildSettings.AutoStartEvents.Get(data.Settings)
&& DateTimeOffset.UtcNow >= scheduledEvent.ScheduledStartTime && DateTimeOffset.UtcNow >= scheduledEvent.ScheduledStartTime
@ -160,7 +160,7 @@ public sealed class ScheduledEventUpdateService : BackgroundService
} }
private async Task<Result> AutoStartEventAsync( private async Task<Result> AutoStartEventAsync(
Snowflake guildId, IGuildScheduledEvent scheduledEvent, CancellationToken ct) Snowflake guildId, IGuildScheduledEvent scheduledEvent, CancellationToken ct = default)
{ {
return (Result)await _eventApi.ModifyGuildScheduledEventAsync( return (Result)await _eventApi.ModifyGuildScheduledEventAsync(
guildId, scheduledEvent.ID, guildId, scheduledEvent.ID,
@ -319,7 +319,7 @@ public sealed class ScheduledEventUpdateService : BackgroundService
} }
private async Task<Result> SendScheduledEventCompletedMessage(ScheduledEventData eventData, GuildData data, private async Task<Result> SendScheduledEventCompletedMessage(ScheduledEventData eventData, GuildData data,
CancellationToken ct) CancellationToken ct = default)
{ {
if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty()) if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty())
{ {
@ -351,7 +351,7 @@ public sealed class ScheduledEventUpdateService : BackgroundService
} }
private async Task<Result> SendScheduledEventCancelledMessage(ScheduledEventData eventData, GuildData data, private async Task<Result> SendScheduledEventCancelledMessage(ScheduledEventData eventData, GuildData data,
CancellationToken ct) CancellationToken ct = default)
{ {
if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty()) if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty())
{ {
@ -405,7 +405,7 @@ public sealed class ScheduledEventUpdateService : BackgroundService
} }
private async Task<Result> SendEarlyEventNotificationAsync( private async Task<Result> SendEarlyEventNotificationAsync(
IGuildScheduledEvent scheduledEvent, GuildData data, CancellationToken ct) IGuildScheduledEvent scheduledEvent, GuildData data, CancellationToken ct = default)
{ {
if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty()) if (GuildSettings.EventNotificationChannel.Get(data.Settings).Empty())
{ {

View file

@ -125,7 +125,7 @@ public sealed class Utility
} }
} }
public async Task<Result<Snowflake>> GetEmergencyFeedbackChannel(IGuild guild, GuildData data, CancellationToken ct) public async Task<Result<Snowflake>> GetEmergencyFeedbackChannel(IGuild guild, GuildData data, CancellationToken ct = default)
{ {
var privateFeedback = GuildSettings.PrivateFeedbackChannel.Get(data.Settings); var privateFeedback = GuildSettings.PrivateFeedbackChannel.Get(data.Settings);
if (!privateFeedback.Empty()) if (!privateFeedback.Empty())