From 748072aa8e7cc71a618239ff4aaa1f42e3baf129 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Wed, 27 Sep 2017 16:22:42 +0200 Subject: [PATCH] .ecr Command added, edits the custom reaction's response given the id --- .../Administration/UserPunishCommands.cs | 5 +- .../CustomReactions/CustomReactions.cs | 54 +++++++++++++++++++ .../Services/CustomReactionsService.cs | 19 +++++++ src/NadekoBot/Services/Impl/StatsService.cs | 2 +- .../_strings/ResponseStrings.en-US.json | 6 ++- src/NadekoBot/data/command_strings.json | 7 +++ 6 files changed, 89 insertions(+), 4 deletions(-) diff --git a/src/NadekoBot/Modules/Administration/UserPunishCommands.cs b/src/NadekoBot/Modules/Administration/UserPunishCommands.cs index d4b29723..fbfd813b 100644 --- a/src/NadekoBot/Modules/Administration/UserPunishCommands.cs +++ b/src/NadekoBot/Modules/Administration/UserPunishCommands.cs @@ -37,7 +37,10 @@ namespace NadekoBot.Modules.Administration .AddField(efb => efb.WithName(GetText("reason")).WithValue(reason ?? "-"))) .ConfigureAwait(false); } - catch { } + catch + { + + } var punishment = await _service.Warn(Context.Guild, user.Id, Context.User.ToString(), reason).ConfigureAwait(false); if (punishment == null) diff --git a/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs b/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs index c3fa8383..4902293f 100644 --- a/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs +++ b/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs @@ -80,6 +80,60 @@ namespace NadekoBot.Modules.CustomReactions ).ConfigureAwait(false); } + [NadekoCommand, Usage, Description, Aliases] + public async Task EditCustReact(int id, [Remainder] string message) + { + var channel = Context.Channel as ITextChannel; + if (string.IsNullOrWhiteSpace(message) || id < 0) + return; + + if ((channel == null && !_creds.IsOwner(Context.User)) || (channel != null && !((IGuildUser)Context.User).GuildPermissions.Administrator)) + { + await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false); + return; + } + + CustomReaction cr; + using (var uow = _db.UnitOfWork) + { + cr = uow.CustomReactions.Get(id); + + if (cr != null) + { + cr.Response = message; + await uow.CompleteAsync().ConfigureAwait(false); + } + } + + if (cr != null) + { + if (channel == null) + { + await _service.EditGcr(id, message).ConfigureAwait(false); + } + else + { + if (_service.GuildReactions.TryGetValue(Context.Guild.Id, out var crs)) + { + var oldCr = crs.FirstOrDefault(x => x.Id == id); + if (oldCr != null) + oldCr.Response = message; + } + } + + await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor() + .WithTitle(GetText("edited_cust_react")) + .WithDescription($"#{cr.Id}") + .AddField(efb => efb.WithName(GetText("trigger")).WithValue(cr.Trigger)) + .AddField(efb => efb.WithName(GetText("response")).WithValue(message.Length > 1024 ? GetText("redacted_too_long") : message)) + ).ConfigureAwait(false); + } + else + { + await ReplyErrorLocalized("edit_fail").ConfigureAwait(false); + } + } + [NadekoCommand, Usage, Description, Aliases] [Priority(1)] public async Task ListCustReact(int page = 1) diff --git a/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs b/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs index f9b1d4d8..f45bcb53 100644 --- a/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs +++ b/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs @@ -59,6 +59,14 @@ namespace NadekoBot.Modules.CustomReactions.Services var id = int.Parse(msg); GlobalReactions = GlobalReactions.Where(cr => cr?.Id != id).ToArray(); }, StackExchange.Redis.CommandFlags.FireAndForget); + sub.Subscribe(_client.CurrentUser.Id + "_gcr.edited", (ch, msg) => + { + var obj = new { Id = 0, Message = "" }; + obj = JsonConvert.DeserializeAnonymousType(msg, obj); + var gcr = GlobalReactions.FirstOrDefault(x => x.Id == obj.Id); + if (gcr != null) + gcr.Response = obj.Message; + }, StackExchange.Redis.CommandFlags.FireAndForget); sub.Subscribe(_client.CurrentUser.Id + "_crad.toggle", (ch, msg) => { var obj = new { Id = 0, Value = false }; @@ -91,6 +99,17 @@ namespace NadekoBot.Modules.CustomReactions.Services GlobalReactions = items.Where(g => g.GuildId == null || g.GuildId == 0).ToArray(); } + public Task EditGcr(int id, string message) + { + var sub = _cache.Redis.GetSubscriber(); + + return sub.PublishAsync(_client.CurrentUser.Id + "_gcr.edited", JsonConvert.SerializeObject(new + { + Id = id, + Message = message, + })); + } + public Task AddGcr(CustomReaction cr) { var sub = _cache.Redis.GetSubscriber(); diff --git a/src/NadekoBot/Services/Impl/StatsService.cs b/src/NadekoBot/Services/Impl/StatsService.cs index e484bfb4..aead9c20 100644 --- a/src/NadekoBot/Services/Impl/StatsService.cs +++ b/src/NadekoBot/Services/Impl/StatsService.cs @@ -20,7 +20,7 @@ namespace NadekoBot.Services.Impl private readonly IBotCredentials _creds; private readonly DateTime _started; - public const string BotVersion = "1.9.1"; + public const string BotVersion = "1.9.2"; public string Author => "Kwoth#2560"; public string Library => "Discord.Net"; diff --git a/src/NadekoBot/_strings/ResponseStrings.en-US.json b/src/NadekoBot/_strings/ResponseStrings.en-US.json index 5e394681..ed93f2d5 100644 --- a/src/NadekoBot/_strings/ResponseStrings.en-US.json +++ b/src/NadekoBot/_strings/ResponseStrings.en-US.json @@ -4,7 +4,8 @@ "customreactions_insuff_perms": "Insufficient permissions. Requires Bot ownership for global custom reactions, and Administrator for server custom reactions.", "customreactions_list_all": "List of all custom reactions", "customreactions_name": "Custom Reactions", - "customreactions_new_cust_react": "New Custom Reaction", + "customreactions_new_cust_react": "\"New Custom Reaction", + "customreactions_edited_cust_react": "Custom Reaction Edited", "customreactions_no_found": "No custom reaction found.", "customreactions_no_found_id": "No custom reaction found with that id.", "customreactions_response": "Response", @@ -880,5 +881,6 @@ "searches_feed_removed": "Feed removed.", "searches_feed_no_feed": "You haven't subscribed to any feeds on this server.", "administration_restart_fail": "You must setup RestartCommand in your credentials.json", - "administration_restarting": "Restarting." + "administration_restarting": "Restarting.", + "customreactions_edit_fail": "Custom reaction with that ID does not exist." } \ No newline at end of file diff --git a/src/NadekoBot/data/command_strings.json b/src/NadekoBot/data/command_strings.json index 2b3432a7..6f84d9a0 100644 --- a/src/NadekoBot/data/command_strings.json +++ b/src/NadekoBot/data/command_strings.json @@ -2988,5 +2988,12 @@ "usage": [ "{0}feeds" ] + }, + "editcustreact": { + "cmd": "editcustreact ecr", + "desc": "Edits the custom reaction's response given it's ID.", + "usage": [ + "{0}ecr 123 I'm a magical girl" + ] } } \ No newline at end of file