mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-05-01 19:49:55 +03:00
Remora.Discord part 3 out of ∞
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
parent
c4835a4e78
commit
67a15f3822
8 changed files with 322 additions and 198 deletions
|
@ -1,4 +1,6 @@
|
|||
using System.Drawing;
|
||||
using DiffPlex;
|
||||
using DiffPlex.DiffBuilder;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Remora.Discord.API.Abstractions.Gateway.Events;
|
||||
using Remora.Discord.API.Abstractions.Objects;
|
||||
|
@ -29,32 +31,30 @@ public class GuildCreateResponder : IResponder<IGuildCreate> {
|
|||
var guild = gatewayEvent.Guild.AsT0;
|
||||
Boyfriend.Logger.LogInformation("Joined guild \"{Name}\"", guild.Name);
|
||||
|
||||
var channelResult = guild.ID.GetChannel("PrivateFeedbackChannel");
|
||||
var channelResult = guild.ID.GetConfigChannel("PrivateFeedbackChannel");
|
||||
if (!channelResult.IsDefined(out var channel)) return Result.FromSuccess();
|
||||
|
||||
var currentUserResult = await _userApi.GetCurrentUserAsync(ct);
|
||||
if (!currentUserResult.IsDefined(out var currentUser)) return Result.FromError(currentUserResult);
|
||||
|
||||
if (guild.GetConfigBool("ReceiveStartupMessages").IsDefined(out var shouldSendStartupMessage)
|
||||
&& shouldSendStartupMessage) {
|
||||
Messages.Culture = guild.GetCulture();
|
||||
var i = Random.Shared.Next(1, 4);
|
||||
if (!guild.GetConfigBool("ReceiveStartupMessages").IsDefined(out var shouldSendStartupMessage)
|
||||
|| !shouldSendStartupMessage) return Result.FromSuccess();
|
||||
|
||||
var embed = new EmbedBuilder()
|
||||
.WithTitle(Boyfriend.GetLocalized($"Beep{i}"))
|
||||
.WithDescription(Messages.Ready)
|
||||
.WithUserFooter(currentUser)
|
||||
.WithCurrentTimestamp()
|
||||
.WithColour(Color.Aqua)
|
||||
.Build();
|
||||
Messages.Culture = guild.ID.GetGuildCulture();
|
||||
var i = Random.Shared.Next(1, 4);
|
||||
|
||||
if (!embed.IsDefined(out var built)) return Result.FromError(embed);
|
||||
var embed = new EmbedBuilder()
|
||||
.WithTitle(Boyfriend.GetLocalized($"Beep{i}"))
|
||||
.WithDescription(Messages.Ready)
|
||||
.WithUserFooter(currentUser)
|
||||
.WithCurrentTimestamp()
|
||||
.WithColour(Color.Aqua)
|
||||
.Build();
|
||||
|
||||
return (Result)await _channelApi.CreateMessageAsync(
|
||||
channel, embeds: new[] { built }!, ct: ct);
|
||||
}
|
||||
if (!embed.IsDefined(out var built)) return Result.FromError(embed);
|
||||
|
||||
return Result.FromSuccess();
|
||||
return (Result)await _channelApi.CreateMessageAsync(
|
||||
channel, embeds: new[] { built }!, ct: ct);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -62,60 +62,151 @@ public class MessageDeletedResponder : IResponder<IMessageDelete> {
|
|||
private readonly IDiscordRestAuditLogAPI _auditLogApi;
|
||||
private readonly CacheService _cacheService;
|
||||
private readonly IDiscordRestChannelAPI _channelApi;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
|
||||
public MessageDeletedResponder(
|
||||
IDiscordRestChannelAPI channelApi, IDiscordRestUserAPI userApi, CacheService cacheService,
|
||||
IDiscordRestAuditLogAPI auditLogApi) {
|
||||
_channelApi = channelApi;
|
||||
_userApi = userApi;
|
||||
_cacheService = cacheService;
|
||||
IDiscordRestAuditLogAPI auditLogApi, CacheService cacheService, IDiscordRestChannelAPI channelApi) {
|
||||
_auditLogApi = auditLogApi;
|
||||
_cacheService = cacheService;
|
||||
_channelApi = channelApi;
|
||||
}
|
||||
|
||||
public async Task<Result> RespondAsync(IMessageDelete gatewayEvent, CancellationToken ct = default) {
|
||||
if (!gatewayEvent.GuildID.IsDefined(out var guildId)) return Result.FromSuccess();
|
||||
|
||||
var channelResult = guildId.GetChannel("PrivateFeedbackChannel");
|
||||
if (!channelResult.IsDefined(out var channel)) return Result.FromSuccess();
|
||||
var channelResult = guildId.GetConfigChannel("PrivateFeedbackChannel");
|
||||
if (!channelResult.IsDefined(out var logChannel)) return Result.FromSuccess();
|
||||
|
||||
var messageResult = await _cacheService.TryGetValueAsync<IMessage>(
|
||||
new KeyHelpers.MessageCacheKey(gatewayEvent.ChannelID, gatewayEvent.ID), ct);
|
||||
if (messageResult.IsDefined(out var message)) {
|
||||
var auditLogResult = await _auditLogApi.GetGuildAuditLogAsync(
|
||||
guildId, actionType: AuditLogEvent.MessageDelete, limit: 1, ct: ct);
|
||||
if (!auditLogResult.IsDefined(out var auditLogPage)) return Result.FromError(auditLogResult);
|
||||
if (!messageResult.IsDefined(out var message)) return Result.FromError(messageResult);
|
||||
if (string.IsNullOrWhiteSpace(message.Content)) return Result.FromSuccess();
|
||||
|
||||
var auditLog = auditLogPage.AuditLogEntries.Single();
|
||||
if (!auditLog.Options.IsDefined(out var options))
|
||||
return Result.FromError(new ArgumentNullError(nameof(auditLog.Options)));
|
||||
var auditLogResult = await _auditLogApi.GetGuildAuditLogAsync(
|
||||
guildId, actionType: AuditLogEvent.MessageDelete, limit: 1, ct: ct);
|
||||
if (!auditLogResult.IsDefined(out var auditLogPage)) return Result.FromError(auditLogResult);
|
||||
|
||||
var user = message.Author;
|
||||
if (options.ChannelID == gatewayEvent.ChannelID
|
||||
&& DateTimeOffset.UtcNow.Subtract(auditLog.ID.Timestamp).TotalSeconds <= 2) {
|
||||
var userResult = await _userApi.GetUserAsync(auditLog.UserID!.Value, ct);
|
||||
if (!userResult.IsDefined(out user)) return Result.FromError(userResult);
|
||||
}
|
||||
var auditLog = auditLogPage.AuditLogEntries.Single();
|
||||
if (!auditLog.Options.IsDefined(out var options))
|
||||
return Result.FromError(new ArgumentNullError(nameof(auditLog.Options)));
|
||||
|
||||
var embed = new EmbedBuilder()
|
||||
.WithAuthor(string.Format(Messages.CachedMessageDeleted, message.Author))
|
||||
.WithTitle(
|
||||
message.Author,
|
||||
string.Format(
|
||||
Messages.CachedMessageDeleted,
|
||||
$"{message.Author.Username}#{message.Author.Discriminator:0000}"))
|
||||
.WithDescription(Markdown.BlockCode(message.Content.SanitizeForBlockCode()))
|
||||
.WithActionFooter(user)
|
||||
.WithTimestamp(message.Timestamp)
|
||||
.WithColour(Color.Crimson)
|
||||
.Build();
|
||||
|
||||
if (!embed.IsDefined(out var built)) return Result.FromError(embed);
|
||||
|
||||
return (Result)await _channelApi.CreateMessageAsync(
|
||||
channel, embeds: new[] { built }, allowedMentions: Boyfriend.NoMentions, ct: ct);
|
||||
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);
|
||||
if (!userResult.IsDefined(out user)) return Result.FromError(userResult);
|
||||
}
|
||||
|
||||
return (Result)messageResult;
|
||||
Messages.Culture = guildId.GetGuildCulture();
|
||||
var embed = new EmbedBuilder()
|
||||
.WithSmallTitle(
|
||||
message.Author,
|
||||
string.Format(
|
||||
Messages.CachedMessageDeleted,
|
||||
message.Author.GetTag()))
|
||||
.WithDescription(
|
||||
$"{Mention.Channel(gatewayEvent.ChannelID)}\n{Markdown.BlockCode(message.Content.SanitizeForBlockCode())}")
|
||||
.WithActionFooter(user)
|
||||
.WithTimestamp(message.Timestamp)
|
||||
.WithColour(Color.Crimson)
|
||||
.Build();
|
||||
if (!embed.IsDefined(out var built)) return Result.FromError(embed);
|
||||
|
||||
return (Result)await _channelApi.CreateMessageAsync(
|
||||
logChannel, embeds: new[] { built }, allowedMentions: Boyfriend.NoMentions, ct: ct);
|
||||
}
|
||||
}
|
||||
|
||||
public class MessageEditedResponder : IResponder<IMessageUpdate> {
|
||||
private readonly CacheService _cacheService;
|
||||
private readonly IDiscordRestChannelAPI _channelApi;
|
||||
|
||||
public MessageEditedResponder(CacheService cacheService, IDiscordRestChannelAPI channelApi) {
|
||||
_cacheService = cacheService;
|
||||
_channelApi = channelApi;
|
||||
}
|
||||
|
||||
public async Task<Result> RespondAsync(IMessageUpdate gatewayEvent, CancellationToken ct = default) {
|
||||
if (!gatewayEvent.GuildID.IsDefined(out var guildId)) 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 messageResult = await _cacheService.TryGetValueAsync<IMessage>(
|
||||
new KeyHelpers.MessageCacheKey(channelId, messageId), 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();
|
||||
|
||||
var logChannelResult = guildId.GetConfigChannel("PrivateFeedbackChannel");
|
||||
if (!logChannelResult.IsDefined(out var logChannel)) return Result.FromSuccess();
|
||||
|
||||
var currentUserResult = await _cacheService.TryGetValueAsync<IUser>(
|
||||
new KeyHelpers.CurrentUserCacheKey(), ct);
|
||||
if (!currentUserResult.IsDefined(out var currentUser)) return Result.FromError(currentUserResult);
|
||||
|
||||
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()}")
|
||||
.WithUserFooter(currentUser)
|
||||
.WithTimestamp(timestamp.Value)
|
||||
.WithColour(Color.Gold)
|
||||
.Build();
|
||||
if (!embed.IsDefined(out var built)) return Result.FromError(embed);
|
||||
|
||||
return (Result)await _channelApi.CreateMessageAsync(
|
||||
logChannel, embeds: new[] { built }, allowedMentions: Boyfriend.NoMentions, ct: ct);
|
||||
}
|
||||
}
|
||||
|
||||
public class GuildMemberAddResponder : IResponder<IGuildMemberAdd> {
|
||||
private readonly CacheService _cacheService;
|
||||
private readonly IDiscordRestChannelAPI _channelApi;
|
||||
|
||||
public GuildMemberAddResponder(CacheService cacheService, IDiscordRestChannelAPI channelApi) {
|
||||
_cacheService = cacheService;
|
||||
_channelApi = channelApi;
|
||||
}
|
||||
|
||||
public async Task<Result> RespondAsync(IGuildMemberAdd gatewayEvent, CancellationToken ct = default) {
|
||||
if (!gatewayEvent.GuildID.GetConfigString("WelcomeMessage").IsDefined(out var welcomeMessage)
|
||||
|| welcomeMessage is "off" or "disable" or "disabled")
|
||||
return Result.FromSuccess();
|
||||
if (welcomeMessage is "default" or "reset") {
|
||||
Messages.Culture = gatewayEvent.GuildID.GetGuildCulture();
|
||||
welcomeMessage = Messages.DefaultWelcomeMessage;
|
||||
}
|
||||
|
||||
if (!gatewayEvent.GuildID.GetConfigChannel("PublicFeedbackChannel").IsDefined(out var channel))
|
||||
return Result.FromSuccess();
|
||||
if (!gatewayEvent.User.IsDefined(out var user))
|
||||
return Result.FromError(new ArgumentNullError(nameof(gatewayEvent.User)));
|
||||
|
||||
var guildResult = await _cacheService.TryGetValueAsync<IGuild>(
|
||||
new KeyHelpers.GuildCacheKey(gatewayEvent.GuildID), ct);
|
||||
if (!guildResult.IsDefined(out var guild)) return Result.FromError(guildResult);
|
||||
|
||||
var embed = new EmbedBuilder()
|
||||
.WithSmallTitle(user, string.Format(welcomeMessage, user.GetTag(), guild.Name))
|
||||
.WithGuildFooter(guild)
|
||||
.WithTimestamp(gatewayEvent.JoinedAt)
|
||||
.WithColour(Color.LawnGreen)
|
||||
.Build();
|
||||
if (!embed.IsDefined(out var built)) return Result.FromError(embed);
|
||||
|
||||
return (Result)await _channelApi.CreateMessageAsync(
|
||||
channel, embeds: new[] { built }, allowedMentions: Boyfriend.NoMentions, ct: ct);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue