From e5ad9e2b8ff1a8309583bee58010e930c1395d02 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Fri, 4 Mar 2016 20:14:31 +0100 Subject: [PATCH] ".repeat 1 Hello world" repeats hello world once per minute... --- NadekoBot/Commands/LogCommand.cs | 70 ++++++++++++++++++++++++++--- NadekoBot/Modules/Administration.cs | 10 ++--- 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/NadekoBot/Commands/LogCommand.cs b/NadekoBot/Commands/LogCommand.cs index ffc7cb1a..0d7eb8d2 100644 --- a/NadekoBot/Commands/LogCommand.cs +++ b/NadekoBot/Commands/LogCommand.cs @@ -1,12 +1,37 @@ using System; using System.Collections.Concurrent; +using System.Timers; using System.Threading.Tasks; using Discord.Commands; using Discord; +using NadekoBot.Classes.Permissions; namespace NadekoBot.Commands { internal class LogCommand : IDiscordCommand { + private class Repeater { + public readonly Timer MessageTimer = new Timer(); + public Channel ReChannel { get; set; } + public string ReMessage { get; set; } + + public Repeater() { + MessageTimer.Elapsed += async (s, e) => { + try { + var ch = ReChannel; + var msg = ReMessage; + if (ch != null && !string.IsNullOrWhiteSpace(msg)) + await ch.SendMessage(msg); + } catch { } + }; + } + } + + private readonly ConcurrentDictionary logs = new ConcurrentDictionary(); + private readonly ConcurrentDictionary loggingPresences = new ConcurrentDictionary(); + private readonly ConcurrentDictionary voiceChannelLog = new ConcurrentDictionary(); + + private readonly ConcurrentDictionary repeaters = new ConcurrentDictionary(); + public LogCommand() { NadekoBot.Client.MessageReceived += MsgRecivd; NadekoBot.Client.MessageDeleted += MsgDltd; @@ -14,11 +39,6 @@ namespace NadekoBot.Commands { NadekoBot.Client.UserUpdated += UsrUpdtd; } - private readonly ConcurrentDictionary logs = new ConcurrentDictionary(); - private readonly ConcurrentDictionary loggingPresences = new ConcurrentDictionary(); - // - private readonly ConcurrentDictionary voiceChannelLog = new ConcurrentDictionary(); - public Func DoFunc() => async e => { if (!NadekoBot.IsOwner(e.User.Id) || !e.User.ServerPermissions.ManageServer) @@ -101,7 +121,47 @@ namespace NadekoBot.Commands { await ch.SendMessage(str); } catch { } } + public void Init(CommandGroupBuilder cgb) { + cgb.CreateCommand(".repeat") + .Description("Repeat a message every X minutes. If no parameters are specified, repeat is disabled. Requires manage messages.") + .Parameter("minutes", ParameterType.Optional) + .Parameter("msg", ParameterType.Unparsed) + .AddCheck(SimpleCheckers.ManageMessages()) + .Do(async e => { + var minutesStr = e.GetArg("minutes"); + var msg = e.GetArg("msg"); + + // if both null, disable + if (string.IsNullOrWhiteSpace(msg) && string.IsNullOrWhiteSpace(minutesStr)) { + await e.Channel.SendMessage("Repeating disabled"); + Repeater rep; + if (repeaters.TryGetValue(e.Server, out rep)) + rep.MessageTimer.Stop(); + return; + } + int minutes; + if (!int.TryParse(minutesStr, out minutes) || minutes < 1 || minutes > 720) { + await e.Channel.SendMessage("Invalid value"); + return; + } + + var repeater = repeaters.GetOrAdd(e.Server, s => new Repeater()); + + repeater.ReChannel = e.Channel; + repeater.MessageTimer.Interval = minutes * 60 * 1000; + + if (!string.IsNullOrWhiteSpace(msg)) + repeater.ReMessage = msg; + + repeater.MessageTimer.Stop(); + repeater.MessageTimer.Start(); + + await e.Channel.SendMessage(String.Format("👌 Repeating `{0}` every " + + "**{1}** minutes on {2} channel.", + repeater.ReMessage, minutes, repeater.ReChannel)); + }); + cgb.CreateCommand(".logserver") .Description("Toggles logging in this channel. Logs every message sent/deleted/edited on the server. BOT OWNER ONLY. SERVER OWNER ONLY.") .Do(DoFunc()); diff --git a/NadekoBot/Modules/Administration.cs b/NadekoBot/Modules/Administration.cs index ac5a2f26..1dccc5e4 100644 --- a/NadekoBot/Modules/Administration.cs +++ b/NadekoBot/Modules/Administration.cs @@ -393,9 +393,9 @@ namespace NadekoBot.Modules { }); cgb.CreateCommand(".heap") .Description("Shows allocated memory - OWNER ONLY") - .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly()) + .AddCheck(SimpleCheckers.OwnerOnly()) .Do(async e => { - var heap = Task.Run(() => NadekoStats.Instance.Heap()); + var heap = await Task.Run(() => NadekoStats.Instance.Heap()); await e.Channel.SendMessage($"`Heap Size:` {heap}"); }); cgb.CreateCommand(".prune") @@ -603,7 +603,7 @@ namespace NadekoBot.Modules { .Description("List of lovely people who donated to keep this project alive.") .Do(async e => { await Task.Run(async () => { - var rows = Classes.DbHandler.Instance.GetAllRows(); + var rows = DbHandler.Instance.GetAllRows(); var donatorsOrdered = rows.OrderByDescending(d => d.Amount); string str = $"**Thanks to the people listed below for making this project happen!**\n"; @@ -625,7 +625,7 @@ namespace NadekoBot.Modules { var amount = int.Parse(e.GetArg("amount")); if (donator == null) return; try { - Classes.DbHandler.Instance.InsertData(new Donator { + DbHandler.Instance.InsertData(new Donator { Amount = amount, UserName = donator.Name, UserId = (long)e.User.Id @@ -661,7 +661,7 @@ namespace NadekoBot.Modules { if (arr == null) return; var objects = arr.Select(x => x.ToObject()); - Classes.DbHandler.Instance.InsertMany(objects); + DbHandler.Instance.InsertMany(objects); } catch { } } }