".repeat 1 Hello world" repeats hello world once per minute...

This commit is contained in:
Master Kwoth 2016-03-04 20:14:31 +01:00
parent f35c4441ac
commit e5ad9e2b8f
2 changed files with 70 additions and 10 deletions

View File

@ -1,12 +1,37 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Timers;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Commands; using Discord.Commands;
using Discord; using Discord;
using NadekoBot.Classes.Permissions;
namespace NadekoBot.Commands { namespace NadekoBot.Commands {
internal class LogCommand : IDiscordCommand { 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<Server, Channel> logs = new ConcurrentDictionary<Server, Channel>();
private readonly ConcurrentDictionary<Server, Channel> loggingPresences = new ConcurrentDictionary<Server, Channel>();
private readonly ConcurrentDictionary<Channel, Channel> voiceChannelLog = new ConcurrentDictionary<Channel, Channel>();
private readonly ConcurrentDictionary<Server, Repeater> repeaters = new ConcurrentDictionary<Server, Repeater>();
public LogCommand() { public LogCommand() {
NadekoBot.Client.MessageReceived += MsgRecivd; NadekoBot.Client.MessageReceived += MsgRecivd;
NadekoBot.Client.MessageDeleted += MsgDltd; NadekoBot.Client.MessageDeleted += MsgDltd;
@ -14,11 +39,6 @@ namespace NadekoBot.Commands {
NadekoBot.Client.UserUpdated += UsrUpdtd; NadekoBot.Client.UserUpdated += UsrUpdtd;
} }
private readonly ConcurrentDictionary<Server, Channel> logs = new ConcurrentDictionary<Server, Channel>();
private readonly ConcurrentDictionary<Server, Channel> loggingPresences = new ConcurrentDictionary<Server, Channel>();
//
private readonly ConcurrentDictionary<Channel, Channel> voiceChannelLog = new ConcurrentDictionary<Channel, Channel>();
public Func<CommandEventArgs, Task> DoFunc() => async e => { public Func<CommandEventArgs, Task> DoFunc() => async e => {
if (!NadekoBot.IsOwner(e.User.Id) || if (!NadekoBot.IsOwner(e.User.Id) ||
!e.User.ServerPermissions.ManageServer) !e.User.ServerPermissions.ManageServer)
@ -101,7 +121,47 @@ namespace NadekoBot.Commands {
await ch.SendMessage(str); await ch.SendMessage(str);
} catch { } } catch { }
} }
public void Init(CommandGroupBuilder cgb) { 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") cgb.CreateCommand(".logserver")
.Description("Toggles logging in this channel. Logs every message sent/deleted/edited on the server. BOT OWNER ONLY. SERVER OWNER ONLY.") .Description("Toggles logging in this channel. Logs every message sent/deleted/edited on the server. BOT OWNER ONLY. SERVER OWNER ONLY.")
.Do(DoFunc()); .Do(DoFunc());

View File

@ -393,9 +393,9 @@ namespace NadekoBot.Modules {
}); });
cgb.CreateCommand(".heap") cgb.CreateCommand(".heap")
.Description("Shows allocated memory - OWNER ONLY") .Description("Shows allocated memory - OWNER ONLY")
.AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly()) .AddCheck(SimpleCheckers.OwnerOnly())
.Do(async e => { .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}"); await e.Channel.SendMessage($"`Heap Size:` {heap}");
}); });
cgb.CreateCommand(".prune") cgb.CreateCommand(".prune")
@ -603,7 +603,7 @@ namespace NadekoBot.Modules {
.Description("List of lovely people who donated to keep this project alive.") .Description("List of lovely people who donated to keep this project alive.")
.Do(async e => { .Do(async e => {
await Task.Run(async () => { await Task.Run(async () => {
var rows = Classes.DbHandler.Instance.GetAllRows<Donator>(); var rows = DbHandler.Instance.GetAllRows<Donator>();
var donatorsOrdered = rows.OrderByDescending(d => d.Amount); var donatorsOrdered = rows.OrderByDescending(d => d.Amount);
string str = $"**Thanks to the people listed below for making this project happen!**\n"; 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")); var amount = int.Parse(e.GetArg("amount"));
if (donator == null) return; if (donator == null) return;
try { try {
Classes.DbHandler.Instance.InsertData(new Donator { DbHandler.Instance.InsertData(new Donator {
Amount = amount, Amount = amount,
UserName = donator.Name, UserName = donator.Name,
UserId = (long)e.User.Id UserId = (long)e.User.Id
@ -661,7 +661,7 @@ namespace NadekoBot.Modules {
if (arr == null) if (arr == null)
return; return;
var objects = arr.Select(x => x.ToObject<T>()); var objects = arr.Select(x => x.ToObject<T>());
Classes.DbHandler.Instance.InsertMany(objects); DbHandler.Instance.InsertMany(objects);
} catch { } } catch { }
} }
} }