Merge help-command into master

This commit is contained in:
Nikolay Kochulin 2019-02-07 17:53:52 +03:00
commit 6835ba4843
11 changed files with 187 additions and 27 deletions

View File

@ -9,7 +9,7 @@ using Telegram.Bot.Types;
namespace JetKarmaBot namespace JetKarmaBot
{ {
class ChatCommandRouter public class ChatCommandRouter
{ {
User BotUser { get; } User BotUser { get; }
[Inject] [Inject]
@ -52,6 +52,7 @@ namespace JetKarmaBot
return false; return false;
} }
public void Add(IChatCommand c) public void Add(IChatCommand c)
{ {
log.ConditionalTrace($"Adding command {c.GetType().Name}"); log.ConditionalTrace($"Adding command {c.GetType().Name}");
@ -64,6 +65,48 @@ namespace JetKarmaBot
} }
} }
internal string GetHelpText(Locale loc)
{
List<string> pieces = new List<string>();
foreach (IChatCommand c in commands.Values.Distinct())
{
string build = "";
List<string> 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 ? "]" : ""))) + " <i>" + getLocalizedCMDDesc(c, loc) + "</i>";
pieces.Add(build);
}
return string.Join('\n', pieces);
}
internal string GetHelpTextFor(string commandname, Locale loc)
{
IChatCommand c = commands[commandname];
string build = "";
List<string> 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 ? "]" : ""))) + " <i>" + getLocalizedCMDDesc(c, loc) + "</i>\n";
build += string.Join("\n", c.Arguments.Select(ca => (!ca.Required ? "[" : "") + ca.Name + (!ca.Required ? "]" : "") + ": <i>" + getLocalizedCMDArgDesc(ca, loc) + "</i>"));
return build;
}
private string getLocalizedCMDDesc(IChatCommand cmd, Locale loc)
{
if (loc.ContainsKey(cmd.DescriptionID)) return loc[cmd.DescriptionID];
else return cmd.Description;
}
private string getLocalizedCMDArgDesc(ChatCommandArgument arg, Locale loc)
{
if (loc.ContainsKey(arg.DescriptionID)) return loc[arg.DescriptionID];
else return arg.Description;
}
Dictionary<string, IChatCommand> commands = new Dictionary<string, IChatCommand>(); Dictionary<string, IChatCommand> commands = new Dictionary<string, IChatCommand>();
} }
} }

View File

@ -104,6 +104,19 @@ namespace JetKarmaBot.Commands
[Inject] Localization Locale { get; set; } [Inject] Localization Locale { get; set; }
User Me { get; } User Me { get; }
public string Description => "Awards/revokes an award to a user.";
public string DescriptionID => "jetkarmabot.award.help";
public IReadOnlyCollection<ChatCommandArgument> Arguments => new ChatCommandArgument[] {
new ChatCommandArgument() {
Name="awardtype",
Required=false,
Type=ChatCommandArgumentType.String,
Description="The award to grant to/strip of the specified user",
DescriptionID="jetkarmabot.award.awardtypehelp"
}
};
public AwardCommand(User me) public AwardCommand(User me)
{ {
Me = me; Me = me;

View File

@ -67,5 +67,18 @@ namespace JetKarmaBot.Commands
[Inject] KarmaContextFactory Db { get; set; } [Inject] KarmaContextFactory Db { get; set; }
[Inject] TelegramBotClient Client { get; set; } [Inject] TelegramBotClient Client { get; set; }
[Inject] Localization Locale { get; set; } [Inject] Localization Locale { get; set; }
public string Description => "Switches current chat locale to [locale]";
public string DescriptionID => "jetkarmabot.changelocale.help";
public IReadOnlyCollection<ChatCommandArgument> 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.",
DescriptionID="jetkarmabot.changelocale.localehelp"
}
};
} }
} }

View File

@ -0,0 +1,61 @@
using System.Collections.Generic;
using Telegram.Bot.Args;
using Perfusion;
using JetKarmaBot.Services;
using Telegram.Bot;
using Telegram.Bot.Types.Enums;
namespace JetKarmaBot.Commands
{
public class HelpCommand : IChatCommand
{
[Inject] KarmaContextFactory Db;
[Inject] TelegramBotClient Client { get; set; }
[Inject] Localization Locale { get; set; }
ChatCommandRouter Router;
public IReadOnlyCollection<string> Names => new[] { "help" };
public string Description => "Displays help text for all(one) command(s)";
public string DescriptionID => "jetkarmabot.help.help";
public IReadOnlyCollection<ChatCommandArgument> 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.",
DescriptionID="jetkarmabot.help.commandhelp"
}
};
public bool Execute(CommandString cmd, MessageEventArgs args)
{
using (var db = Db.GetContext())
{
var currentLocale = Locale[db.Chats.Find(args.Message.Chat.Id).Locale];
if (cmd.Parameters.Length < 1)
{
Client.SendTextMessageAsync(
args.Message.Chat.Id,
Router.GetHelpText(currentLocale),
replyToMessageId: args.Message.MessageId,
parseMode: ParseMode.Html);
return true;
}
else
{
Client.SendTextMessageAsync(
args.Message.Chat.Id,
Router.GetHelpTextFor(cmd.Parameters[0], currentLocale),
replyToMessageId: args.Message.MessageId,
parseMode: ParseMode.Html);
return true;
}
}
}
public HelpCommand(ChatCommandRouter router)
{
Router = router;
}
}
}

View File

@ -6,7 +6,26 @@ namespace JetKarmaBot.Commands
public interface IChatCommand public interface IChatCommand
{ {
IReadOnlyCollection<string> Names { get; } IReadOnlyCollection<string> Names { get; }
string Description { get; }
string DescriptionID { get; }
IReadOnlyCollection<ChatCommandArgument> Arguments { get; }
bool Execute(CommandString cmd, MessageEventArgs messageEventArgs); bool Execute(CommandString cmd, MessageEventArgs messageEventArgs);
} }
public struct ChatCommandArgument
{
public string Name;
public bool Required;
public ChatCommandArgumentType Type;
public string Description;
public string DescriptionID;
}
public enum ChatCommandArgumentType
{
Boolean,
String,
Integer,
}
} }

View File

@ -1,25 +0,0 @@
using System.Collections.Generic;
using Telegram.Bot.Args;
using Perfusion;
using JetKarmaBot.Services;
namespace JetKarmaBot.Commands
{
public class StartCommand : IChatCommand
{
[Inject] KarmaContextFactory Db;
public IReadOnlyCollection<string> Names => new[] { "start" };
public bool Execute(CommandString cmd, MessageEventArgs args)
{
using (var db = Db.GetContext())
{
db.Chats.Add(new Models.Chat { ChatId = args.Message.Chat.Id });
db.Users.Add(new Models.User { UserId = args.Message.From.Id });
db.SaveChanges();
return true;
}
}
}
}

View File

@ -67,5 +67,17 @@ namespace JetKarmaBot.Commands
[Inject] TelegramBotClient Client { get; set; } [Inject] TelegramBotClient Client { get; set; }
[Inject] Localization Locale { get; set; } [Inject] Localization Locale { get; set; }
public string Description => "Shows the amount of awards that you have";
public string DescriptionID => "jetkarmabot.status.help";
public IReadOnlyCollection<ChatCommandArgument> Arguments => new ChatCommandArgument[] {
new ChatCommandArgument(){
Name="awardtype",
Required=false,
Type=ChatCommandArgumentType.String,
Description="The awardtype to show. If empty shows everything.",
DescriptionID= "jetkarmabot.status.awardtypehelp"
}
};
} }
} }

View File

