diff --git a/NadekoBot/Classes/Permissions/SimpleCheckers.cs b/NadekoBot/Classes/Permissions/SimpleCheckers.cs new file mode 100644 index 00000000..90df1aad --- /dev/null +++ b/NadekoBot/Classes/Permissions/SimpleCheckers.cs @@ -0,0 +1,14 @@ +using Discord; +using Discord.Commands; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NadekoBot.Classes.Permissions { + static class SimpleCheckers { + public static Func OwnerOnly() => + (com, user, ch) => user.Id == NadekoBot.creds.OwnerID; + } +} diff --git a/NadekoBot/Commands/PlayingRotate.cs b/NadekoBot/Commands/PlayingRotate.cs new file mode 100644 index 00000000..53b46f4b --- /dev/null +++ b/NadekoBot/Commands/PlayingRotate.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord.Commands; +using System.Timers; + +namespace NadekoBot.Commands { + class PlayingRotate : DiscordCommand { + + private static List rotatingStatuses = new List(); + private static Timer timer = new Timer(12000); + + private Dictionary> playingPlaceholders => new Dictionary> { + {"%servers%", ()=> NadekoBot.client.Servers.Count().ToString() }, + {"%users%", () => NadekoBot.client.Servers.SelectMany(s=>s.Users).Count().ToString() }, + {"%playing%", () => { + var cnt = Modules.Music.musicPlayers.Count; + if(cnt == 1) { + try { + var mp = Modules.Music.musicPlayers.FirstOrDefault(); + return mp.Value.CurrentSong.Title; + } catch { } + } + return cnt.ToString(); + } + }, + {"%queued%", () => Modules.Music.musicPlayers.Sum(kvp=>kvp.Value.SongQueue.Count).ToString() }, + {"%trivia%", () => Commands.Trivia.runningTrivias.Count.ToString() } + }; + private object playingPlaceholderLock => new object(); + + public PlayingRotate() { + int i = -1; + timer.Elapsed += (s, e) => { + i++; + Console.WriteLine("elapsed"); + string status = ""; + lock (playingPlaceholderLock) { + if (playingPlaceholders.Count == 0) + return; + if (i >= playingPlaceholders.Count) { + i = -1; + return; + } + status = rotatingStatuses[i]; + foreach (var kvp in playingPlaceholders) { + status = status.Replace(kvp.Key, kvp.Value()); + } + } + if (string.IsNullOrWhiteSpace(status)) + return; + NadekoBot.client.SetGame(status); + }; + } + + public override Func DoFunc() => async e => { + if (timer.Enabled) + timer.Stop(); + else + timer.Start(); + await e.Channel.SendMessage($"❗`Rotating playing status has been {(timer.Enabled ? "enabled" : "disabled")}.`"); + }; + + public override void Init(CommandGroupBuilder cgb) { + cgb.CreateCommand(".rotateplaying") + .Alias(".ropl") + .Alias("Toggles rotation of playing status of the dynamic strings you specified earlier.") + .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly()) + .Do(DoFunc()); + + cgb.CreateCommand(".addplaying") + .Alias(".adpl") + .Description("Adds a specified string to the list of playing strings to rotate. Supported placeholders: " + string.Join(", ", playingPlaceholders.Keys)) + .Parameter("text", ParameterType.Unparsed) + .Do(async e => { + var arg = e.GetArg("text"); + if (string.IsNullOrWhiteSpace(arg)) + return; + lock (playingPlaceholderLock) { + rotatingStatuses.Add(arg); + } + await e.Channel.SendMessage("🆗 `Added a new paying string.`"); + }); + + cgb.CreateCommand(".listplaying") + .Alias(".lipl") + .Description("Lists all playing statuses with their corresponding number.") + .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly()) + .Do(async e => { + if (rotatingStatuses.Count == 0) + await e.Channel.SendMessage("`There are no playing strings. Add some with .addplaying [text] command.`"); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < rotatingStatuses.Count; i++) { + sb.AppendLine($"`{i + 1}.` {rotatingStatuses[i]}"); + } + await e.Channel.SendMessage(sb.ToString()); + }); + + cgb.CreateCommand(".removeplaying") + .Alias(".repl") + .Description("Removes a playing string on a given number.") + .Parameter("number", ParameterType.Required) + .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly()) + .Do(async e => { + var arg = e.GetArg("number"); + int num; + string str; + lock (playingPlaceholderLock) { + if (!int.TryParse(arg.Trim(), out num) || num <= 0 || num > rotatingStatuses.Count) + return; + str = rotatingStatuses[num]; + rotatingStatuses.RemoveAt(num - 1); + } + await e.Channel.SendMessage($"🆗 `Removed playing string #{num}`({str})"); + }); + } + } +} diff --git a/NadekoBot/Modules/Administration.cs b/NadekoBot/Modules/Administration.cs index 750309cc..ca662364 100644 --- a/NadekoBot/Modules/Administration.cs +++ b/NadekoBot/Modules/Administration.cs @@ -21,6 +21,7 @@ namespace NadekoBot.Modules { public Administration() : base() { commands.Add(new ServerGreetCommand()); commands.Add(new LogCommand()); + commands.Add(new PlayingRotate()); } public override void Install(ModuleManager manager) { diff --git a/NadekoBot/Modules/NSFW.cs b/NadekoBot/Modules/NSFW.cs index 615f69e6..d9d6ddc8 100644 --- a/NadekoBot/Modules/NSFW.cs +++ b/NadekoBot/Modules/NSFW.cs @@ -49,7 +49,7 @@ namespace NadekoBot.Modules { }); cgb.CreateCommand("~e621") .Description("Shows a random hentai image from e621.net with a given tag. Tag is optional but preffered. Use spaces for multiple tags.\n**Usage**: ~e621 yuri+kissing") - .Parameter("tag", ParameterType.Unparsed + .Parameter("tag", ParameterType.Unparsed) .Do(async e => { string tag = e.GetArg("tag"); if (tag == null) diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index efe406c5..39cdad7c 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -63,7 +63,7 @@ true - true + false true @@ -135,6 +135,7 @@ + @@ -148,6 +149,7 @@ +