From c5037a2c1f81dd1baec02ed819687d2c365f8553 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Thu, 5 Jan 2017 13:49:50 +0100 Subject: [PATCH] .rotaterolecolor added. closes #936 --- NadekoBot.sln | 8 --- src/NadekoBot/Modules/Searches/Searches.cs | 6 +- .../Modules/Utility/Commands/Remind.cs | 1 + src/NadekoBot/Modules/Utility/Utility.cs | 70 +++++++++++++++++++ .../Resources/CommandStrings.Designer.cs | 27 +++++++ src/NadekoBot/Resources/CommandStrings.resx | 9 +++ .../Services/Impl/GoogleApiService.cs | 2 +- 7 files changed, 109 insertions(+), 14 deletions(-) diff --git a/NadekoBot.sln b/NadekoBot.sln index e0ebea17..7551e778 100644 --- a/NadekoBot.sln +++ b/NadekoBot.sln @@ -12,8 +12,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "NadekoBot", "src\NadekoBot\NadekoBot.xproj", "{45EC1473-C678-4857-A544-07DFE0D0B478}" EndProject -Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Discord.Net.API", "discord.net\src\Discord.Net.API\Discord.Net.API.xproj", "{834C70DF-1230-4AAA-9C13-48AB232E8D76}" -EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Discord.Net.Commands", "discord.net\src\Discord.Net.Commands\Discord.Net.Commands.xproj", "{078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}" EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Discord.Net.Core", "discord.net\src\Discord.Net.Core\Discord.Net.Core.xproj", "{E5F4786F-58F3-469E-8C87-1908A95436B7}" @@ -35,12 +33,6 @@ Global {45EC1473-C678-4857-A544-07DFE0D0B478}.GlobalNadeko|Any CPU.Build.0 = Release|Any CPU {45EC1473-C678-4857-A544-07DFE0D0B478}.Release|Any CPU.ActiveCfg = Release|Any CPU {45EC1473-C678-4857-A544-07DFE0D0B478}.Release|Any CPU.Build.0 = Release|Any CPU - {834C70DF-1230-4AAA-9C13-48AB232E8D76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {834C70DF-1230-4AAA-9C13-48AB232E8D76}.Debug|Any CPU.Build.0 = Debug|Any CPU - {834C70DF-1230-4AAA-9C13-48AB232E8D76}.GlobalNadeko|Any CPU.ActiveCfg = Debug|Any CPU - {834C70DF-1230-4AAA-9C13-48AB232E8D76}.GlobalNadeko|Any CPU.Build.0 = Debug|Any CPU - {834C70DF-1230-4AAA-9C13-48AB232E8D76}.Release|Any CPU.ActiveCfg = Release|Any CPU - {834C70DF-1230-4AAA-9C13-48AB232E8D76}.Release|Any CPU.Build.0 = Release|Any CPU {078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}.Debug|Any CPU.Build.0 = Debug|Any CPU {078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}.GlobalNadeko|Any CPU.ActiveCfg = Debug|Any CPU diff --git a/src/NadekoBot/Modules/Searches/Searches.cs b/src/NadekoBot/Modules/Searches/Searches.cs index 643ea0e0..16b67e18 100644 --- a/src/NadekoBot/Modules/Searches/Searches.cs +++ b/src/NadekoBot/Modules/Searches/Searches.cs @@ -582,14 +582,10 @@ namespace NadekoBot.Modules.Searches public async Task Color([Remainder] string color = null) { color = color?.Trim().Replace("#", ""); - if (string.IsNullOrWhiteSpace((string)color)) + if (string.IsNullOrWhiteSpace(color)) return; var img = new ImageSharp.Image(50, 50); - var red = Convert.ToInt32(color.Substring(0, 2), 16); - var green = Convert.ToInt32(color.Substring(2, 2), 16); - var blue = Convert.ToInt32(color.Substring(4, 2), 16); - img.BackgroundColor(new ImageSharp.Color(color)); await Context.Channel.SendFileAsync(img.ToStream(), $"{color}.png"); diff --git a/src/NadekoBot/Modules/Utility/Commands/Remind.cs b/src/NadekoBot/Modules/Utility/Commands/Remind.cs index 6cb3965f..58660bce 100644 --- a/src/NadekoBot/Modules/Utility/Commands/Remind.cs +++ b/src/NadekoBot/Modules/Utility/Commands/Remind.cs @@ -9,6 +9,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; namespace NadekoBot.Modules.Utility diff --git a/src/NadekoBot/Modules/Utility/Utility.cs b/src/NadekoBot/Modules/Utility/Utility.cs index e2d17029..3abf4e04 100644 --- a/src/NadekoBot/Modules/Utility/Utility.cs +++ b/src/NadekoBot/Modules/Utility/Utility.cs @@ -10,12 +10,82 @@ using System.Text.RegularExpressions; using System.Reflection; using NadekoBot.Services.Impl; using System.Net.Http; +using System.Collections.Concurrent; +using System.Threading; +using ImageSharp; namespace NadekoBot.Modules.Utility { [NadekoModule("Utility", ".")] public partial class Utility : DiscordModule { + private static ConcurrentDictionary rotatingRoleColors = new ConcurrentDictionary(); + + [NadekoCommand, Usage, Description, Aliases] + [RequireContext(ContextType.Guild)] + [OwnerOnly] + public async Task RotateRoleColor(int timeout, IRole role, params string[] hexes) + { + var channel = (ITextChannel)Context.Channel; + + if (timeout < 0 || timeout > 3600) + return; + + Timer t; + if (timeout == 0 || hexes.Length == 0) + { + if (rotatingRoleColors.TryRemove(role.Id, out t)) + { + t.Change(Timeout.Infinite, Timeout.Infinite); + await channel.SendConfirmAsync($"Stopped rotating colors for the **{role.Name}** role").ConfigureAwait(false); + } + return; + } + + var hexColors = hexes.Select(hex => + { + try { return (ImageSharp.Color?)new ImageSharp.Color(hex.Replace("#", "")); } catch { return null; } + }) + .Where(c => c != null) + .Select(c => c.Value) + .ToArray(); + + if (!hexColors.Any()) + { + await channel.SendMessageAsync("No colors are in the correct format. Use `#00ff00` for example.").ConfigureAwait(false); + return; + } + + var images = hexColors.Select(color => + { + var img = new ImageSharp.Image(50, 50); + img.BackgroundColor(color); + return img; + }).Merge().ToStream(); + + var i = 0; + t = new Timer(async (_) => + { + try + { + var color = hexColors[i]; + await role.ModifyAsync(r => r.Color = new Discord.Color(color.R, color.G, color.B)).ConfigureAwait(false); + ++i; + if (i >= hexColors.Length) + i = 0; + } + catch { } + }, null, 0, timeout * 1000); + + rotatingRoleColors.AddOrUpdate(role.Id, t, (key, old) => + { + old.Change(Timeout.Infinite, Timeout.Infinite); + return t; + }); + + await channel.SendFileAsync(images, "magicalgirl.jpg", $"Rotating **{role.Name}** role's color.").ConfigureAwait(false); + } + [NadekoCommand, Usage, Description, Aliases] public async Task TogetherTube() { diff --git a/src/NadekoBot/Resources/CommandStrings.Designer.cs b/src/NadekoBot/Resources/CommandStrings.Designer.cs index 473a9183..17754f51 100644 --- a/src/NadekoBot/Resources/CommandStrings.Designer.cs +++ b/src/NadekoBot/Resources/CommandStrings.Designer.cs @@ -6053,6 +6053,33 @@ namespace NadekoBot.Resources { } } + /// + /// Looks up a localized string similar to rotaterolecolor rrc. + /// + public static string rotaterolecolor_cmd { + get { + return ResourceManager.GetString("rotaterolecolor_cmd", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Rotates a roles color on an interval with a list of supplied colors. First argument is interval in seconds. Second argument is a role, followed by a space-separated list of colors in hex. Provide a rolename with a 0 interval to disable.. + /// + public static string rotaterolecolor_desc { + get { + return ResourceManager.GetString("rotaterolecolor_desc", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to `{0}rrc 1 MyLsdRole #ff0000 #00ff00 #0000ff` or `{0}rrc 0 MyLsdRole`. + /// + public static string rotaterolecolor_usage { + get { + return ResourceManager.GetString("rotaterolecolor_usage", resourceCulture); + } + } + /// /// Looks up a localized string similar to rps. /// diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index 9f6e7b62..c6e2bd15 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -2862,4 +2862,13 @@ `{0}setstatus Idle` + + rotaterolecolor rrc + + + Rotates a roles color on an interval with a list of supplied colors. First argument is interval in seconds. Second argument is a role, followed by a space-separated list of colors in hex. Provide a rolename with a 0 interval to disable. + + + `{0}rrc 1 MyLsdRole #ff0000 #00ff00 #0000ff` or `{0}rrc 0 MyLsdRole` + \ No newline at end of file diff --git a/src/NadekoBot/Services/Impl/GoogleApiService.cs b/src/NadekoBot/Services/Impl/GoogleApiService.cs index 6e5727f7..56a40965 100644 --- a/src/NadekoBot/Services/Impl/GoogleApiService.cs +++ b/src/NadekoBot/Services/Impl/GoogleApiService.cs @@ -149,7 +149,7 @@ namespace NadekoBot.Services.Impl return toReturn; } - + //todo AsyncEnumerable public async Task> GetVideoDurationsAsync(IEnumerable videoIds) { var videoIdsList = videoIds as List ?? videoIds.ToList();