@ -73,7 +73,7 @@ namespace JetKarmaBot
void InitCommands(Container c) void InitCommands(Container c)
{ {
Commands = c.ResolveObject(new ChatCommandRouter(Me)); Commands = c.ResolveObject(new ChatCommandRouter(Me));
Commands.Add(c.ResolveObject(new StartCommand())); Commands.Add(c.ResolveObject(new HelpCommand(Commands)));
Commands.Add(c.ResolveObject(new AwardCommand(Me))); Commands.Add(c.ResolveObject(new AwardCommand(Me)));
Commands.Add(c.ResolveObject(new StatusCommand())); Commands.Add(c.ResolveObject(new StatusCommand()));
Commands.Add(c.ResolveObject(new LocaleCommand())); Commands.Add(c.ResolveObject(new LocaleCommand()));

View File

@ -13,14 +13,22 @@
"jetkarmabot.award.awardmessage": "Ўручыў \"{0}\" {1}!", "jetkarmabot.award.awardmessage": "Ўручыў \"{0}\" {1}!",
"jetkarmabot.award.revokemessage": "Адабраў \"{0}\" у {1}!", "jetkarmabot.award.revokemessage": "Адабраў \"{0}\" у {1}!",
"jetkarmabot.award.statustext": "У {0} цяпер {1}{2}.", "jetkarmabot.award.statustext": "У {0} цяпер {1}{2}.",
"jetkarmabot.award.help": "",
"jetkarmabot.award.awardtypehelp": "",
"jetkarmabot.status.listalltext": "У вас :", "jetkarmabot.status.listalltext": "У вас :",
"jetkarmabot.status.listspecifictext": "У вас зараз {0}{1}.", "jetkarmabot.status.listspecifictext": "У вас зараз {0}{1}.",
"jetkarmabot.status.havenothing": "У вас пакуль нічога няма.", "jetkarmabot.status.havenothing": "У вас пакуль нічога няма.",
"jetkarmabot.status.help": "",
"jetkarmabot.status.awardtypehelp": "",
"jetkarmabot.changelocale.justchanged": "Так дакладна.", "jetkarmabot.changelocale.justchanged": "Так дакладна.",
"jetkarmabot.changelocale.getlocale": "Я зараз кажу па-беларускай.", "jetkarmabot.changelocale.getlocale": "Я зараз кажу па-беларускай.",
"jetkarmabot.changelocale.listalltext": "Я ведаю:", "jetkarmabot.changelocale.listalltext": "Я ведаю:",
"jetkarmabot.changelocale.errorall": "Мне б хацелася гаварыць на ўсіх мовах у той жа самы час, але з-за абмежаванняў сусвету, мне гэта не дазваляецца.", "jetkarmabot.changelocale.errorall": "Мне б хацелася гаварыць на ўсіх мовах у той жа самы час, але з-за абмежаванняў сусвету, мне гэта не дазваляецца.",
"jetkarmabot.changelocale.beforenote": "Увага: ", "jetkarmabot.changelocale.beforenote": "Увага: ",
"jetkarmabot.changelocale.help": "",
"jetkarmabot.changelocale.localehelp": "",
"jetkarmabot.help.help": "",
"jetkarmabot.help.commandhelp": "",
"jetkarmabot.awardtypes.star": "Зорачка", "jetkarmabot.awardtypes.star": "Зорачка",
"jetkarmabot.awardtypes.pie": "з паліцы піражок", "jetkarmabot.awardtypes.pie": "з паліцы піражок",
"jetkarmabot.awardtypes.dream": "Мара", "jetkarmabot.awardtypes.dream": "Мара",

View File

@ -12,14 +12,22 @@
"jetkarmabot.award.awardmessage": "Awarded a {0} to {1}!", "jetkarmabot.award.awardmessage": "Awarded a {0} to {1}!",
"jetkarmabot.award.revokemessage": "Revoked a {0} from {1}!", "jetkarmabot.award.revokemessage": "Revoked a {0} from {1}!",
"jetkarmabot.award.statustext": "{0} is at {1}{2} now.", "jetkarmabot.award.statustext": "{0} is at {1}{2} now.",
"jetkarmabot.award.help": "Awards/revokes an award to a user.",
"jetkarmabot.award.awardtypehelp": "The award to grant to/strip of the specified user",
"jetkarmabot.status.listalltext": "Your badges report:", "jetkarmabot.status.listalltext": "Your badges report:",
"jetkarmabot.status.listspecifictext": "You are at {0}{1} now.", "jetkarmabot.status.listspecifictext": "You are at {0}{1} now.",
"jetkarmabot.status.havenothing": "You don't have anything yet.", "jetkarmabot.status.havenothing": "You don't have anything yet.",
"jetkarmabot.status.help": "Shows the amount of awards that you have",
"jetkarmabot.status.awardtypehelp": "The awardtype to show. If empty shows everything.",
"jetkarmabot.changelocale.justchanged": "Roger that.", "jetkarmabot.changelocale.justchanged": "Roger that.",
"jetkarmabot.changelocale.getlocale": "I'm currently speaking English.", "jetkarmabot.changelocale.getlocale": "I'm currently speaking English.",
"jetkarmabot.changelocale.listalltext": "I know:", "jetkarmabot.changelocale.listalltext": "I know:",
"jetkarmabot.changelocale.errorall": "I would like to speak all languages at once, but because of the rules of the universe, I am not allowed to do that.", "jetkarmabot.changelocale.errorall": "I would like to speak all languages at once, but because of the rules of the universe, I am not allowed to do that.",
"jetkarmabot.changelocale.beforenote": "Warning: ", "jetkarmabot.changelocale.beforenote": "Warning: ",
"jetkarmabot.changelocale.help": "Switches current chat locale to [locale]",
"jetkarmabot.changelocale.localehelp": "The locale to switch to. Can be \"list\" to list all possible locales. Also can be empty to get current locale",
"jetkarmabot.help.help": "Displays help text for all(one) command(s)",
"jetkarmabot.help.commandhelp": "The command to return help text for. If empty shows all commands.",
"jetkarmabot.awardtypes.star": "Star", "jetkarmabot.awardtypes.star": "Star",
"jetkarmabot.awardtypes.pie": "a pie from the shelf", "jetkarmabot.awardtypes.pie": "a pie from the shelf",
"jetkarmabot.awardtypes.dream": "Dream", "jetkarmabot.awardtypes.dream": "Dream",

View File

@ -12,14 +12,22 @@
"jetkarmabot.award.awardmessage": "Вручил \"{0}\" {1}!", "jetkarmabot.award.awardmessage": "Вручил \"{0}\" {1}!",
"jetkarmabot.award.revokemessage": "Отнял \"{0}\" у {1}!", "jetkarmabot.award.revokemessage": "Отнял \"{0}\" у {1}!",
"jetkarmabot.award.statustext": "У {0} теперь {1}{2}.", "jetkarmabot.award.statustext": "У {0} теперь {1}{2}.",
"jetkarmabot.award.help": "Поручает/лишает награду от ползователя/пользователю.",
"jetkarmabot.award.awardtypehelp": "Награду которую поручить/лишить от пользователя",
"jetkarmabot.status.listalltext": "У вас :", "jetkarmabot.status.listalltext": "У вас :",
"jetkarmabot.status.listspecifictext": "У вас сейчас {0}{1}.", "jetkarmabot.status.listspecifictext": "У вас сейчас {0}{1}.",
"jetkarmabot.status.havenothing": "У вас пока ничего нет.", "jetkarmabot.status.havenothing": "У вас пока ничего нет.",
"jetkarmabot.status.help": "",
"jetkarmabot.status.awardtypehelp": "",
"jetkarmabot.changelocale.justchanged": "Так точно.", "jetkarmabot.changelocale.justchanged": "Так точно.",
"jetkarmabot.changelocale.getlocale": "Я сейчас говорю по-русски.", "jetkarmabot.changelocale.getlocale": "Я сейчас говорю по-русски.",
"jetkarmabot.changelocale.listalltext": "Я знаю:", "jetkarmabot.changelocale.listalltext": "Я знаю:",
"jetkarmabot.changelocale.errorall": "Мне бы хотелось говорить на всех языках в то же самое время, но из-за ограничений вселенной, мне это не позволяется.", "jetkarmabot.changelocale.errorall": "Мне бы хотелось говорить на всех языках в то же самое время, но из-за ограничений вселенной, мне это не позволяется.",
"jetkarmabot.changelocale.beforenote": "Внимание: ", "jetkarmabot.changelocale.beforenote": "Внимание: ",
"jetkarmabot.changelocale.help": "",
"jetkarmabot.changelocale.localehelp": "",
"jetkarmabot.help.help": "",
"jetkarmabot.help.commandhelp": "",
"jetkarmabot.awardtypes.star": "Звездочка", "jetkarmabot.awardtypes.star": "Звездочка",
"jetkarmabot.awardtypes.pie": "с полки пирожок", "jetkarmabot.awardtypes.pie": "с полки пирожок",
"jetkarmabot.awardtypes.dream": "Мечта", "jetkarmabot.awardtypes.dream": "Мечта",