From 83d1b458af725e47d220030bd5cdc24ddb369089 Mon Sep 17 00:00:00 2001 From: Nikolay Kochulin Date: Thu, 7 Feb 2019 16:44:32 +0300 Subject: [PATCH] Add help command mechanisms --- JetKarmaBot/CommandRouter.cs | 34 +++++++++++++++++- JetKarmaBot/Commands/AwardCommand.cs | 11 ++++++ JetKarmaBot/Commands/ChangeLocaleCommand.cs | 11 ++++++ JetKarmaBot/Commands/HelpCommand.cs | 40 ++++++++++++++++++--- JetKarmaBot/Commands/IChatCommand.cs | 17 +++++++++ JetKarmaBot/Commands/StatusCommand.cs | 10 ++++++ JetKarmaBot/JetKarmaBot.cs | 2 +- 7 files changed, 118 insertions(+), 7 deletions(-) diff --git a/JetKarmaBot/CommandRouter.cs b/JetKarmaBot/CommandRouter.cs index 53edf69..69e5499 100644 --- a/JetKarmaBot/CommandRouter.cs +++ b/JetKarmaBot/CommandRouter.cs @@ -9,7 +9,7 @@ using Telegram.Bot.Types; namespace JetKarmaBot { - class ChatCommandRouter + public class ChatCommandRouter { User BotUser { get; } [Inject] @@ -52,6 +52,7 @@ namespace JetKarmaBot return false; } + public void Add(IChatCommand c) { log.ConditionalTrace($"Adding command {c.GetType().Name}"); @@ -64,6 +65,37 @@ namespace JetKarmaBot } } + public string GetHelpText() + { + List pieces = new List(); + foreach (IChatCommand c in commands.Values.Distinct()) + { + string build = ""; + List names = c.Names.ToList(); + for (int i = 0; i < names.Count - 1; i++) + { + build = build + "/" + names[i] + "\n"; + } + build += "/" + names[names.Count - 1] + " " + string.Join(' ', c.Arguments.Select(x => (!x.Required ? "[" : "") + x.Name + (!x.Required ? "]" : ""))) + " " + c.Description + ""; + pieces.Add(build); + } + return string.Join('\n', pieces); + } + + internal string GetHelpTextFor(string commandname) + { + 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 += "/" + names[names.Count - 1] + " " + string.Join(' ', c.Arguments.Select(x => (!x.Required ? "[" : "") + x.Name + (!x.Required ? "]" : ""))) + " " + c.Description + "\n"; + build += string.Join("\n", c.Arguments.Select(ca => (!ca.Required ? "[" : "") + ca.Name + (!ca.Required ? "]" : "") + ": " + ca.Description + "")); + return build; + } + Dictionary commands = new Dictionary(); } } diff --git a/JetKarmaBot/Commands/AwardCommand.cs b/JetKarmaBot/Commands/AwardCommand.cs index 6d07a51..00bf025 100644 --- a/JetKarmaBot/Commands/AwardCommand.cs +++ b/JetKarmaBot/Commands/AwardCommand.cs @@ -92,6 +92,17 @@ namespace JetKarmaBot.Commands [Inject] Localization Locale { get; set; } User Me { get; } + public string Description => "Awards/revokes an award to a user."; + + public IReadOnlyCollection Arguments => new ChatCommandArgument[] { + new ChatCommandArgument() { + Name="awardtype", + Required=false, + Type=ChatCommandArgumentType.String, + Description="The award to grant to/strip of the specified user" + } + }; + public AwardCommand(User me) { Me = me; diff --git a/JetKarmaBot/Commands/ChangeLocaleCommand.cs b/JetKarmaBot/Commands/ChangeLocaleCommand.cs index a60455e..99f9433 100644 --- a/JetKarmaBot/Commands/ChangeLocaleCommand.cs +++ b/JetKarmaBot/Commands/ChangeLocaleCommand.cs @@ -67,5 +67,16 @@ namespace JetKarmaBot.Commands [Inject] KarmaContextFactory Db { get; set; } [Inject] TelegramBotClient Client { get; set; } [Inject] Localization Locale { get; set; } + + public string Description => "Switches current chat locale to [locale]"; + + public IReadOnlyCollection Arguments => new ChatCommandArgument[] { + new ChatCommandArgument() { + Name="locale", + Required=false, + Type=ChatCommandArgumentType.String, + Description="The locale to switch to. Can be \"list\" to list all possible locales. Also can be empty to get current locale" + } + }; } } diff --git a/JetKarmaBot/Commands/HelpCommand.cs b/JetKarmaBot/Commands/HelpCommand.cs index 02eda2c..4f2bd8d 100644 --- a/JetKarmaBot/Commands/HelpCommand.cs +++ b/JetKarmaBot/Commands/HelpCommand.cs @@ -3,6 +3,7 @@ using Telegram.Bot.Args; using Perfusion; using JetKarmaBot.Services; using Telegram.Bot; +using Telegram.Bot.Types.Enums; namespace JetKarmaBot.Commands { @@ -11,19 +12,48 @@ namespace JetKarmaBot.Commands [Inject] KarmaContextFactory Db; [Inject] TelegramBotClient Client { get; set; } [Inject] Localization Locale { get; set; } + ChatCommandRouter Router; public IReadOnlyCollection Names => new[] { "help" }; + public string Description => "Displays help text for all(one) command(s)"; + + public IReadOnlyCollection Arguments => new ChatCommandArgument[] { + new ChatCommandArgument() { + Name="command", + Required=false, + Type=ChatCommandArgumentType.String, + Description="The command to return help text for. If empty shows all commands" + } + }; + public bool Execute(CommandString cmd, MessageEventArgs args) { using (var db = Db.GetContext()) { var currentLocale = Locale[db.Chats.Find(args.Message.Chat.Id).Locale]; - Client.SendTextMessageAsync( - args.Message.Chat.Id, - currentLocale["jetkarmabot.help"], - replyToMessageId: args.Message.MessageId); - return true; + if (cmd.Parameters.Length < 1) + { + Client.SendTextMessageAsync( + args.Message.Chat.Id, + Router.GetHelpText(), + replyToMessageId: args.Message.MessageId, + parseMode: ParseMode.Html); + return true; + } + else + { + Client.SendTextMessageAsync( + args.Message.Chat.Id, + Router.GetHelpTextFor(cmd.Parameters[0]), + replyToMessageId: args.Message.MessageId, + parseMode: ParseMode.Html); + return true; + } } } + public HelpCommand(ChatCommandRouter router) + { + Router = router; + } } } diff --git a/JetKarmaBot/Commands/IChatCommand.cs b/JetKarmaBot/Commands/IChatCommand.cs index ebfacc0..69fd0b3 100644 --- a/JetKarmaBot/Commands/IChatCommand.cs +++ b/JetKarmaBot/Commands/IChatCommand.cs @@ -6,7 +6,24 @@ namespace JetKarmaBot.Commands public interface IChatCommand { IReadOnlyCollection Names { get; } + string Description { get; } + IReadOnlyCollection Arguments { get; } + bool Execute(CommandString cmd, MessageEventArgs messageEventArgs); } + public struct ChatCommandArgument + { + public string Name; + public bool Required; + public ChatCommandArgumentType Type; + public string Description; + } + + public enum ChatCommandArgumentType + { + Boolean, + String, + Integer, + } } diff --git a/JetKarmaBot/Commands/StatusCommand.cs b/JetKarmaBot/Commands/StatusCommand.cs index c095595..6039fa8 100644 --- a/JetKarmaBot/Commands/StatusCommand.cs +++ b/JetKarmaBot/Commands/StatusCommand.cs @@ -67,5 +67,15 @@ namespace JetKarmaBot.Commands [Inject] TelegramBotClient Client { get; set; } [Inject] Localization Locale { get; set; } + public string Description => "Shows the amount of awards that you have"; + + public IReadOnlyCollection Arguments => new ChatCommandArgument[] { + new ChatCommandArgument(){ + Name="awardtype", + Required=false, + Type=ChatCommandArgumentType.String, + Description="The awardtype to show. If empty shows everything." + } + }; } } diff --git a/JetKarmaBot/JetKarmaBot.cs b/JetKarmaBot/JetKarmaBot.cs index 124f7fe..044b0a9 100644 --- a/JetKarmaBot/JetKarmaBot.cs +++ b/JetKarmaBot/JetKarmaBot.cs @@ -73,7 +73,7 @@ namespace JetKarmaBot void InitCommands(Container c) { Commands = c.ResolveObject(new ChatCommandRouter(Me)); - Commands.Add(c.ResolveObject(new HelpCommand())); + Commands.Add(c.ResolveObject(new HelpCommand(Commands))); Commands.Add(c.ResolveObject(new AwardCommand(Me))); Commands.Add(c.ResolveObject(new StatusCommand())); Commands.Add(c.ResolveObject(new LocaleCommand()));