From 5c933b676d44e4a81e953f14a94b17df3b184c13 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sat, 2 Jul 2022 14:15:02 +0200 Subject: [PATCH] .timely will now have a button to set a reminder --- src/NadekoBot/Common/NadekoModule.cs | 6 +-- src/NadekoBot/Modules/Gambling/Gambling.cs | 47 +++++++++++++++++-- .../Modules/Utility/Remind/RemindService.cs | 22 +++++++++ .../strings/responses/responses.en-US.json | 2 + 4 files changed, 71 insertions(+), 6 deletions(-) diff --git a/src/NadekoBot/Common/NadekoModule.cs b/src/NadekoBot/Common/NadekoModule.cs index d261fb4cf..335adfc5d 100644 --- a/src/NadekoBot/Common/NadekoModule.cs +++ b/src/NadekoBot/Common/NadekoModule.cs @@ -67,13 +67,13 @@ public abstract class NadekoModule : ModuleBase // localized replies public Task ReplyErrorLocalizedAsync(LocStr str, NadekoButtonInteraction inter = null) - => SendErrorAsync($"{Format.Bold(ctx.User.ToString())} {GetText(str)}"); + => SendErrorAsync($"{Format.Bold(ctx.User.ToString())} {GetText(str)}", inter); public Task ReplyPendingLocalizedAsync(LocStr str, NadekoButtonInteraction inter = null) - => SendPendingAsync($"{Format.Bold(ctx.User.ToString())} {GetText(str)}"); + => SendPendingAsync($"{Format.Bold(ctx.User.ToString())} {GetText(str)}", inter); public Task ReplyConfirmLocalizedAsync(LocStr str, NadekoButtonInteraction inter = null) - => SendConfirmAsync($"{Format.Bold(ctx.User.ToString())} {GetText(str)}"); + => SendConfirmAsync($"{Format.Bold(ctx.User.ToString())} {GetText(str)}", inter); public async Task PromptUserConfirmAsync(IEmbedBuilder embed) { diff --git a/src/NadekoBot/Modules/Gambling/Gambling.cs b/src/NadekoBot/Modules/Gambling/Gambling.cs index 38cb0eca3..aa20e9c38 100644 --- a/src/NadekoBot/Modules/Gambling/Gambling.cs +++ b/src/NadekoBot/Modules/Gambling/Gambling.cs @@ -7,6 +7,7 @@ using NadekoBot.Modules.Utility.Patronage; using NadekoBot.Modules.Gambling.Bank; using NadekoBot.Modules.Gambling.Common; using NadekoBot.Modules.Gambling.Services; +using NadekoBot.Modules.Utility.Services; using NadekoBot.Services.Currency; using NadekoBot.Services.Database.Models; using System.Collections.Immutable; @@ -44,6 +45,7 @@ public partial class Gambling : GamblingModule private readonly GamblingConfigService _configService; private readonly IBankService _bank; private readonly IPatronageService _ps; + private readonly RemindService _remind; private IUserMessage rdMsg; @@ -54,7 +56,8 @@ public partial class Gambling : GamblingModule DownloadTracker tracker, GamblingConfigService configService, IBankService bank, - IPatronageService ps) + IPatronageService ps, + RemindService remind) : base(configService) { _db = db; @@ -62,6 +65,7 @@ public partial class Gambling : GamblingModule _client = client; _bank = bank; _ps = ps; + _remind = remind; _enUsCulture = new CultureInfo("en-US", false).NumberFormat; _enUsCulture.NumberDecimalDigits = 0; @@ -109,7 +113,40 @@ public partial class Gambling : GamblingModule Key = "timely:extra_percent", PrettyName = "Timely" }; - + + public class RemindMeInteraction : NInteraction + { + public RemindMeInteraction( + [NotNull] DiscordSocketClient client, + ulong userId, + [NotNull] Func action) + : base(client, userId, action) + { + } + + protected override NadekoInteractionData Data + => new NadekoInteractionData( + Emote: Emoji.Parse("⏰"), + CustomId: "timely:remind_me", + Text: "Remind me" + ); + } + + private Func RemindTimelyAction(DateTime when) + => async smc => + { + var tt = TimestampTag.FromDateTime(when, TimestampTagStyles.Relative); + + await _remind.AddReminderAsync(ctx.User.Id, + ctx.Channel.Id, + ctx.Guild.Id, + true, + when, + GetText(strs.timely_time)); + + await smc.RespondConfirmAsync(_eb, GetText(strs.remind_timely(tt)), ephemeral: true); + }; + [Cmd] public async partial Task Timely() { @@ -135,7 +172,11 @@ public partial class Gambling : GamblingModule await _cs.AddAsync(ctx.User.Id, val, new("timely", "claim")); - await ReplyConfirmLocalizedAsync(strs.timely(N(val), period)); + var inter = new RemindMeInteraction(_client, + ctx.User.Id, + RemindTimelyAction(DateTime.UtcNow.Add(TimeSpan.FromHours(period)))); + + await ReplyConfirmLocalizedAsync(strs.timely(N(val), period), inter.GetInteraction()); } [Cmd] diff --git a/src/NadekoBot/Modules/Utility/Remind/RemindService.cs b/src/NadekoBot/Modules/Utility/Remind/RemindService.cs index d7d32ca25..4fbac330e 100644 --- a/src/NadekoBot/Modules/Utility/Remind/RemindService.cs +++ b/src/NadekoBot/Modules/Utility/Remind/RemindService.cs @@ -178,4 +178,26 @@ public class RemindService : INService, IReadyExecutor public string What { get; set; } public TimeSpan Time { get; set; } } + + public async Task AddReminderAsync(ulong userId, + ulong channelId, + ulong? guildId, + bool isPrivate, + DateTime time, + string message) + { + var rem = new Reminder + { + UserId = userId, + ChannelId = channelId, + ServerId = guildId ?? 0, + IsPrivate = isPrivate, + When = time, + Message = message, + }; + + await using var ctx = _db.GetDbContext(); + await ctx.Reminders + .AddAsync(rem); + } } \ No newline at end of file diff --git a/src/NadekoBot/data/strings/responses/responses.en-US.json b/src/NadekoBot/data/strings/responses/responses.en-US.json index 06bbf6b59..9716d9f3a 100644 --- a/src/NadekoBot/data/strings/responses/responses.en-US.json +++ b/src/NadekoBot/data/strings/responses/responses.en-US.json @@ -596,6 +596,7 @@ "quote_deleted": "Quote #{0} deleted.", "region": "Region", "remind": "I will remind {0} to {1} in {2} `({3:d.M.yyyy.} at {4:HH:mm})`", + "remind_timely": "I will remind you about your timely reward {0}", "remind_invalid": "Not a valid remind format. Remind must have a target, timer and a reason. Check the command list.", "remind_too_long": "Remind time has exceeded maximum.", "repeater_redundant_no": "Repeater **#{0}** won't post redundant messages anymore.", @@ -883,6 +884,7 @@ "timely_set": "Users will be able to claim {0} every {1}h", "timely_set_none": "Users will not be able to claim any timely currency.", "timely_reset": "All users will be able to claim timely currency again.", + "timely_time": "It's time for your timely reward.", "price": "Price", "market_cap": "Market Cap", "market_cap_dominance": "Dominance",