mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-05-01 19:49:55 +03:00
Remora.Discord part 4 out of ∞ (well that was painful)
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
parent
67a15f3822
commit
f7dd09d43e
8 changed files with 231 additions and 13 deletions
|
@ -5,11 +5,14 @@ using Microsoft.Extensions.Logging;
|
|||
using Remora.Discord.API.Abstractions.Gateway.Events;
|
||||
using Remora.Discord.API.Abstractions.Objects;
|
||||
using Remora.Discord.API.Abstractions.Rest;
|
||||
using Remora.Discord.API.Objects;
|
||||
using Remora.Discord.Caching;
|
||||
using Remora.Discord.Caching.Services;
|
||||
using Remora.Discord.Extensions.Embeds;
|
||||
using Remora.Discord.Extensions.Formatting;
|
||||
using Remora.Discord.Gateway.Responders;
|
||||
using Remora.Discord.Interactivity;
|
||||
using Remora.Rest.Core;
|
||||
using Remora.Results;
|
||||
|
||||
// ReSharper disable UnusedType.Global
|
||||
|
@ -50,11 +53,10 @@ public class GuildCreateResponder : IResponder<IGuildCreate> {
|
|||
.WithCurrentTimestamp()
|
||||
.WithColour(Color.Aqua)
|
||||
.Build();
|
||||
|
||||
if (!embed.IsDefined(out var built)) return Result.FromError(embed);
|
||||
|
||||
return (Result)await _channelApi.CreateMessageAsync(
|
||||
channel, embeds: new[] { built }!, ct: ct);
|
||||
channel, embeds: new[] { built }, ct: ct);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -62,12 +64,15 @@ public class MessageDeletedResponder : IResponder<IMessageDelete> {
|
|||
private readonly IDiscordRestAuditLogAPI _auditLogApi;
|
||||
private readonly CacheService _cacheService;
|
||||
private readonly IDiscordRestChannelAPI _channelApi;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
|
||||
public MessageDeletedResponder(
|
||||
IDiscordRestAuditLogAPI auditLogApi, CacheService cacheService, IDiscordRestChannelAPI channelApi) {
|
||||
IDiscordRestAuditLogAPI auditLogApi, CacheService cacheService, IDiscordRestChannelAPI channelApi,
|
||||
IDiscordRestUserAPI userApi) {
|
||||
_auditLogApi = auditLogApi;
|
||||
_cacheService = cacheService;
|
||||
_channelApi = channelApi;
|
||||
_userApi = userApi;
|
||||
}
|
||||
|
||||
public async Task<Result> RespondAsync(IMessageDelete gatewayEvent, CancellationToken ct = default) {
|
||||
|
@ -92,12 +97,12 @@ public class MessageDeletedResponder : IResponder<IMessageDelete> {
|
|||
var user = message.Author;
|
||||
if (options.ChannelID == gatewayEvent.ChannelID
|
||||
&& DateTimeOffset.UtcNow.Subtract(auditLog.ID.Timestamp).TotalSeconds <= 2) {
|
||||
var userResult = await _cacheService.TryGetValueAsync<IUser>(
|
||||
new KeyHelpers.UserCacheKey(auditLog.UserID!.Value), ct);
|
||||
var userResult = await auditLog.UserID!.Value.TryGetUserAsync(_cacheService, _userApi, ct);
|
||||
if (!userResult.IsDefined(out user)) return Result.FromError(userResult);
|
||||
}
|
||||
|
||||
Messages.Culture = guildId.GetGuildCulture();
|
||||
|
||||
var embed = new EmbedBuilder()
|
||||
.WithSmallTitle(
|
||||
message.Author,
|
||||
|
@ -127,23 +132,32 @@ public class MessageEditedResponder : IResponder<IMessageUpdate> {
|
|||
}
|
||||
|
||||
public async Task<Result> RespondAsync(IMessageUpdate gatewayEvent, CancellationToken ct = default) {
|
||||
if (!gatewayEvent.GuildID.IsDefined(out var guildId)) return Result.FromSuccess();
|
||||
if (!gatewayEvent.GuildID.IsDefined(out var guildId))
|
||||
return Result.FromSuccess();
|
||||
if (!gatewayEvent.Content.IsDefined(out var newContent))
|
||||
return Result.FromSuccess();
|
||||
|
||||
if (!gatewayEvent.ChannelID.IsDefined(out var channelId))
|
||||
return Result.FromError(new ArgumentNullError(nameof(gatewayEvent.ChannelID)));
|
||||
if (!gatewayEvent.ID.IsDefined(out var messageId))
|
||||
return Result.FromError(new ArgumentNullError(nameof(gatewayEvent.ID)));
|
||||
if (!gatewayEvent.Content.IsDefined(out var newContent))
|
||||
return Result.FromError(new ArgumentNullError(nameof(gatewayEvent.Content)));
|
||||
if (!gatewayEvent.EditedTimestamp.IsDefined(out var timestamp))
|
||||
return Result.FromError(new ArgumentNullError(nameof(gatewayEvent.EditedTimestamp)));
|
||||
|
||||
var cacheKey = new KeyHelpers.MessageCacheKey(channelId, messageId);
|
||||
var messageResult = await _cacheService.TryGetValueAsync<IMessage>(
|
||||
new KeyHelpers.MessageCacheKey(channelId, messageId), ct);
|
||||
cacheKey, ct);
|
||||
if (!messageResult.IsDefined(out var message)) return Result.FromError(messageResult);
|
||||
if (string.IsNullOrWhiteSpace(message.Content)
|
||||
|| string.IsNullOrWhiteSpace(newContent)
|
||||
|| message.Content == newContent) return Result.FromSuccess();
|
||||
|
||||
await _cacheService.EvictAsync<IMessage>(cacheKey, ct);
|
||||
var newMessageResult = await _channelApi.GetChannelMessageAsync(channelId, messageId, ct);
|
||||
if (!newMessageResult.IsDefined(out var newMessage)) return Result.FromError(newMessageResult);
|
||||
// No need to await the recache since we don't depend on it
|
||||
_ = _cacheService.CacheAsync(cacheKey, newMessage, ct);
|
||||
|
||||
var logChannelResult = guildId.GetConfigChannel("PrivateFeedbackChannel");
|
||||
if (!logChannelResult.IsDefined(out var logChannel)) return Result.FromSuccess();
|
||||
|
||||
|
@ -154,12 +168,12 @@ public class MessageEditedResponder : IResponder<IMessageUpdate> {
|
|||
var diff = new SideBySideDiffBuilder(Differ.Instance).BuildDiffModel(message.Content, newContent, true, true);
|
||||
|
||||
Messages.Culture = guildId.GetGuildCulture();
|
||||
|
||||
var embed = new EmbedBuilder()
|
||||
.WithSmallTitle(
|
||||
message.Author,
|
||||
string.Format(Messages.CachedMessageEdited, message.Author.GetTag()),
|
||||
$"https://discord.com/channels/{guildId}/{channelId}/{messageId}")
|
||||
.WithDescription($"{Mention.Channel(message.ChannelID)}\n{diff.AsMarkdown()}")
|
||||
string.Format(Messages.CachedMessageEdited, message.Author.GetTag()))
|
||||
.WithDescription($"https://discord.com/channels/{guildId}/{channelId}/{messageId}\n{diff.AsMarkdown()}")
|
||||
.WithUserFooter(currentUser)
|
||||
.WithTimestamp(timestamp.Value)
|
||||
.WithColour(Color.Gold)
|
||||
|
@ -210,3 +224,90 @@ public class GuildMemberAddResponder : IResponder<IGuildMemberAdd> {
|
|||
channel, embeds: new[] { built }, allowedMentions: Boyfriend.NoMentions, ct: ct);
|
||||
}
|
||||
}
|
||||
|
||||
public class GuildScheduledEventCreateResponder : IResponder<IGuildScheduledEventCreate> {
|
||||
private readonly CacheService _cacheService;
|
||||
private readonly IDiscordRestChannelAPI _channelApi;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
|
||||
public GuildScheduledEventCreateResponder(
|
||||
CacheService cacheService, IDiscordRestChannelAPI channelApi, IDiscordRestUserAPI userApi) {
|
||||
_cacheService = cacheService;
|
||||
_channelApi = channelApi;
|
||||
_userApi = userApi;
|
||||
}
|
||||
|
||||
public async Task<Result> RespondAsync(IGuildScheduledEventCreate gatewayEvent, CancellationToken ct = default) {
|
||||
var channelResult = gatewayEvent.GuildID.GetConfigChannel("EventNotificationChannel");
|
||||
if (!channelResult.IsDefined(out var channel)) return Result.FromSuccess();
|
||||
|
||||
var currentUserResult = await _cacheService.TryGetValueAsync<IUser>(
|
||||
new KeyHelpers.CurrentUserCacheKey(), ct);
|
||||
if (!currentUserResult.IsDefined(out var currentUser)) return Result.FromError(currentUserResult);
|
||||
|
||||
if (!gatewayEvent.CreatorID.IsDefined(out var creatorId))
|
||||
return Result.FromError(new ArgumentNullError(nameof(gatewayEvent.CreatorID)));
|
||||
var creatorResult = await creatorId.Value.TryGetUserAsync(_cacheService, _userApi, ct);
|
||||
if (!creatorResult.IsDefined(out var creator)) return Result.FromError(creatorResult);
|
||||
|
||||
Messages.Culture = gatewayEvent.GuildID.GetGuildCulture();
|
||||
|
||||
string embedDescription;
|
||||
var eventDescription = gatewayEvent.Description is { HasValue: true, Value: not null }
|
||||
? gatewayEvent.Description.Value
|
||||
: string.Empty;
|
||||
switch (gatewayEvent.EntityType) {
|
||||
case GuildScheduledEventEntityType.StageInstance or GuildScheduledEventEntityType.Voice:
|
||||
if (!gatewayEvent.ChannelID.AsOptional().IsDefined(out var channelId))
|
||||
return Result.FromError(new ArgumentNullError(nameof(gatewayEvent.ChannelID)));
|
||||
|
||||
embedDescription = $"{eventDescription}\n\n{Markdown.BlockQuote(
|
||||
string.Format(
|
||||
Messages.LocalEventCreatedDescription,
|
||||
Markdown.Timestamp(gatewayEvent.ScheduledStartTime),
|
||||
Mention.Channel(channelId)
|
||||
))}";
|
||||
break;
|
||||
case GuildScheduledEventEntityType.External:
|
||||
if (!gatewayEvent.EntityMetadata.AsOptional().IsDefined(out var metadata))
|
||||
return Result.FromError(new ArgumentNullError(nameof(gatewayEvent.EntityMetadata)));
|
||||
if (!gatewayEvent.ScheduledEndTime.AsOptional().IsDefined(out var endTime))
|
||||
return Result.FromError(new ArgumentNullError(nameof(gatewayEvent.ScheduledEndTime)));
|
||||
if (!metadata.Location.IsDefined(out var location))
|
||||
return Result.FromError(new ArgumentNullError(nameof(metadata.Location)));
|
||||
|
||||
embedDescription = $"{eventDescription}\n\n{Markdown.BlockQuote(
|
||||
string.Format(
|
||||
Messages.ExternalEventCreatedDescription,
|
||||
Markdown.Timestamp(gatewayEvent.ScheduledStartTime),
|
||||
Markdown.Timestamp(endTime),
|
||||
Markdown.InlineCode(location)
|
||||
))}";
|
||||
break;
|
||||
default:
|
||||
return Result.FromError(new ArgumentOutOfRangeError(nameof(gatewayEvent.EntityType)));
|
||||
}
|
||||
|
||||
var embed = new EmbedBuilder()
|
||||
.WithSmallTitle(creator, string.Format(Messages.EventCreatedTitle, creator.GetTag()))
|
||||
.WithTitle(gatewayEvent.Name)
|
||||
.WithDescription(embedDescription)
|
||||
.WithEventCover(gatewayEvent.ID, gatewayEvent.Image)
|
||||
.WithUserFooter(currentUser)
|
||||
.WithCurrentTimestamp()
|
||||
.WithColour(Color.Gray)
|
||||
.Build();
|
||||
if (!embed.IsDefined(out var built)) return Result.FromError(embed);
|
||||
|
||||
var button = new ButtonComponent(
|
||||
ButtonComponentStyle.Primary,
|
||||
Messages.EventDetailsButton,
|
||||
new PartialEmoji(Name: "📋"),
|
||||
CustomIDHelpers.CreateButtonIDWithState(
|
||||
"scheduled-event-details", $"{gatewayEvent.GuildID}:{gatewayEvent.ID}")
|
||||
);
|
||||
|
||||
return (Result)await _channelApi.CreateMessageAsync(
|
||||
channel, embeds: new[] { built }, components: new[] { new ActionRowComponent(new[] { button }) }, ct: ct);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue