Some random stuff

This commit is contained in:
Kwoth 2016-08-28 14:19:50 +02:00
parent 4b6ae56f5a
commit fb223effbb
3 changed files with 120 additions and 168 deletions

View File

@ -1,48 +0,0 @@
//using Discord.Commands;
//using NadekoBot.Classes;
//using NadekoBot.DataModels;
//using NadekoBot.Modules.Permissions.Classes;
//using System.IO;
//using System.Linq;
////todo DB
//namespace NadekoBot.Modules.Administration
//{
// internal class IncidentsCommands : DiscordCommand
// {
// public IncidentsCommands(DiscordModule module) : base(module) { }
// internal override void Init(CommandGroupBuilder cgb)
// {
// cgb.CreateCommand(Module.Prefix + "listincidents")
// .Alias(Prefix + "lin")
// .Description($"List all UNREAD incidents and flags them as read. **Needs Manage Server Permissions.**| `{Prefix}lin`")
// .AddCheck(SimpleCheckers.ManageServer())
// .Do(async e =>
// {
// var sid = (long)e.Server.Id;
// var incs = DbHandler.Instance.FindAll<Incident>(i => i.ServerId == sid && i.Read == false);
// DbHandler.Instance.Connection.UpdateAll(incs.Select(i => { i.Read = true; return i; }));
// await umsg.Author.SendMessageAsync(string.Join("\n----------------------", incs.Select(i => i.Text)));
// });
// cgb.CreateCommand(Module.Prefix + "listallincidents")
// .Alias(Prefix + "lain")
// .Description($"Sends you a file containing all incidents and flags them as read. **Needs Manage Server Permissions.**| `{Prefix}lain`")
// .AddCheck(SimpleCheckers.ManageServer())
// .Do(async e =>
// {
// var sid = (long)e.Server.Id;
// var incs = DbHandler.Instance.FindAll<Incident>(i => i.ServerId == sid);
// DbHandler.Instance.Connection.UpdateAll(incs.Select(i => { i.Read = true; return i; }));
// var data = string.Join("\n----------------------\n", incs.Select(i => i.Text));
// MemoryStream ms = new MemoryStream();
// var sw = new StreamWriter(ms);
// sw.WriteLine(data);
// sw.Flush();
// sw.BaseStream.Position = 0;
// await umsg.Author.SendFile("incidents.txt", sw.BaseStream);
// });
// }
// }
//}

View File

@ -1,130 +1,130 @@
//using Discord; using Discord;
//using Discord.Commands; using Discord.Commands;
//using NadekoBot.Classes; using NadekoBot.Classes;
//using NadekoBot.Modules.Permissions.Classes; using NadekoBot.Modules.Permissions.Classes;
//using System; using System;
//using System.Collections.Concurrent; using System.Collections.Concurrent;
//using System.Threading.Tasks; using System.Threading.Tasks;
//using System.Timers; using System.Timers;
////todo DB
////todo persist restarts
//namespace NadekoBot.Modules.Administration
//{
// class MessageRepeater : DiscordCommand
// {
// private readonly ConcurrentDictionary<Server, Repeater> repeaters = new ConcurrentDictionary<Server, Repeater>();
// private class Repeater
// {
// [Newtonsoft.Json.JsonIgnore]
// public Timer MessageTimer { get; set; }
// [Newtonsoft.Json.JsonIgnore]
// public Channel RepeatingChannel { get; set; }
// public ulong RepeatingServerId { get; set; } //todo DB
// public ulong RepeatingChannelId { get; set; } namespace NadekoBot.Modules.Administration
// public Message lastMessage { get; set; } = null; {
// public string RepeatingMessage { get; set; } class MessageRepeater : DiscordCommand
// public int Interval { get; set; } {
private readonly ConcurrentDictionary<Server, Repeater> repeaters = new ConcurrentDictionary<Server, Repeater>();
private class Repeater
{
[Newtonsoft.Json.JsonIgnore]
public Timer MessageTimer { get; set; }
[Newtonsoft.Json.JsonIgnore]
public Channel RepeatingChannel { get; set; }
// public Repeater Start() public ulong RepeatingServerId { get; set; }
// { public ulong RepeatingChannelId { get; set; }
// MessageTimer = new Timer { Interval = Interval }; public Message lastMessage { get; set; } = null;
// MessageTimer.Elapsed += async (s, e) => await Invoke(); public string RepeatingMessage { get; set; }
// return this; public int Interval { get; set; }
// }
// public async Task Invoke() public Repeater Start()
// { {
// var ch = RepeatingChannel; MessageTimer = new Timer { Interval = Interval };
// var msg = RepeatingMessage; MessageTimer.Elapsed += async (s, e) => await Invoke();
// if (ch != null && !string.IsNullOrWhiteSpace(msg)) return this;
// { }
// try
// {
// if (lastMessage != null)
// await lastMessage.Delete().ConfigureAwait(false);
// }
// catch { }
// try
// {
// lastMessage = await ch.SendMessageAsync(msg).ConfigureAwait(false);
// }
// catch { }
// }
// }
// }
// internal override void Init(CommandGroupBuilder cgb)
// {
// cgb.CreateCommand(Module.Prefix + "repeatinvoke") public async Task Invoke()
// .Alias(Module.Prefix + "repinv") {
// .Description($"Immediately shows the repeat message and restarts the timer. **Needs Manage Messages Permissions.**| `{Prefix}repinv`") var ch = RepeatingChannel;
// .AddCheck(SimpleCheckers.ManageMessages()) var msg = RepeatingMessage;
// .Do(async e => if (ch != null && !string.IsNullOrWhiteSpace(msg))
// { {
// Repeater rep; try
// if (!repeaters.TryGetValue(e.Server, out rep)) {
// { if (lastMessage != null)
// await channel.SendMessageAsync("`No repeating message found on this server.`"); await lastMessage.Delete().ConfigureAwait(false);
// return; }
// } catch { }
try
{
lastMessage = await ch.SendMessageAsync(msg).ConfigureAwait(false);
}
catch { }
}
}
}
internal override void Init(CommandGroupBuilder cgb)
{
// await rep.Invoke(); cgb.CreateCommand(Module.Prefix + "repeatinvoke")
// }); .Alias(Module.Prefix + "repinv")
.Description($"Immediately shows the repeat message and restarts the timer. **Needs Manage Messages Permissions.**| `{Prefix}repinv`")
.AddCheck(SimpleCheckers.ManageMessages())
.Do(async e =>
{
Repeater rep;
if (!repeaters.TryGetValue(e.Server, out rep))
{
await channel.SendMessageAsync("`No repeating message found on this server.`");
return;
}
// cgb.CreateCommand(Module.Prefix + "repeat") await rep.Invoke();
// .Description("Repeat a message every X minutes. If no parameters are specified, " + });
// $"repeat is disabled. **Needs Manage Messages Permissions.** |`{Prefix}repeat 5 Hello there`")
// .Parameter("minutes", ParameterType.Optional)
// .Parameter("msg", ParameterType.Unparsed)
// .AddCheck(SimpleCheckers.ManageMessages())
// .Do(async e =>
// {
// var minutesStr = minutes;
// var msg = msg;
// // if both null, disable cgb.CreateCommand(Module.Prefix + "repeat")
// if (string.IsNullOrWhiteSpace(msg) && string.IsNullOrWhiteSpace(minutesStr)) .Description("Repeat a message every X minutes. If no parameters are specified, " +
// { $"repeat is disabled. **Needs Manage Messages Permissions.** |`{Prefix}repeat 5 Hello there`")
.Parameter("minutes", ParameterType.Optional)
.Parameter("msg", ParameterType.Unparsed)
.AddCheck(SimpleCheckers.ManageMessages())
.Do(async e =>
{
var minutesStr = minutes;
var msg = msg;
// Repeater rep; // if both null, disable
// if (!repeaters.TryRemove(e.Server, out rep)) if (string.IsNullOrWhiteSpace(msg) && string.IsNullOrWhiteSpace(minutesStr))
// return; {
// rep.MessageTimer.Stop();
// await channel.SendMessageAsync("Repeating disabled").ConfigureAwait(false);
// return;
// }
// int minutes;
// if (!int.TryParse(minutesStr, out minutes) || minutes < 1 || minutes > 1440)
// {
// await channel.SendMessageAsync("Invalid value").ConfigureAwait(false);
// return;
// }
// var repeater = repeaters.GetOrAdd( Repeater rep;
// e.Server, if (!repeaters.TryRemove(e.Server, out rep))
// s => new Repeater return;
// { rep.MessageTimer.Stop();
// Interval = minutes * 60 * 1000, await channel.SendMessageAsync("Repeating disabled").ConfigureAwait(false);
// RepeatingChannel = e.Channel, return;
// RepeatingChannelId = e.Channel.Id, }
// RepeatingServerId = e.Server.Id, int minutes;
// }.Start() if (!int.TryParse(minutesStr, out minutes) || minutes < 1 || minutes > 1440)
// ); {
await channel.SendMessageAsync("Invalid value").ConfigureAwait(false);
return;
}
// if (!string.IsNullOrWhiteSpace(msg)) var repeater = repeaters.GetOrAdd(
// repeater.RepeatingMessage = msg; e.Server,
s => new Repeater
{
Interval = minutes * 60 * 1000,
RepeatingChannel = e.Channel,
RepeatingChannelId = e.Channel.Id,
RepeatingServerId = e.Server.Id,
}.Start()
);
// repeater.MessageTimer.Stop(); if (!string.IsNullOrWhiteSpace(msg))
// repeater.MessageTimer.Start(); repeater.RepeatingMessage = msg;
// await channel.SendMessageAsync(String.Format("👌 Repeating `{0}` every " + repeater.MessageTimer.Stop();
// "**{1}** minutes on {2} channel.", repeater.MessageTimer.Start();
// repeater.RepeatingMessage, minutes, repeater.RepeatingChannel))
// .ConfigureAwait(false);
// });
// }
// public MessageRepeater(DiscordModule module) : base(module) { } await channel.SendMessageAsync(String.Format("👌 Repeating `{0}` every " +
// } "**{1}** minutes on {2} channel.",
//} repeater.RepeatingMessage, minutes, repeater.RepeatingChannel))
.ConfigureAwait(false);
});
}
public MessageRepeater(DiscordModule module) : base(module) { }
}
}

View File

@ -28,7 +28,7 @@ namespace NadekoBot.Modules.Utility
Quote quote; Quote quote;
using (var uow = DbHandler.Instance.GetUnitOfWork()) using (var uow = DbHandler.Instance.GetUnitOfWork())
{ {
quote = await uow.Quotes.GetRandomQuoteByKeywordAsync(channel.Guild.Id, keyword); quote = await uow.Quotes.GetRandomQuoteByKeywordAsync(channel.Guild.Id, keyword).ConfigureAwait(false);
} }
if (quote == null) if (quote == null)
@ -58,8 +58,8 @@ namespace NadekoBot.Modules.Utility
Keyword = keyword, Keyword = keyword,
Text = text, Text = text,
}); });
await uow.CompleteAsync(); await uow.CompleteAsync().ConfigureAwait(false);
await channel.SendMessageAsync("`Quote added.`"); await channel.SendMessageAsync("`Quote added.`").ConfigureAwait(false);
} }
} }