mirror of
https://github.com/Jetsparrow/karmabot.git
synced 2026-01-21 00:56:09 +03:00
Add help command mechanisms
This commit is contained in:
parent
b93d20e07e
commit
83d1b458af
@ -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<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>" + c.Description + "</i>";
|
||||
pieces.Add(build);
|
||||
}
|
||||
return string.Join('\n', pieces);
|
||||
}
|
||||
|
||||
internal string GetHelpTextFor(string commandname)
|
||||
{
|
||||
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>" + c.Description + "</i>\n";
|
||||
build += string.Join("\n", c.Arguments.Select(ca => (!ca.Required ? "[" : "") + ca.Name + (!ca.Required ? "]" : "") + ": <i>" + ca.Description + "</i>"));
|
||||
return build;
|
||||
}
|
||||
|
||||
Dictionary<string, IChatCommand> commands = new Dictionary<string, IChatCommand>();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<ChatCommandArgument> 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;
|
||||
|
||||
@ -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<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"
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<string> Names => new[] { "help" };
|
||||
|
||||
public string Description => "Displays help text for all(one) command(s)";
|
||||
|
||||
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"
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,7 +6,24 @@ namespace JetKarmaBot.Commands
|
||||
public interface IChatCommand
|
||||
{
|
||||
IReadOnlyCollection<string> Names { get; }
|
||||
string Description { get; }
|
||||
IReadOnlyCollection<ChatCommandArgument> 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,
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<ChatCommandArgument> Arguments => new ChatCommandArgument[] {
|
||||
new ChatCommandArgument(){
|
||||
Name="awardtype",
|
||||
Required=false,
|
||||
Type=ChatCommandArgumentType.String,
|
||||
Description="The awardtype to show. If empty shows everything."
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -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()));
|
||||
|
||||
Loading…
Reference in New Issue
Block a user