mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-01-31 00:19:00 +03:00
Add ChannelApiExtensions (#217)
This PR adds an extension method to make it easier to pass Result<Embed> to CreateMessageAsync --------- Co-authored-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
parent
9a23e1d533
commit
541e18fff0
10 changed files with 64 additions and 102 deletions
|
@ -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(
|
||||
|
|
|
@ -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(
|
||||
|
|
29
src/Extensions/ChannelApiExtensions.cs
Normal file
29
src/Extensions/ChannelApiExtensions.cs
Normal file
|
@ -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<Result> CreateMessageWithEmbedResultAsync(this IDiscordRestChannelAPI channelApi,
|
||||
Snowflake channelId, Optional<string> message = default, Optional<string> nonce = default,
|
||||
Optional<bool> isTextToSpeech = default, Optional<Result<Embed>> embedResult = default,
|
||||
Optional<IAllowedMentions> allowedMentions = default, Optional<IMessageReference> messageRefenence = default,
|
||||
Optional<IReadOnlyList<IMessageComponent>> components = default,
|
||||
Optional<IReadOnlyList<Snowflake>> stickerIds = default,
|
||||
Optional<IReadOnlyList<OneOf<FileData, IPartialAttachment>>> attachments = default,
|
||||
Optional<MessageFlags> 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);
|
||||
}
|
||||
}
|
|
@ -92,17 +92,19 @@ public class GuildLoadedResponder : IResponder<IGuildCreate>
|
|||
.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<Result> 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<IGuildCreate>
|
|||
.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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -72,13 +72,9 @@ public class GuildMemberJoinedResponder : IResponder<IGuildMemberAdd>
|
|||
.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);
|
||||
}
|
||||
|
||||
|
|
|
@ -98,13 +98,9 @@ public class MessageDeletedResponder : IResponder<IMessageDelete>
|
|||
.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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -107,13 +107,9 @@ public class MessageEditedResponder : IResponder<IMessageUpdate>
|
|||
.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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<Result> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue