fixes to customreactions

This commit is contained in:
Master Kwoth 2016-05-05 17:15:53 +02:00
parent 2a63e5bbda
commit 0022c7cb8d
2 changed files with 72 additions and 74 deletions

View File

@ -1,12 +1,10 @@
using NadekoBot.Classes; using Discord;
using System; using Discord.Commands;
using System.Collections.Generic; using NadekoBot.Classes;
using NadekoBot.Modules.Permissions.Classes;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Commands;
using NadekoBot.Modules.Permissions.Classes;
using Discord;
namespace NadekoBot.Modules.Administration.Commands namespace NadekoBot.Modules.Administration.Commands
{ {
@ -30,37 +28,30 @@ namespace NadekoBot.Modules.Administration.Commands
.Do(async e => .Do(async e =>
{ {
var name = e.GetArg("name"); var name = e.GetArg("name");
var message = e.GetArg("message").Trim(); var message = e.GetArg("message")?.Trim();
if (string.IsNullOrWhiteSpace(message)) if (string.IsNullOrWhiteSpace(message))
{ {
await e.Channel.SendMessage($"Incorrect command usage. See -h {Prefix}acr for correct formatting").ConfigureAwait(false); await e.Channel.SendMessage($"Incorrect command usage. See -h {Prefix}acr for correct formatting").ConfigureAwait(false);
return; return;
} }
try if (NadekoBot.Config.CustomReactions.ContainsKey(name))
{
NadekoBot.Config.CustomReactions[name].Add(message); NadekoBot.Config.CustomReactions[name].Add(message);
} else
catch (KeyNotFoundException)
{
NadekoBot.Config.CustomReactions.Add(name, new System.Collections.Generic.List<string>() { message }); NadekoBot.Config.CustomReactions.Add(name, new System.Collections.Generic.List<string>() { message });
} await Task.Run(() => Classes.JSONModels.ConfigHandler.SaveConfig());
finally
{
Classes.JSONModels.ConfigHandler.SaveConfig();
}
await e.Channel.SendMessage($"Added {name} : {message}").ConfigureAwait(false); await e.Channel.SendMessage($"Added {name} : {message}").ConfigureAwait(false);
}); });
cgb.CreateCommand(Prefix + "listcustomreactions") cgb.CreateCommand(Prefix + "listcustomreactions")
.Alias(Prefix + "lcr") .Alias(Prefix + "lcr")
.Description("Lists all current custom reactions (paginated with 5 commands per page).\n**Usage**:.lcr 1") .Description($"Lists all current custom reactions (paginated with 5 commands per page).\n**Usage**:{Prefix}lcr 1")
.Parameter("num", ParameterType.Required) .Parameter("num", ParameterType.Required)
.Do(async e => .Do(async e =>
{ {
int num; int num;
if (!int.TryParse(e.GetArg("num"), out num)) return; if (!int.TryParse(e.GetArg("num"), out num) || num <= 0) return;
string result = getCustomsOnPage(num -1); //People prefer starting with 1 string result = GetCustomsOnPage(num - 1); //People prefer starting with 1
await e.Channel.SendMessage(result); await e.Channel.SendMessage(result);
}); });
@ -71,7 +62,9 @@ namespace NadekoBot.Modules.Administration.Commands
.Parameter("index", ParameterType.Optional) .Parameter("index", ParameterType.Optional)
.Do(async e => .Do(async e =>
{ {
var name = e.GetArg("name"); var name = e.GetArg("name")?.Trim();
if (string.IsNullOrWhiteSpace(name))
return;
if (!NadekoBot.Config.CustomReactions.ContainsKey(name)) if (!NadekoBot.Config.CustomReactions.ContainsKey(name))
{ {
await e.Channel.SendMessage("Could not find given commandname"); await e.Channel.SendMessage("Could not find given commandname");
@ -79,53 +72,61 @@ namespace NadekoBot.Modules.Administration.Commands
} }
string message = ""; string message = "";
int index; int index;
if (int.TryParse(e.GetArg("index") ?? "", out index)) if (int.TryParse(e.GetArg("index")?.Trim() ?? "", out index))
{ {
try index = index - 1;
if (index < 0 || index > NadekoBot.Config.CustomReactions[name].Count)
{ {
NadekoBot.Config.CustomReactions[name].RemoveAt(index - 1); await e.Channel.SendMessage("Given index was out of range").ConfigureAwait(false);
return;
}
NadekoBot.Config.CustomReactions[name].RemoveAt(index);
if (!NadekoBot.Config.CustomReactions[name].Any()) if (!NadekoBot.Config.CustomReactions[name].Any())
{ {
NadekoBot.Config.CustomReactions.Remove(name); NadekoBot.Config.CustomReactions.Remove(name);
} }
message = $"Deleted response #{index} from {name}"; message = $"Deleted response #{index} from `{name}`";
}
catch (ArgumentOutOfRangeException)
{
await e.Channel.SendMessage("Index given was out of range").ConfigureAwait(false);
return;
}
} }
else else
{ {
NadekoBot.Config.CustomReactions.Remove(name); NadekoBot.Config.CustomReactions.Remove(name);
message = $"Deleted custom reaction \"{name}\""; message = $"Deleted custom reaction: `{name}`";
} }
Classes.JSONModels.ConfigHandler.SaveConfig(); await Task.Run(() => Classes.JSONModels.ConfigHandler.SaveConfig());
await e.Channel.SendMessage(message); await e.Channel.SendMessage(message);
}); });
} }
private readonly int ItemsPerPage = 5; private readonly int ItemsPerPage = 5;
private string getCustomsOnPage(int page) private string GetCustomsOnPage(int page)
{ {
var items = NadekoBot.Config.CustomReactions.Skip(page * ItemsPerPage).Take(ItemsPerPage); var items = NadekoBot.Config.CustomReactions.Skip(page * ItemsPerPage).Take(ItemsPerPage);
if(!items.Any()) if (!items.Any())
{ {
return $"No items on page {page}."; return $"No items on page {page + 1}.";
} }
string message = $"Custom reactions of page {page + 1}:"; var message = new StringBuilder($"```js\n --- Custom reactions - page {page + 1} ---\n");
foreach (var cr in items) foreach (var cr in items)
{ {
message += $"\n**\"{Format.Escape(cr.Key)}\"**:"; message.Append($"\"{ Format.Escape(cr.Key)}\"\n");
int i = 1; int i = 1;
var last = cr.Value.Last();
foreach (var reaction in cr.Value) foreach (var reaction in cr.Value)
{ {
message += "\n " + i++ + "." + Format.Code(reaction); if (last != reaction)
message.AppendLine(" ├" + i++ + "─" + reaction);
else
message.AppendLine(" └" + i++ + "─" + reaction);
} }
} }
return message; return message.ToString() + "\n```";
} }
} }
} }
// zeta is a god
//├
//─
//│
//└

