From 541e18fff002598827e94f39c93e0142835ab617 Mon Sep 17 00:00:00 2001 From: Macintxsh <95250141+mctaylors@users.noreply.github.com> Date: Sun, 17 Dec 2023 19:47:52 +0300 Subject: [PATCH] Add ChannelApiExtensions (#217) This PR adds an extension method to make it easier to pass Result to CreateMessageAsync --------- Co-authored-by: Octol1ttle --- src/Commands/BanCommandGroup.cs | 7 +-- src/Commands/KickCommandGroup.cs | 7 +-- src/Extensions/ChannelApiExtensions.cs | 29 ++++++++++++ src/Responders/GuildLoadedResponder.cs | 29 +++++------- src/Responders/GuildMemberJoinedResponder.cs | 8 +--- src/Responders/MessageDeletedResponder.cs | 8 +--- src/Responders/MessageEditedResponder.cs | 8 +--- src/Services/Update/MemberUpdateService.cs | 11 ++--- .../Update/ScheduledEventUpdateService.cs | 44 +++++-------------- src/Services/UtilityService.cs | 15 ++----- 10 files changed, 64 insertions(+), 102 deletions(-) create mode 100644 src/Extensions/ChannelApiExtensions.cs diff --git a/src/Commands/BanCommandGroup.cs b/src/Commands/BanCommandGroup.cs index 7493505..f0da978 100644 --- a/src/Commands/BanCommandGroup.cs +++ b/src/Commands/BanCommandGroup.cs @@ -158,12 +158,7 @@ public class BanCommandGroup : CommandGroup .WithColour(ColorsList.Red) .Build(); - if (!dmEmbed.IsDefined(out var dmBuilt)) - { - return Result.FromError(dmEmbed); - } - - await _channelApi.CreateMessageAsync(dmChannel.ID, embeds: new[] { dmBuilt }, ct: ct); + await _channelApi.CreateMessageWithEmbedResultAsync(dmChannel.ID, embedResult: dmEmbed, ct: ct); } var banResult = await _guildApi.CreateGuildBanAsync( diff --git a/src/Commands/KickCommandGroup.cs b/src/Commands/KickCommandGroup.cs index 1ef6057..cad8ea9 100644 --- a/src/Commands/KickCommandGroup.cs +++ b/src/Commands/KickCommandGroup.cs @@ -140,12 +140,7 @@ public class KickCommandGroup : CommandGroup .WithColour(ColorsList.Red) .Build(); - if (!dmEmbed.IsDefined(out var dmBuilt)) - { - return Result.FromError(dmEmbed); - } - - await _channelApi.CreateMessageAsync(dmChannel.ID, embeds: new[] { dmBuilt }, ct: ct); + await _channelApi.CreateMessageWithEmbedResultAsync(dmChannel.ID, embedResult: dmEmbed, ct: ct); } var kickResult = await _guildApi.RemoveGuildMemberAsync( diff --git a/src/Extensions/ChannelApiExtensions.cs b/src/Extensions/ChannelApiExtensions.cs new file mode 100644 index 0000000..12ccf35 --- /dev/null +++ b/src/Extensions/ChannelApiExtensions.cs @@ -0,0 +1,29 @@ +using OneOf; +using Remora.Discord.API.Abstractions.Objects; +using Remora.Discord.API.Abstractions.Rest; +using Remora.Discord.API.Objects; +using Remora.Rest.Core; +using Remora.Results; + +namespace Octobot.Extensions; + +public static class ChannelApiExtensions +{ + public static async Task CreateMessageWithEmbedResultAsync(this IDiscordRestChannelAPI channelApi, + Snowflake channelId, Optional message = default, Optional nonce = default, + Optional isTextToSpeech = default, Optional> embedResult = default, + Optional allowedMentions = default, Optional messageRefenence = default, + Optional> components = default, + Optional> stickerIds = default, + Optional>> attachments = default, + Optional flags = default, CancellationToken ct = default) + { + if (!embedResult.IsDefined() || !embedResult.Value.IsDefined(out var embed)) + { + return Result.FromError(embedResult.Value); + } + + return (Result)await channelApi.CreateMessageAsync(channelId, message, nonce, isTextToSpeech, new[] { embed }, + allowedMentions, messageRefenence, components, stickerIds, attachments, flags, ct); + } +} diff --git a/src/Responders/GuildLoadedResponder.cs b/src/Responders/GuildLoadedResponder.cs index 5d5d68a..cc720c8 100644 --- a/src/Responders/GuildLoadedResponder.cs +++ b/src/Responders/GuildLoadedResponder.cs @@ -92,17 +92,19 @@ public class GuildLoadedResponder : IResponder .WithCurrentTimestamp() .WithColour(ColorsList.Blue) .Build(); - if (!embed.IsDefined(out var built)) - { - return Result.FromError(embed); - } - return (Result)await _channelApi.CreateMessageAsync( - GuildSettings.PrivateFeedbackChannel.Get(cfg), embeds: new[] { built }, ct: ct); + return await _channelApi.CreateMessageWithEmbedResultAsync( + GuildSettings.PrivateFeedbackChannel.Get(cfg), embedResult: embed, ct: ct); } private async Task SendDataLoadFailed(IGuild guild, GuildData data, IUser bot, CancellationToken ct) { + var channelResult = await _utility.GetEmergencyFeedbackChannel(guild, data, ct); + if (!channelResult.IsDefined(out var channel)) + { + return Result.FromError(channelResult); + } + var errorEmbed = new EmbedBuilder() .WithSmallTitle(Messages.DataLoadFailedTitle, bot) .WithDescription(Messages.DataLoadFailedDescription) @@ -110,18 +112,7 @@ public class GuildLoadedResponder : IResponder .WithColour(ColorsList.Red) .Build(); - if (!errorEmbed.IsDefined(out var errorBuilt)) - { - return Result.FromError(errorEmbed); - } - - var channelResult = await _utility.GetEmergencyFeedbackChannel(guild, data, ct); - if (!channelResult.IsDefined(out var channel)) - { - return Result.FromError(channelResult); - } - - return (Result)await _channelApi.CreateMessageAsync( - channel, embeds: new[] { errorBuilt }, ct: ct); + return await _channelApi.CreateMessageWithEmbedResultAsync( + channel, embedResult: errorEmbed, ct: ct); } } diff --git a/src/Responders/GuildMemberJoinedResponder.cs b/src/Responders/GuildMemberJoinedResponder.cs index 09075bf..66faa28 100644 --- a/src/Responders/GuildMemberJoinedResponder.cs +++ b/src/Responders/GuildMemberJoinedResponder.cs @@ -72,13 +72,9 @@ public class GuildMemberJoinedResponder : IResponder .WithTimestamp(gatewayEvent.JoinedAt) .WithColour(ColorsList.Green) .Build(); - if (!embed.IsDefined(out var built)) - { - return Result.FromError(embed); - } - return (Result)await _channelApi.CreateMessageAsync( - GuildSettings.PublicFeedbackChannel.Get(cfg), embeds: new[] { built }, + return await _channelApi.CreateMessageWithEmbedResultAsync( + GuildSettings.PublicFeedbackChannel.Get(cfg), embedResult: embed, allowedMentions: Octobot.NoMentions, ct: ct); } diff --git a/src/Responders/MessageDeletedResponder.cs b/src/Responders/MessageDeletedResponder.cs index 5e4870b..bfedb22 100644 --- a/src/Responders/MessageDeletedResponder.cs +++ b/src/Responders/MessageDeletedResponder.cs @@ -98,13 +98,9 @@ public class MessageDeletedResponder : IResponder .WithTimestamp(message.Timestamp) .WithColour(ColorsList.Red) .Build(); - if (!embed.IsDefined(out var built)) - { - return Result.FromError(embed); - } - return (Result)await _channelApi.CreateMessageAsync( - GuildSettings.PrivateFeedbackChannel.Get(cfg), embeds: new[] { built }, + return await _channelApi.CreateMessageWithEmbedResultAsync( + GuildSettings.PrivateFeedbackChannel.Get(cfg), embedResult: embed, allowedMentions: Octobot.NoMentions, ct: ct); } } diff --git a/src/Responders/MessageEditedResponder.cs b/src/Responders/MessageEditedResponder.cs index 3b0a6aa..16f7af4 100644 --- a/src/Responders/MessageEditedResponder.cs +++ b/src/Responders/MessageEditedResponder.cs @@ -107,13 +107,9 @@ public class MessageEditedResponder : IResponder .WithTimestamp(timestamp.Value) .WithColour(ColorsList.Yellow) .Build(); - if (!embed.IsDefined(out var built)) - { - return Result.FromError(embed); - } - return (Result)await _channelApi.CreateMessageAsync( - GuildSettings.PrivateFeedbackChannel.Get(cfg), embeds: new[] { built }, + return await _channelApi.CreateMessageWithEmbedResultAsync( + GuildSettings.PrivateFeedbackChannel.Get(cfg), embedResult: embed, allowedMentions: Octobot.NoMentions, ct: ct); } } diff --git a/src/Services/Update/MemberUpdateService.cs b/src/Services/Update/MemberUpdateService.cs index c3139c3..e7860ae 100644 --- a/src/Services/Update/MemberUpdateService.cs +++ b/src/Services/Update/MemberUpdateService.cs @@ -236,16 +236,11 @@ public sealed partial class MemberUpdateService : BackgroundService .WithColour(ColorsList.Magenta) .Build(); - if (!embed.IsDefined(out var built)) - { - return Result.FromError(embed); - } - - var messageResult = await _channelApi.CreateMessageAsync( - reminder.ChannelId.ToSnowflake(), Mention.User(user), embeds: new[] { built }, ct: ct); + var messageResult = await _channelApi.CreateMessageWithEmbedResultAsync( + reminder.ChannelId.ToSnowflake(), Mention.User(user), embedResult: embed, ct: ct); if (!messageResult.IsSuccess) { - return Result.FromError(messageResult); + return messageResult; } data.Reminders.Remove(reminder); diff --git a/src/Services/Update/ScheduledEventUpdateService.cs b/src/Services/Update/ScheduledEventUpdateService.cs index 9ec9fcf..38fe4a7 100644 --- a/src/Services/Update/ScheduledEventUpdateService.cs +++ b/src/Services/Update/ScheduledEventUpdateService.cs @@ -215,10 +215,6 @@ public sealed class ScheduledEventUpdateService : BackgroundService .WithCurrentTimestamp() .WithColour(ColorsList.White) .Build(); - if (!embed.IsDefined(out var built)) - { - return Result.FromError(embed); - } var roleMention = !GuildSettings.EventNotificationRole.Get(settings).Empty() ? Mention.Role(GuildSettings.EventNotificationRole.Get(settings)) @@ -231,8 +227,8 @@ public sealed class ScheduledEventUpdateService : BackgroundService URL: $"https://discord.com/events/{scheduledEvent.GuildID}/{scheduledEvent.ID}" ); - return (Result)await _channelApi.CreateMessageAsync( - GuildSettings.EventNotificationChannel.Get(settings), roleMention, embeds: new[] { built }, + return await _channelApi.CreateMessageWithEmbedResultAsync( + GuildSettings.EventNotificationChannel.Get(settings), roleMention, embedResult: embed, components: new[] { new ActionRowComponent(new[] { button }) }, ct: ct); } @@ -317,14 +313,9 @@ public sealed class ScheduledEventUpdateService : BackgroundService .WithCurrentTimestamp() .Build(); - if (!startedEmbed.IsDefined(out var startedBuilt)) - { - return Result.FromError(startedEmbed); - } - - return (Result)await _channelApi.CreateMessageAsync( + return await _channelApi.CreateMessageWithEmbedResultAsync( GuildSettings.EventNotificationChannel.Get(data.Settings), - content, embeds: new[] { startedBuilt }, ct: ct); + content, embedResult: startedEmbed, ct: ct); } private async Task SendScheduledEventCompletedMessage(ScheduledEventData eventData, GuildData data, @@ -348,14 +339,9 @@ public sealed class ScheduledEventUpdateService : BackgroundService .WithCurrentTimestamp() .Build(); - if (!completedEmbed.IsDefined(out var completedBuilt)) - { - return Result.FromError(completedEmbed); - } - - var createResult = (Result)await _channelApi.CreateMessageAsync( + var createResult = await _channelApi.CreateMessageWithEmbedResultAsync( GuildSettings.EventNotificationChannel.Get(data.Settings), - embeds: new[] { completedBuilt }, ct: ct); + embedResult: completedEmbed, ct: ct); if (createResult.IsSuccess) { data.ScheduledEvents.Remove(eventData.Id); @@ -380,13 +366,8 @@ public sealed class ScheduledEventUpdateService : BackgroundService .WithCurrentTimestamp() .Build(); - if (!embed.IsDefined(out var built)) - { - return Result.FromError(embed); - } - - var createResult = (Result)await _channelApi.CreateMessageAsync( - GuildSettings.EventNotificationChannel.Get(data.Settings), embeds: new[] { built }, ct: ct); + var createResult = await _channelApi.CreateMessageWithEmbedResultAsync( + GuildSettings.EventNotificationChannel.Get(data.Settings), embedResult: embed, ct: ct); if (createResult.IsSuccess) { data.ScheduledEvents.Remove(eventData.Id); @@ -445,14 +426,9 @@ public sealed class ScheduledEventUpdateService : BackgroundService .WithColour(ColorsList.Default) .Build(); - if (!earlyResult.IsDefined(out var earlyBuilt)) - { - return Result.FromError(earlyResult); - } - - return (Result)await _channelApi.CreateMessageAsync( + return await _channelApi.CreateMessageWithEmbedResultAsync( GuildSettings.EventNotificationChannel.Get(data.Settings), content, - embeds: new[] { earlyBuilt }, ct: ct); + embedResult: earlyResult, ct: ct); } } diff --git a/src/Services/UtilityService.cs b/src/Services/UtilityService.cs index d40570a..9ac481b 100644 --- a/src/Services/UtilityService.cs +++ b/src/Services/UtilityService.cs @@ -226,26 +226,19 @@ public sealed class UtilityService : IHostedService .WithColour(color) .Build(); - if (!logEmbed.IsDefined(out var logBuilt)) - { - return Result.FromError(logEmbed); - } - - var builtArray = new[] { logBuilt }; - // Not awaiting to reduce response time if (isPublic && publicChannel != channelId) { - _ = _channelApi.CreateMessageAsync( - publicChannel, embeds: builtArray, + _ = _channelApi.CreateMessageWithEmbedResultAsync( + publicChannel, embedResult: logEmbed, ct: ct); } if (privateChannel != publicChannel && privateChannel != channelId) { - _ = _channelApi.CreateMessageAsync( - privateChannel, embeds: builtArray, + _ = _channelApi.CreateMessageWithEmbedResultAsync( + privateChannel, embedResult: logEmbed, ct: ct); }