1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 00:19:00 +03:00

Add link to original message for activated reminders and /listremind (#203)

This commit is contained in:
neroduckale 2023-12-12 01:38:26 +05:00 committed by GitHub
parent e21e193218
commit 59c2c5eada
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 21 deletions

View file

@ -26,19 +26,21 @@ namespace Octobot.Commands;
[UsedImplicitly]
public class RemindCommandGroup : CommandGroup
{
private readonly ICommandContext _context;
private readonly IInteractionCommandContext _context;
private readonly IFeedbackService _feedback;
private readonly GuildDataService _guildData;
private readonly IDiscordRestUserAPI _userApi;
private readonly IDiscordRestInteractionAPI _interactionApi;
public RemindCommandGroup(
ICommandContext context, GuildDataService guildData, IFeedbackService feedback,
IDiscordRestUserAPI userApi)
IInteractionCommandContext context, GuildDataService guildData, IFeedbackService feedback,
IDiscordRestUserAPI userApi, IDiscordRestInteractionAPI interactionApi)
{
_context = context;
_guildData = guildData;
_feedback = feedback;
_userApi = userApi;
_interactionApi = interactionApi;
}
/// <summary>
@ -72,10 +74,10 @@ public class RemindCommandGroup : CommandGroup
var data = await _guildData.GetData(guildId, CancellationToken);
Messages.Culture = GuildSettings.Language.Get(data.Settings);
return await ListRemindersAsync(data.GetOrCreateMemberData(executorId), executor, bot, CancellationToken);
return await ListRemindersAsync(data.GetOrCreateMemberData(executorId), guildId, executor, bot, CancellationToken);
}
private Task<Result> ListRemindersAsync(MemberData data, IUser executor, IUser bot, CancellationToken ct)
private Task<Result> ListRemindersAsync(MemberData data, Snowflake guildId, IUser executor, IUser bot, CancellationToken ct)
{
if (data.Reminders.Count == 0)
{
@ -92,7 +94,8 @@ public class RemindCommandGroup : CommandGroup
var reminder = data.Reminders[i];
builder.AppendBulletPointLine(string.Format(Messages.ReminderPosition, Markdown.InlineCode((i + 1).ToString())))
.AppendSubBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(reminder.Text)))
.AppendSubBulletPointLine(string.Format(Messages.ReminderTime, Markdown.Timestamp(reminder.At)));
.AppendSubBulletPointLine(string.Format(Messages.ReminderTime, Markdown.Timestamp(reminder.At)))
.AppendSubBulletPointLine(string.Format(Messages.DescriptionActionJumpToMessage, $"https://discord.com/channels/{guildId.Value}/{reminder.ChannelId}/{reminder.MessageId}"));
}
var embed = new EmbedBuilder().WithSmallTitle(
@ -139,25 +142,29 @@ public class RemindCommandGroup : CommandGroup
return await AddReminderAsync(@in, text, data, channelId, executor, CancellationToken);
}
private Task<Result> AddReminderAsync(
TimeSpan @in, string text, GuildData data,
private async Task<Result> AddReminderAsync(TimeSpan @in, string text, GuildData data,
Snowflake channelId, IUser executor, CancellationToken ct = default)
{
var remindAt = DateTimeOffset.UtcNow.Add(@in);
var memberData = data.GetOrCreateMemberData(executor.ID);
var remindAt = DateTimeOffset.UtcNow.Add(@in);
var responseResult = await _interactionApi.GetOriginalInteractionResponseAsync(_context.Interaction.ApplicationID, _context.Interaction.Token, ct);
if (!responseResult.IsDefined(out var response))
{
return (Result)responseResult;
}
memberData.Reminders.Add(
new Reminder
{
At = remindAt,
Channel = channelId.Value,
Text = text
ChannelId = channelId.Value,
Text = text,
MessageId = response.ID.Value
});
var builder = new StringBuilder().AppendBulletPointLine(string.Format(
Messages.ReminderText, Markdown.InlineCode(text)))
var builder = new StringBuilder()
.AppendBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(text)))
.AppendBulletPoint(string.Format(Messages.ReminderTime, Markdown.Timestamp(remindAt)));
var embed = new EmbedBuilder().WithSmallTitle(
string.Format(Messages.ReminderCreated, executor.GetTag()), executor)
.WithDescription(builder.ToString())
@ -165,7 +172,7 @@ public class RemindCommandGroup : CommandGroup
.WithFooter(string.Format(Messages.ReminderPosition, memberData.Reminders.Count))
.Build();
return _feedback.SendContextualEmbedResultAsync(embed, ct);
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
}
/// <summary>

View file

@ -4,5 +4,6 @@ public struct Reminder
{
public DateTimeOffset At { get; init; }
public string Text { get; init; }
public ulong Channel { get; init; }
public ulong ChannelId { get; init; }
public ulong MessageId { get; init; }
}

View file

@ -1,3 +1,4 @@
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
@ -114,7 +115,7 @@ public sealed partial class MemberUpdateService : BackgroundService
for (var i = data.Reminders.Count - 1; i >= 0; i--)
{
var reminderTickResult = await TickReminderAsync(data.Reminders[i], user, data, ct);
var reminderTickResult = await TickReminderAsync(data.Reminders[i], user, data, guildId, ct);
failedResults.AddIfFailed(reminderTickResult);
}
@ -217,17 +218,21 @@ public sealed partial class MemberUpdateService : BackgroundService
[GeneratedRegex("[^0-9A-Za-zА-Яа-яЁё]")]
private static partial Regex IllegalChars();
private async Task<Result> TickReminderAsync(Reminder reminder, IUser user, MemberData data, CancellationToken ct)
private async Task<Result> TickReminderAsync(Reminder reminder, IUser user, MemberData data, Snowflake guildId,
CancellationToken ct)
{
if (DateTimeOffset.UtcNow < reminder.At)
{
return Result.FromSuccess();
}
var builder = new StringBuilder()
.AppendBulletPointLine(string.Format(Messages.DescriptionReminder, Markdown.InlineCode(reminder.Text)))
.AppendBulletPointLine(string.Format(Messages.DescriptionActionJumpToMessage, $"https://discord.com/channels/{guildId.Value}/{reminder.ChannelId}/{reminder.MessageId}"));
var embed = new EmbedBuilder().WithSmallTitle(
string.Format(Messages.Reminder, user.GetTag()), user)
.WithDescription(
string.Format(Messages.DescriptionReminder, Markdown.InlineCode(reminder.Text)))
.WithDescription(builder.ToString())
.WithColour(ColorsList.Magenta)
.Build();
@ -237,7 +242,7 @@ public sealed partial class MemberUpdateService : BackgroundService
}
var messageResult = await _channelApi.CreateMessageAsync(
reminder.Channel.ToSnowflake(), Mention.User(user), embeds: new[] { built }, ct: ct);
reminder.ChannelId.ToSnowflake(), Mention.User(user), embeds: new[] { built }, ct: ct);
if (!messageResult.IsSuccess)
{
return Result.FromError(messageResult);