View File

@ -1,11 +1,10 @@
using Discord.Commands;
using Discord.Modules;
using NadekoBot.Extensions;
using NadekoBot.Modules.Permissions.Classes;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Discord.Modules;
using Discord.Commands;
using NadekoBot.Modules.Permissions.Classes;
using NadekoBot.Extensions;
using System.Text.RegularExpressions;
namespace NadekoBot.Modules.CustomReactions namespace NadekoBot.Modules.CustomReactions
{ {
@ -29,16 +28,14 @@ namespace NadekoBot.Modules.CustomReactions
{"%target%", e => e.GetArg("args")?.Trim() ?? "" }, {"%target%", e => e.GetArg("args")?.Trim() ?? "" },
}; };
foreach (var command in NadekoBot.Config.CustomReactions) foreach (var command in NadekoBot.Config.CustomReactions)
{ {
var commandName = command.Key.Replace("%mention%", NadekoBot.BotMention); var commandName = command.Key.Replace("%mention%", NadekoBot.BotMention);
var c = cgb.CreateCommand(commandName); cgb.CreateCommand(commandName)
c.Description($"Custom reaction.\n**Usage**:{command.Key}"); .Description($"Custom reaction.\n**Usage**:{command.Key}")
c.Parameter("args", ParameterType.Unparsed); .Parameter("args", ParameterType.Unparsed)
c.Do(async e => .Do(async e =>
{ {
string str = command.Value[range.Next(0, command.Value.Count())]; string str = command.Value[range.Next(0, command.Value.Count())];
commandFuncs.Keys.ForEach(k => str = str.Replace(k, commandFuncs[k](e))); commandFuncs.Keys.ForEach(k => str = str.Replace(k, commandFuncs[k](e)));