forked from TeamInklings/Octobot
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)
|
.WithColour(ColorsList.Red)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
if (!dmEmbed.IsDefined(out var dmBuilt))
|
await _channelApi.CreateMessageWithEmbedResultAsync(dmChannel.ID, embedResult: dmEmbed, ct: ct);
|
||||||
{
|
|
||||||
return Result.FromError(dmEmbed);
|
|
||||||
}
|
|
||||||
|
|
||||||
await _channelApi.CreateMessageAsync(dmChannel.ID, embeds: new[] { dmBuilt }, ct: ct);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var banResult = await _guildApi.CreateGuildBanAsync(
|
var banResult = await _guildApi.CreateGuildBanAsync(
|
||||||
|
|
|
@ -140,12 +140,7 @@ public class KickCommandGroup : CommandGroup
|
||||||
.WithColour(ColorsList.Red)
|
.WithColour(ColorsList.Red)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
if (!dmEmbed.IsDefined(out var dmBuilt))
|
await _channelApi.CreateMessageWithEmbedResultAsync(dmChannel.ID, embedResult: dmEmbed, ct: ct);
|
||||||
{
|
|
||||||
return Result.FromError(dmEmbed);
|
|
||||||
}
|
|
||||||
|
|
||||||
await _channelApi.CreateMessageAsync(dmChannel.ID, embeds: new[] { dmBuilt }, ct: ct);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var kickResult = await _guildApi.RemoveGuildMemberAsync(
|
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()
|
.WithCurrentTimestamp()
|
||||||
.WithColour(ColorsList.Blue)
|
.WithColour(ColorsList.Blue)
|
||||||
.Build();
|
.Build();
|
||||||
if (!embed.IsDefined(out var built))
|
|
||||||
{
|
|
||||||
return Result.FromError(embed);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (Result)await _channelApi.CreateMessageAsync(
|
return await _channelApi.CreateMessageWithEmbedResultAsync(
|
||||||
GuildSettings.PrivateFeedbackChannel.Get(cfg), embeds: new[] { built }, 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)
|
||||||
{
|
{
|
||||||
|
var channelResult = await _utility.GetEmergencyFeedbackChannel(guild, data, ct);
|
||||||
|
if (!channelResult.IsDefined(out var channel))
|
||||||
|
{
|
||||||
|
return Result.FromError(channelResult);
|
||||||
|
}
|
||||||
|
|
||||||
var errorEmbed = new EmbedBuilder()
|
var errorEmbed = new EmbedBuilder()
|
||||||
.WithSmallTitle(Messages.DataLoadFailedTitle, bot)
|
.WithSmallTitle(Messages.DataLoadFailedTitle, bot)
|
||||||
.WithDescription(Messages.DataLoadFailedDescription)
|
.WithDescription(Messages.DataLoadFailedDescription)
|
||||||
|
@ -110,18 +112,7 @@ public class GuildLoadedResponder : IResponder<IGuildCreate>
|
||||||
.WithColour(ColorsList.Red)
|
.WithColour(ColorsList.Red)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
if (!errorEmbed.IsDefined(out var errorBuilt))
|
return await _channelApi.CreateMessageWithEmbedResultAsync(
|
||||||
{
|
channel, embedResult: errorEmbed, ct: ct);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,13 +72,9 @@ public class GuildMemberJoinedResponder : IResponder<IGuildMemberAdd>
|
||||||
.WithTimestamp(gatewayEvent.JoinedAt)
|
.WithTimestamp(gatewayEvent.JoinedAt)
|
||||||
.WithColour(ColorsList.Green)
|
.WithColour(ColorsList.Green)
|
||||||
.Build();
|
.Build();
|
||||||
if (!embed.IsDefined(out var built))
|
|
||||||
{
|
|
||||||
return Result.FromError(embed);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (Result)await _channelApi.CreateMessageAsync(
|
return await _channelApi.CreateMessageWithEmbedResultAsync(
|
||||||
GuildSettings.PublicFeedbackChannel.Get(cfg), embeds: new[] { built },
|
GuildSettings.PublicFeedbackChannel.Get(cfg), embedResult: embed,
|
||||||
allowedMentions: Octobot.NoMentions, ct: ct);
|
allowedMentions: Octobot.NoMentions, ct: ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -98,13 +98,9 @@ public class MessageDeletedResponder : IResponder<IMessageDelete>
|
||||||
.WithTimestamp(message.Timestamp)
|
.WithTimestamp(message.Timestamp)
|
||||||
.WithColour(ColorsList.Red)
|
.WithColour(ColorsList.Red)
|
||||||
.Build();
|
.Build();
|
||||||
if (!embed.IsDefined(out var built))
|
|
||||||
{
|
|
||||||
return Result.FromError(embed);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (Result)await _channelApi.CreateMessageAsync(
|
return await _channelApi.CreateMessageWithEmbedResultAsync(
|
||||||
GuildSettings.PrivateFeedbackChannel.Get(cfg), embeds: new[] { built },
|
GuildSettings.PrivateFeedbackChannel.Get(cfg), embedResult: embed,
|
||||||
allowedMentions: Octobot.NoMentions, ct: ct);
|
allowedMentions: Octobot.NoMentions, ct: ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,13 +107,9 @@ public class MessageEditedResponder : IResponder<IMessageUpdate>
|
||||||
.WithTimestamp(timestamp.Value)
|
.WithTimestamp(timestamp.Value)
|
||||||
.WithColour(ColorsList.Yellow)
|
.WithColour(ColorsList.Yellow)
|
||||||
.Build();
|
.Build();
|
||||||
if (!embed.IsDefined(out var built))
|
|
||||||
{
|
|
||||||
return Result.FromError(embed);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (Result)await _channelApi.CreateMessageAsync(
|
return await _channelApi.CreateMessageWithEmbedResultAsync(
|
||||||
GuildSettings.PrivateFeedbackChannel.Get(cfg), embeds: new[] { built },
|
GuildSettings.PrivateFeedbackChannel.Get(cfg), embedResult: embed,
|
||||||
allowedMentions: Octobot.NoMentions, ct: ct);
|
allowedMentions: Octobot.NoMentions, ct: ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -236,16 +236,11 @@ public sealed partial class MemberUpdateService : BackgroundService
|
||||||
.WithColour(ColorsList.Magenta)
|
.WithColour(ColorsList.Magenta)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
if (!embed.IsDefined(out var built))
|
var messageResult = await _channelApi.CreateMessageWithEmbedResultAsync(
|
||||||
{
|
reminder.ChannelId.ToSnowflake(), Mention.User(user), embedResult: embed, ct: ct);
|
||||||
return Result.FromError(embed);
|
|
||||||
}
|
|
||||||
|
|
||||||
var messageResult = await _channelApi.CreateMessageAsync(
|
|
||||||
reminder.ChannelId.ToSnowflake(), Mention.User(user), embeds: new[] { built }, ct: ct);
|
|
||||||
if (!messageResult.IsSuccess)
|
if (!messageResult.IsSuccess)
|
||||||
{
|
{
|
||||||
return Result.FromError(messageResult);
|
return messageResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
data.Reminders.Remove(reminder);
|
data.Reminders.Remove(reminder);
|
||||||
|
|
|
@ -215,10 +215,6 @@ public sealed class ScheduledEventUpdateService : BackgroundService
|
||||||
.WithCurrentTimestamp()
|
.WithCurrentTimestamp()
|
||||||
.WithColour(ColorsList.White)
|
.WithColour(ColorsList.White)
|
||||||
.Build();
|
.Build();
|
||||||
if (!embed.IsDefined(out var built))
|
|
||||||
{
|
|
||||||
return Result.FromError(embed);
|
|
||||||
}
|
|
||||||
|
|
||||||
var roleMention = !GuildSettings.EventNotificationRole.Get(settings).Empty()
|
var roleMention = !GuildSettings.EventNotificationRole.Get(settings).Empty()
|
||||||
? Mention.Role(GuildSettings.EventNotificationRole.Get(settings))
|
? Mention.Role(GuildSettings.EventNotificationRole.Get(settings))
|
||||||
|
@ -231,8 +227,8 @@ public sealed class ScheduledEventUpdateService : BackgroundService
|
||||||
URL: $"https://discord.com/events/{scheduledEvent.GuildID}/{scheduledEvent.ID}"
|
URL: $"https://discord.com/events/{scheduledEvent.GuildID}/{scheduledEvent.ID}"
|
||||||
);
|
);
|
||||||
|
|
||||||
return (Result)await _channelApi.CreateMessageAsync(
|
return await _channelApi.CreateMessageWithEmbedResultAsync(
|
||||||
GuildSettings.EventNotificationChannel.Get(settings), roleMention, embeds: new[] { built },
|
GuildSettings.EventNotificationChannel.Get(settings), roleMention, embedResult: embed,
|
||||||
components: new[] { new ActionRowComponent(new[] { button }) }, ct: ct);
|
components: new[] { new ActionRowComponent(new[] { button }) }, ct: ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -317,14 +313,9 @@ public sealed class ScheduledEventUpdateService : BackgroundService
|
||||||
.WithCurrentTimestamp()
|
.WithCurrentTimestamp()
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
if (!startedEmbed.IsDefined(out var startedBuilt))
|
return await _channelApi.CreateMessageWithEmbedResultAsync(
|
||||||
{
|
|
||||||
return Result.FromError(startedEmbed);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (Result)await _channelApi.CreateMessageAsync(
|
|
||||||
GuildSettings.EventNotificationChannel.Get(data.Settings),
|
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,
|
private async Task<Result> SendScheduledEventCompletedMessage(ScheduledEventData eventData, GuildData data,
|
||||||
|
@ -348,14 +339,9 @@ public sealed class ScheduledEventUpdateService : BackgroundService
|
||||||
.WithCurrentTimestamp()
|
.WithCurrentTimestamp()
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
if (!completedEmbed.IsDefined(out var completedBuilt))
|
var createResult = await _channelApi.CreateMessageWithEmbedResultAsync(
|
||||||
{
|
|
||||||
return Result.FromError(completedEmbed);
|
|
||||||
}
|
|
||||||
|
|
||||||
var createResult = (Result)await _channelApi.CreateMessageAsync(
|
|
||||||
GuildSettings.EventNotificationChannel.Get(data.Settings),
|
GuildSettings.EventNotificationChannel.Get(data.Settings),
|
||||||
embeds: new[] { completedBuilt }, ct: ct);
|
embedResult: completedEmbed, ct: ct);
|
||||||
if (createResult.IsSuccess)
|
if (createResult.IsSuccess)
|
||||||
{
|
{
|
||||||
data.ScheduledEvents.Remove(eventData.Id);
|
data.ScheduledEvents.Remove(eventData.Id);
|
||||||
|
@ -380,13 +366,8 @@ public sealed class ScheduledEventUpdateService : BackgroundService
|
||||||
.WithCurrentTimestamp()
|
.WithCurrentTimestamp()
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
if (!embed.IsDefined(out var built))
|
var createResult = await _channelApi.CreateMessageWithEmbedResultAsync(
|
||||||
{
|
GuildSettings.EventNotificationChannel.Get(data.Settings), embedResult: embed, ct: ct);
|
||||||
return Result.FromError(embed);
|
|
||||||
}
|
|
||||||
|
|
||||||
var createResult = (Result)await _channelApi.CreateMessageAsync(
|
|
||||||
GuildSettings.EventNotificationChannel.Get(data.Settings), embeds: new[] { built }, ct: ct);
|
|
||||||
if (createResult.IsSuccess)
|
if (createResult.IsSuccess)
|
||||||
{
|
{
|
||||||
data.ScheduledEvents.Remove(eventData.Id);
|
data.ScheduledEvents.Remove(eventData.Id);
|
||||||
|
@ -445,14 +426,9 @@ public sealed class ScheduledEventUpdateService : BackgroundService
|
||||||
.WithColour(ColorsList.Default)
|
.WithColour(ColorsList.Default)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
if (!earlyResult.IsDefined(out var earlyBuilt))
|
return await _channelApi.CreateMessageWithEmbedResultAsync(
|
||||||
{
|
|
||||||
return Result.FromError(earlyResult);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (Result)await _channelApi.CreateMessageAsync(
|
|
||||||
GuildSettings.EventNotificationChannel.Get(data.Settings),
|
GuildSettings.EventNotificationChannel.Get(data.Settings),
|
||||||
content,
|
content,
|
||||||
embeds: new[] { earlyBuilt }, ct: ct);
|
embedResult: earlyResult, ct: ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -226,26 +226,19 @@ public sealed class UtilityService : IHostedService
|
||||||
.WithColour(color)
|
.WithColour(color)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
if (!logEmbed.IsDefined(out var logBuilt))
|
|
||||||
{
|
|
||||||
return Result.FromError(logEmbed);
|
|
||||||
}
|
|
||||||
|
|
||||||
var builtArray = new[] { logBuilt };
|
|
||||||
|
|
||||||
// Not awaiting to reduce response time
|
// Not awaiting to reduce response time
|
||||||
if (isPublic && publicChannel != channelId)
|
if (isPublic && publicChannel != channelId)
|
||||||
{
|
{
|
||||||
_ = _channelApi.CreateMessageAsync(
|
_ = _channelApi.CreateMessageWithEmbedResultAsync(
|
||||||
publicChannel, embeds: builtArray,
|
publicChannel, embedResult: logEmbed,
|
||||||
ct: ct);
|
ct: ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (privateChannel != publicChannel
|
if (privateChannel != publicChannel
|
||||||
&& privateChannel != channelId)
|
&& privateChannel != channelId)
|
||||||
{
|
{
|
||||||
_ = _channelApi.CreateMessageAsync(
|
_ = _channelApi.CreateMessageWithEmbedResultAsync(
|
||||||
privateChannel, embeds: builtArray,
|
privateChannel, embedResult: logEmbed,
|
||||||
ct: ct);
|
ct: ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue