From 84a96745c9b35a8e5761f6d0c35d01e311cab66d Mon Sep 17 00:00:00 2001 From: Nikolay Kochulin Date: Sat, 16 Nov 2019 19:45:25 +0000 Subject: [PATCH] Help verb command + change to IChatCommand --- JetKarmaBot/ChatCommandRouter.cs | 10 +++-- JetKarmaBot/Commands/AwardCommand.cs | 2 +- JetKarmaBot/Commands/AwardTypeCommand.cs | 7 +++- .../Commands/AwardTypeManage/TestCommand.cs | 2 +- JetKarmaBot/Commands/ChangeLocaleCommand.cs | 2 +- JetKarmaBot/Commands/CurrenciesCommand.cs | 2 +- JetKarmaBot/Commands/HelpCommand.cs | 7 ++-- JetKarmaBot/Commands/IChatCommand.cs | 2 +- JetKarmaBot/Commands/LeaderboardCommand.cs | 2 +- JetKarmaBot/Commands/StatusCommand.cs | 2 +- JetKarmaBot/ICommandRouter.cs | 12 ++++++ JetKarmaBot/Program.cs | 1 - JetKarmaBot/Services/LogInfo.cs | 9 ----- JetKarmaBot/VerbCommandRouter.cs | 38 +++++++++---------- JetKarmaBot/lang/en-US.json | 1 + 15 files changed, 52 insertions(+), 47 deletions(-) create mode 100644 JetKarmaBot/ICommandRouter.cs diff --git a/JetKarmaBot/ChatCommandRouter.cs b/JetKarmaBot/ChatCommandRouter.cs index c9f2629..5f20d0d 100644 --- a/JetKarmaBot/ChatCommandRouter.cs +++ b/JetKarmaBot/ChatCommandRouter.cs @@ -12,12 +12,14 @@ using Telegram.Bot.Args; namespace JetKarmaBot { - public class ChatCommandRouter + public class ChatCommandRouter : ICommandRouter { public Telegram.Bot.Types.User Me { get; private set; } [Inject] private Logger log; [Inject] private TelegramBotClient Client { get; set; } + public string Prefix => "/"; + public async Task Start() { Me = await Client.GetMeAsync(); @@ -41,7 +43,7 @@ namespace JetKarmaBot if (commands.ContainsKey(cmd.Command)) { log.Debug($"Handling message via {commands[cmd.Command].GetType().Name}"); - return commands[cmd.Command].Execute(cmd, args); + return commands[cmd.Command].Execute(this, cmd, args); } } catch (Exception e) @@ -66,7 +68,7 @@ namespace JetKarmaBot } } - internal string GetHelpText(Locale loc) + public string GetHelpText(Locale loc) { List pieces = new List(); foreach (IChatCommand c in commands.Values.Distinct()) @@ -83,7 +85,7 @@ namespace JetKarmaBot return string.Join("\n", pieces); } - internal string GetHelpTextFor(string commandname, Locale loc) + public string GetHelpTextFor(string commandname, Locale loc) { IChatCommand c = commands[commandname]; string build = ""; diff --git a/JetKarmaBot/Commands/AwardCommand.cs b/JetKarmaBot/Commands/AwardCommand.cs index 9fbbcc5..2e83736 100644 --- a/JetKarmaBot/Commands/AwardCommand.cs +++ b/JetKarmaBot/Commands/AwardCommand.cs @@ -18,7 +18,7 @@ namespace JetKarmaBot.Commands [Inject] private Logger log; - public async Task Execute(CommandString cmd, MessageEventArgs args) + public async Task Execute(ICommandRouter route, CommandString cmd, MessageEventArgs args) { using (var db = Db.GetContext()) { diff --git a/JetKarmaBot/Commands/AwardTypeCommand.cs b/JetKarmaBot/Commands/AwardTypeCommand.cs index 613c79f..63b9522 100644 --- a/JetKarmaBot/Commands/AwardTypeCommand.cs +++ b/JetKarmaBot/Commands/AwardTypeCommand.cs @@ -23,7 +23,9 @@ namespace JetKarmaBot.Commands public AwardTypeCommand(IContainer c, VerbCommandRouter r) { Router = r; + Router.SuperCommand = "at"; r.Add(c.GetInstance()); + r.Add(c.GetInstance()); } public IReadOnlyCollection Arguments => new[] { @@ -36,12 +38,13 @@ namespace JetKarmaBot.Commands } }; - public async Task Execute(CommandString cmd, MessageEventArgs args) + public async Task Execute(ICommandRouter route, CommandString cmd, MessageEventArgs args) { + Router.SuperRouter = route; using (var db = Db.GetContext()) { var currentLocale = Locale[(await db.Chats.FindAsync(args.Message.Chat.Id)).Locale]; - if (!await Router.Process(cmd, args)) + if (!await Router.Process(route, cmd, args)) { await Client.SendTextMessageAsync( args.Message.Chat.Id, diff --git a/JetKarmaBot/Commands/AwardTypeManage/TestCommand.cs b/JetKarmaBot/Commands/AwardTypeManage/TestCommand.cs index 4c0459f..48c14ba 100644 --- a/JetKarmaBot/Commands/AwardTypeManage/TestCommand.cs +++ b/JetKarmaBot/Commands/AwardTypeManage/TestCommand.cs @@ -15,7 +15,7 @@ namespace JetKarmaBot.Commands.AwardTypeManage public IReadOnlyCollection Arguments => Array.Empty(); - public Task Execute(CommandString cmd, MessageEventArgs messageEventArgs) + public Task Execute(ICommandRouter route, CommandString cmd, MessageEventArgs messageEventArgs) { throw new NotImplementedException(); } diff --git a/JetKarmaBot/Commands/ChangeLocaleCommand.cs b/JetKarmaBot/Commands/ChangeLocaleCommand.cs index 99464e6..51ef36b 100644 --- a/JetKarmaBot/Commands/ChangeLocaleCommand.cs +++ b/JetKarmaBot/Commands/ChangeLocaleCommand.cs @@ -15,7 +15,7 @@ namespace JetKarmaBot.Commands [Inject] private Logger log; - public async Task Execute(CommandString cmd, MessageEventArgs args) + public async Task Execute(ICommandRouter route, CommandString cmd, MessageEventArgs args) { using (var db = Db.GetContext()) { diff --git a/JetKarmaBot/Commands/CurrenciesCommand.cs b/JetKarmaBot/Commands/CurrenciesCommand.cs index 07b627f..d3a1228 100644 --- a/JetKarmaBot/Commands/CurrenciesCommand.cs +++ b/JetKarmaBot/Commands/CurrenciesCommand.cs @@ -23,7 +23,7 @@ namespace JetKarmaBot.Commands public IReadOnlyCollection Arguments => new ChatCommandArgument[] { }; - public async Task Execute(CommandString cmd, MessageEventArgs args) + public async Task Execute(ICommandRouter route, CommandString cmd, MessageEventArgs args) { using (var db = Db.GetContext()) { diff --git a/JetKarmaBot/Commands/HelpCommand.cs b/JetKarmaBot/Commands/HelpCommand.cs index 01b2ac8..4999f50 100644 --- a/JetKarmaBot/Commands/HelpCommand.cs +++ b/JetKarmaBot/Commands/HelpCommand.cs @@ -13,7 +13,6 @@ namespace JetKarmaBot.Commands [Inject] KarmaContextFactory Db; [Inject] TelegramBotClient Client { get; set; } [Inject] Localization Locale { get; set; } - [Inject] ChatCommandRouter Router; public IReadOnlyCollection Names => new[] { "help" }; public string Description => "Displays help text for all(one) command(s)"; @@ -29,7 +28,7 @@ namespace JetKarmaBot.Commands } }; - public async Task Execute(CommandString cmd, MessageEventArgs args) + public async Task Execute(ICommandRouter route, CommandString cmd, MessageEventArgs args) { using (var db = Db.GetContext()) { @@ -38,7 +37,7 @@ namespace JetKarmaBot.Commands { await Client.SendTextMessageAsync( args.Message.Chat.Id, - Router.GetHelpText(currentLocale), + route.GetHelpText(currentLocale), replyToMessageId: args.Message.MessageId, parseMode: ParseMode.Html); return true; @@ -47,7 +46,7 @@ namespace JetKarmaBot.Commands { await Client.SendTextMessageAsync( args.Message.Chat.Id, - Router.GetHelpTextFor(cmd.Parameters[0], currentLocale), + route.GetHelpTextFor(cmd.Parameters[0], currentLocale), replyToMessageId: args.Message.MessageId, parseMode: ParseMode.Html); return true; diff --git a/JetKarmaBot/Commands/IChatCommand.cs b/JetKarmaBot/Commands/IChatCommand.cs index 9ebe00c..0d147fa 100644 --- a/JetKarmaBot/Commands/IChatCommand.cs +++ b/JetKarmaBot/Commands/IChatCommand.cs @@ -11,7 +11,7 @@ namespace JetKarmaBot.Commands string DescriptionID { get; } IReadOnlyCollection Arguments { get; } - Task Execute(CommandString cmd, MessageEventArgs messageEventArgs); + Task Execute(ICommandRouter route, CommandString cmd, MessageEventArgs messageEventArgs); } public struct ChatCommandArgument diff --git a/JetKarmaBot/Commands/LeaderboardCommand.cs b/JetKarmaBot/Commands/LeaderboardCommand.cs index 9f7f7de..940b82c 100644 --- a/JetKarmaBot/Commands/LeaderboardCommand.cs +++ b/JetKarmaBot/Commands/LeaderboardCommand.cs @@ -17,7 +17,7 @@ namespace JetKarmaBot.Commands { public IReadOnlyCollection Names => new[] { "leaderboard" }; - public async Task Execute(CommandString cmd, MessageEventArgs args) + public async Task Execute(ICommandRouter route, CommandString cmd, MessageEventArgs args) { using (var db = Db.GetContext()) { diff --git a/JetKarmaBot/Commands/StatusCommand.cs b/JetKarmaBot/Commands/StatusCommand.cs index 60232ca..2ecc55c 100644 --- a/JetKarmaBot/Commands/StatusCommand.cs +++ b/JetKarmaBot/Commands/StatusCommand.cs @@ -17,7 +17,7 @@ namespace JetKarmaBot.Commands { public IReadOnlyCollection Names => new[] { "status" }; - public async Task Execute(CommandString cmd, MessageEventArgs args) + public async Task Execute(ICommandRouter route, CommandString cmd, MessageEventArgs args) { using (var db = Db.GetContext()) { diff --git a/JetKarmaBot/ICommandRouter.cs b/JetKarmaBot/ICommandRouter.cs new file mode 100644 index 0000000..956df8e --- /dev/null +++ b/JetKarmaBot/ICommandRouter.cs @@ -0,0 +1,12 @@ +using JetKarmaBot.Commands; + +namespace JetKarmaBot +{ + public interface ICommandRouter + { + string GetHelpText(Locale loc); + string GetHelpTextFor(string commandname, Locale loc); + void Add(IChatCommand c); + string Prefix { get; } + } +} \ No newline at end of file diff --git a/JetKarmaBot/Program.cs b/JetKarmaBot/Program.cs index 1bfec8e..ed77599 100644 --- a/JetKarmaBot/Program.cs +++ b/JetKarmaBot/Program.cs @@ -29,7 +29,6 @@ namespace JetKarmaBot var dbOptions = new DbContextOptionsBuilder() .UseMySql(cfg.ConnectionString + (cfg.ConnectionString.EndsWith(";") ? "" : ";") + "TreatTinyAsBoolean=false"); c.AddInfo(new LogInfo()); - c.AddInfo(new VerbInfo()); if (cfg.SqlDebug) { dbOptions = dbOptions.UseLoggerFactory(c.GetInstance()); diff --git a/JetKarmaBot/Services/LogInfo.cs b/JetKarmaBot/Services/LogInfo.cs index 3afd0a1..6a55117 100644 --- a/JetKarmaBot/Services/LogInfo.cs +++ b/JetKarmaBot/Services/LogInfo.cs @@ -19,13 +19,4 @@ namespace JetKarmaBot + (t.GenericTypeArguments.Length > 0 ? "<" + string.Join(",", t.GenericTypeArguments.Select(getTypeName)) + ">" : ""); } } - public class VerbInfo : ObjectInfo - { - public override ObjectInfo Clone() => new VerbInfo(); - - public override object GetInstance(IContainer c, Type requester = null) - { - return c.ResolveObject(new VerbCommandRouter(requester != null ? requester.Name : "")); - } - } } \ No newline at end of file diff --git a/JetKarmaBot/VerbCommandRouter.cs b/JetKarmaBot/VerbCommandRouter.cs index 7687c4c..425991f 100644 --- a/JetKarmaBot/VerbCommandRouter.cs +++ b/JetKarmaBot/VerbCommandRouter.cs @@ -9,22 +9,20 @@ using Telegram.Bot.Args; namespace JetKarmaBot { - public class VerbCommandRouter + public class VerbCommandRouter : ICommandRouter { Dictionary commands = new Dictionary(); [Inject] private Logger log; - string superCommand; - public VerbCommandRouter(string supercommand) - { - superCommand = supercommand; - } + public string SuperCommand { get; set; } + public string Prefix => SuperRouter.Prefix + SuperCommand + " "; + public ICommandRouter SuperRouter { get; set; } - public Task Process(CommandString cs, MessageEventArgs args) + public Task Process(ICommandRouter route, CommandString cs, MessageEventArgs args) { - log.Debug($"(verb for {superCommand}) Processing verb"); + log.Debug($"(verb for {SuperCommand}) Processing verb"); if (cs.Parameters.Length < 1) { - log.Debug($"(verb for {superCommand}) too few arguments"); + log.Debug($"(verb for {SuperCommand}) too few arguments"); return Task.FromResult(false); } CommandString ncs = new CommandString(cs.Parameters[0], cs.Parameters.Skip(1).ToArray()); @@ -32,13 +30,13 @@ namespace JetKarmaBot { if (commands.ContainsKey(ncs.Command)) { - log.Debug($"(verb for {superCommand}) Handling via {commands[ncs.Command].GetType().Name}"); - return commands[ncs.Command].Execute(ncs, args); + log.Debug($"(verb for {SuperCommand}) Handling via {commands[ncs.Command].GetType().Name}"); + return commands[ncs.Command].Execute(this, ncs, args); } } catch (Exception e) { - log.Error($"(verb for {superCommand}) Error while handling verb {ncs.Command}!"); + log.Error($"(verb for {SuperCommand}) Error while handling verb {ncs.Command}!"); log.Error(e); return Task.FromResult(true); //Don't trigger message } @@ -48,17 +46,17 @@ namespace JetKarmaBot public void Add(IChatCommand c) { - log.ConditionalTrace($"(verb for {superCommand}) Adding command {c.GetType().Name}"); + log.ConditionalTrace($"(verb for {SuperCommand}) Adding command {c.GetType().Name}"); foreach (var name in c.Names) { - log.ConditionalTrace($"(verb for {superCommand}) Mounting {c.GetType().Name} to {name}"); + log.ConditionalTrace($"(verb for {SuperCommand}) Mounting {c.GetType().Name} to {name}"); if (commands.ContainsKey(name)) throw new Exception($"command collision for name {name}, commands {commands[name].GetType()} and {c.GetType()}"); commands[name] = c; } } - internal string GetHelpText(Locale loc) + public string GetHelpText(Locale loc) { List pieces = new List(); foreach (IChatCommand c in commands.Values.Distinct()) @@ -67,24 +65,24 @@ namespace JetKarmaBot List names = c.Names.ToList(); for (int i = 0; i < names.Count - 1; i++) { - build = build + "/" + names[i] + "\n"; + build = build + Prefix + names[i] + "\n"; } - build += "/" + names[names.Count - 1] + " " + string.Join(" ", c.Arguments.Select(x => (!x.Required ? "[" : "") + x.Name + (!x.Required ? "]" : ""))) + " " + getLocalizedCMDDesc(c, loc) + ""; + build += Prefix + names[names.Count - 1] + " " + string.Join(" ", c.Arguments.Select(x => (!x.Required ? "[" : "") + x.Name + (!x.Required ? "]" : ""))) + " " + getLocalizedCMDDesc(c, loc) + ""; pieces.Add(build); } return string.Join("\n", pieces); } - internal string GetHelpTextFor(string commandname, Locale loc) + public string GetHelpTextFor(string commandname, Locale loc) { IChatCommand c = commands[commandname]; string build = ""; List names = c.Names.ToList(); for (int i = 0; i < names.Count - 1; i++) { - build = build + "/" + names[i] + "\n"; + build = build + Prefix + names[i] + "\n"; } - build += "/" + names[names.Count - 1] + " " + string.Join(" ", c.Arguments.Select(x => (!x.Required ? "[" : "") + x.Name + (!x.Required ? "]" : ""))) + " " + getLocalizedCMDDesc(c, loc) + "\n"; + build += Prefix + names[names.Count - 1] + " " + string.Join(" ", c.Arguments.Select(x => (!x.Required ? "[" : "") + x.Name + (!x.Required ? "]" : ""))) + " " + getLocalizedCMDDesc(c, loc) + "\n"; build += string.Join("\n", c.Arguments.Select(ca => (!ca.Required ? "[" : "") + ca.Name + (!ca.Required ? "]" : "") + ": " + getLocalizedCMDArgDesc(ca, loc) + "")); return build; } diff --git a/JetKarmaBot/lang/en-US.json b/JetKarmaBot/lang/en-US.json index efd3014..38d9270 100644 --- a/JetKarmaBot/lang/en-US.json +++ b/JetKarmaBot/lang/en-US.json @@ -40,6 +40,7 @@ "jetkarmabot.leaderboard.specifictext": "Leaderboard for {0}:", "jetkarmabot.at.help": "Manages custom award types.", "jetkarmabot.at.verbhelp": "The action to perform.", + "jetkarmabot.verbhelp": "Displays help text for all(one) verb(s)", "jetkarmabot.star.nominative": "star", "jetkarmabot.star.accusative": "star" }