very minor things, added disguise, code reorganizaiton

This commit is contained in:
Master Kwoth 2016-03-08 22:42:43 +01:00
parent 1bc681e1aa
commit aa62ac21a2
10 changed files with 138 additions and 66 deletions

View File

@ -12,6 +12,8 @@ using NadekoBot.Classes;
namespace NadekoBot.Extensions {
public static class Extensions
{
private static Random rng = new Random();
public static string Scramble(this string word) {
var letters = word.ToArray();
@ -32,7 +34,7 @@ namespace NadekoBot.Extensions {
}
return "`"+string.Join(" ", letters)+"`";
}
public static string TrimTo(this string str, int num) {
public static string TrimTo(this string str, int num, bool hideDots = false) {
if (num < 0)
throw new ArgumentOutOfRangeException(nameof(num), "TrimTo argument cannot be less than 0");
if (num == 0)
@ -41,7 +43,7 @@ namespace NadekoBot.Extensions {
return string.Join("", str.Select(c => '.'));
if (str.Length < num)
return str;
return string.Join("", str.Take(num - 3)) + "...";
return string.Join("", str.Take(num - 3)) + (hideDots ? "" : "...");
}
/// <summary>
/// Removes trailing S or ES (if specified) on the given string if the num is 1
@ -162,6 +164,11 @@ namespace NadekoBot.Extensions {
/// <param name="action"></param>
public static string GetRuntime(this DiscordClient c) => ".Net Framework 4.5.2";
public static string Matrix(this string s)
=>
string.Join("", s.Select(c => c.ToString() + " ̵̢̬̜͉̞̭̖̰͋̉̎ͬ̔̇̌̀".TrimTo(rng.Next(0, 12), true)));
//.Replace("`", "");
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) {
foreach (var element in source) {
action(element);

View File

@ -16,6 +16,13 @@ namespace NadekoBot.Classes.JSONModels {
143515953525817344
};
public string[] DisguiseResponses { get; } = {
"https://cdn.discordapp.com/attachments/140007341880901632/156721710458994690/Cc5mixjUYAADgBs.jpg",
"https://cdn.discordapp.com/attachments/140007341880901632/156721715831898113/hqdefault.jpg",
"https://cdn.discordapp.com/attachments/140007341880901632/156721724430352385/okawari_01_haruka_weird_mask.jpg",
"https://cdn.discordapp.com/attachments/140007341880901632/156721728763068417/mustache-best-girl.png"
};
public string[] CryResponses { get; } = {
"http://i.imgur.com/Xg3i1Qy.gif",
"http://i.imgur.com/3K8DRrU.gif",

View File

@ -8,11 +8,10 @@ namespace NadekoBot.Classes.Trivia {
public class TriviaQuestion {
//represents the min size to judge levDistance with
private static readonly HashSet<Tuple<int, int>> strictness = new HashSet<Tuple<int, int>> {
new Tuple<int, int>(6, 0),
new Tuple<int, int>(7, 1),
new Tuple<int, int>(12, 2),
new Tuple<int, int>(17, 3),
new Tuple<int, int>(22, 4),
new Tuple<int, int>(9, 0),
new Tuple<int, int>(14, 1),
new Tuple<int, int>(19, 2),
new Tuple<int, int>(22, 3),
};
public static int maxStringLength = 22;

View File

@ -10,29 +10,10 @@ 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<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() {
NadekoBot.Client.MessageReceived += MsgRecivd;
NadekoBot.Client.MessageDeleted += MsgDltd;
@ -139,44 +120,6 @@ namespace NadekoBot.Commands {
}
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.")

View File

@ -0,0 +1,89 @@
using System;
using System.Timers;
using System.Collections.Concurrent;
using Discord;
using NadekoBot.Classes.Permissions;
using Discord.Commands;
namespace NadekoBot.Commands {
class MessageRepeater : IDiscordCommand {
private readonly ConcurrentDictionary<Server, Repeater> repeaters = new ConcurrentDictionary<Server, Repeater>();
private class Repeater {
[Newtonsoft.Json.JsonIgnore]
public readonly Timer MessageTimer;
[Newtonsoft.Json.JsonIgnore]
public Channel RepeatingChannel { get; }
public ulong RepeatingServerId { get; set; }
public ulong RepeatingChannelId { get; set; }
public string RepeatingMessage { get; set; }
public int Interval { get; set; }
private Repeater(int interval) {
this.Interval = interval;
MessageTimer = new Timer {Interval = Interval};
MessageTimer.Elapsed += async (s, e) => {
var ch = RepeatingChannel;
var msg = RepeatingMessage;
if (ch != null && !string.IsNullOrWhiteSpace(msg)) {
try {
await ch.SendMessage(msg);
} catch { }
}
};
}
private Repeater(int interval, ulong channelId, ulong serverId) : this(interval) {
this.RepeatingChannelId = channelId;
this.RepeatingServerId = serverId;
}
public Repeater(int interval, ulong channelId, ulong serverId, Channel channel)
: this(interval,channelId,serverId) {
this.RepeatingChannel = channel;
}
}
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(minutes * 60 * 1000, e.Channel.Id, e.Server.Id, e.Channel)
);
if (!string.IsNullOrWhiteSpace(msg))
repeater.RepeatingMessage = msg;
repeater.MessageTimer.Stop();
repeater.MessageTimer.Start();
await e.Channel.SendMessage(String.Format("👌 Repeating `{0}` every " +
"**{1}** minutes on {2} channel.",
repeater.RepeatingMessage, minutes, repeater.RepeatingChannel));
});
}
}
}

View File

@ -17,8 +17,9 @@ namespace NadekoBot.Modules {
public Administration() {
commands.Add(new ServerGreetCommand());
commands.Add(new LogCommand());
commands.Add(new MessageRepeater());
commands.Add(new PlayingRotate());
commands.Add(new Commands.RatelimitCommand());
commands.Add(new RatelimitCommand());
}
public override string Prefix { get; } = ".";
@ -392,6 +393,13 @@ namespace NadekoBot.Modules {
.Do(async e => {
await e.Channel.SendMessage(await NadekoStats.Instance.GetStats());
});
cgb.CreateCommand(Prefix + "dysyd")
.Description("Shows some basic stats for Nadeko.")
.Do(async e => {
await e.Channel.SendMessage((await NadekoStats.Instance.GetStats()).Matrix().TrimTo(1990));
});
cgb.CreateCommand(Prefix + "heap")
.Description("Shows allocated memory - OWNER ONLY")
.AddCheck(SimpleCheckers.OwnerOnly())

View File

@ -190,6 +190,18 @@ namespace NadekoBot.Modules {
}
});
cgb.CreateCommand("disguise")
.Description("Tell Nadeko to disguise herself.")
.Do(async e => {
try {
await
e.Channel.SendMessage(
$"{NadekoBot.Config.DisguiseResponses[rng.Next(0, NadekoBot.Config.DisguiseResponses.Length)]}");
} catch {
await e.Channel.SendMessage("Error while handling DisguiseResponses check your data/config.json");
}
});
cgb.CreateCommand("are you real")
.Description("Useless.")
.Do(async e => {

View File

@ -121,7 +121,7 @@ namespace NadekoBot {
modules.Add(new Conversations(), "Conversations", ModuleFilter.None);
modules.Add(new Gambling(), "Gambling", ModuleFilter.None);
modules.Add(new Games(), "Games", ModuleFilter.None);
modules.Add(new Music(), "Music", ModuleFilter.None);
//modules.Add(new Music(), "Music", ModuleFilter.None);
modules.Add(new Searches(), "Searches", ModuleFilter.None);
modules.Add(new NSFW(), "NSFW", ModuleFilter.None);
if (!string.IsNullOrWhiteSpace(Creds.TrelloAppKey))

View File

@ -144,6 +144,7 @@
<Compile Include="Commands\ClashOfClans.cs" />
<Compile Include="Commands\LogCommand.cs" />
<Compile Include="Commands\LoLCommands.cs" />
<Compile Include="Commands\MessageRepeater.cs" />
<Compile Include="Commands\PlayingRotate.cs" />
<Compile Include="Commands\TriviaCommand.cs" />
<Compile Include="Classes\Trivia\TriviaGame.cs" />

View File

@ -11,6 +11,12 @@
119174277298782216,
143515953525817344
],
"DisguiseResponses": [
"https://cdn.discordapp.com/attachments/140007341880901632/156721710458994690/Cc5mixjUYAADgBs.jpg",
"https://cdn.discordapp.com/attachments/140007341880901632/156721715831898113/hqdefault.jpg",
"https://cdn.discordapp.com/attachments/140007341880901632/156721724430352385/okawari_01_haruka_weird_mask.jpg",
"https://cdn.discordapp.com/attachments/140007341880901632/156721728763068417/mustache-best-girl.png"
],
"CryResponses": [
"http://i.imgur.com/Xg3i1Qy.gif",
"http://i.imgur.com/3K8DRrU.gif",