mirror of
https://github.com/Jetsparrow/karmabot.git
synced 2026-01-21 09:06:09 +03:00
Add help command mechanisms
This commit is contained in:
parent
5789890fd9
commit
7232d18736
@ -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,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>();
|
Dictionary<string, IChatCommand> commands = new Dictionary<string, IChatCommand>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -92,6 +92,17 @@ 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 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)
|
public AwardCommand(User me)
|
||||||
{
|
{
|
||||||
Me = me;
|
Me = me;
|
||||||
|
|||||||
@ -67,5 +67,16 @@ 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 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 Perfusion;
|
||||||
using JetKarmaBot.Services;
|
using JetKarmaBot.Services;
|
||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
|
using Telegram.Bot.Types.Enums;
|
||||||
|
|
||||||
namespace JetKarmaBot.Commands
|
namespace JetKarmaBot.Commands
|
||||||
{
|
{
|
||||||
@ -11,19 +12,48 @@ namespace JetKarmaBot.Commands
|
|||||||
[Inject] KarmaContextFactory Db;
|
[Inject] KarmaContextFactory Db;
|
||||||
[Inject] TelegramBotClient Client { get; set; }
|
[Inject] TelegramBotClient Client { get; set; }
|
||||||
[Inject] Localization Locale { get; set; }
|
[Inject] Localization Locale { get; set; }
|
||||||
|
ChatCommandRouter Router;
|
||||||
public IReadOnlyCollection<string> Names => new[] { "help" };
|
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)
|
public bool Execute(CommandString cmd, MessageEventArgs args)
|
||||||
{
|
{
|
||||||
using (var db = Db.GetContext())
|
using (var db = Db.GetContext())
|
||||||
{
|
{
|
||||||
var currentLocale = Locale[db.Chats.Find(args.Message.Chat.Id).Locale];
|
var currentLocale = Locale[db.Chats.Find(args.Message.Chat.Id).Locale];
|
||||||
Client.SendTextMessageAsync(
|
if (cmd.Parameters.Length < 1)
|
||||||
args.Message.Chat.Id,
|
{
|
||||||
currentLocale["jetkarmabot.help"],
|
Client.SendTextMessageAsync(
|
||||||
replyToMessageId: args.Message.MessageId);
|
args.Message.Chat.Id,
|
||||||
return true;
|
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
|
public interface IChatCommand
|
||||||
{
|
{
|
||||||
IReadOnlyCollection<string> Names { get; }
|
IReadOnlyCollection<string> Names { get; }
|
||||||
|
string Description { 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 enum ChatCommandArgumentType
|
||||||
|
{
|
||||||
|
Boolean,
|
||||||
|
String,
|
||||||
|
Integer,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -67,5 +67,15 @@ 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 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)
|
void InitCommands(Container c)
|
||||||
{
|
{
|
||||||
Commands = c.ResolveObject(new ChatCommandRouter(Me));
|
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 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()));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user