diff --git a/JetKarmaBot/CommandRouter.cs b/JetKarmaBot/CommandRouter.cs index 1f02714..399e494 100644 --- a/JetKarmaBot/CommandRouter.cs +++ b/JetKarmaBot/CommandRouter.cs @@ -14,14 +14,14 @@ namespace JetKarmaBot { public class ChatCommandRouter { - Telegram.Bot.Types.User BotUser { get; } + public Telegram.Bot.Types.User Me { get; private set; } [Inject] private Logger log; [Inject] private KarmaContextFactory Db; [Inject] private TelegramBotClient Client { get; set; } - public ChatCommandRouter(Telegram.Bot.Types.User botUser) + public async Task Start() { - BotUser = botUser; + Me = await Client.GetMeAsync(); } public async Task Execute(object sender, MessageEventArgs args) @@ -30,7 +30,7 @@ namespace JetKarmaBot var text = args.Message.Text; if (CommandString.TryParse(text, out var cmd)) { - if (cmd.UserName != null && cmd.UserName != BotUser.Username) + if (cmd.UserName != null && cmd.UserName != Me.Username) { // directed not at us! log.Debug("Message not directed at us"); diff --git a/JetKarmaBot/Commands/AwardCommand.cs b/JetKarmaBot/Commands/AwardCommand.cs index d36648a..9064909 100644 --- a/JetKarmaBot/Commands/AwardCommand.cs +++ b/JetKarmaBot/Commands/AwardCommand.cs @@ -43,7 +43,7 @@ namespace JetKarmaBot.Commands return true; } - if (Me.Id == recipient.Id) + if (CommandRouter.Me.Id == recipient.Id) { await Client.SendTextMessageAsync( args.Message.Chat.Id, @@ -114,7 +114,7 @@ namespace JetKarmaBot.Commands [Inject] KarmaContextFactory Db { get; set; } [Inject] TelegramBotClient Client { get; set; } [Inject] Localization Locale { get; set; } - User Me { get; } + [Inject] ChatCommandRouter CommandRouter { get; set; } public string Description => "Awards/revokes an award to a user."; public string DescriptionID => "jetkarmabot.award.help"; @@ -128,10 +128,5 @@ namespace JetKarmaBot.Commands DescriptionID="jetkarmabot.award.awardtypehelp" } }; - - public AwardCommand(User me) - { - Me = me; - } } } diff --git a/JetKarmaBot/Commands/HelpCommand.cs b/JetKarmaBot/Commands/HelpCommand.cs index 000cfb1..01b2ac8 100644 --- a/JetKarmaBot/Commands/HelpCommand.cs +++ b/JetKarmaBot/Commands/HelpCommand.cs @@ -13,7 +13,7 @@ namespace JetKarmaBot.Commands [Inject] KarmaContextFactory Db; [Inject] TelegramBotClient Client { get; set; } [Inject] Localization Locale { get; set; } - ChatCommandRouter Router; + [Inject] ChatCommandRouter Router; public IReadOnlyCollection Names => new[] { "help" }; public string Description => "Displays help text for all(one) command(s)"; @@ -54,9 +54,5 @@ namespace JetKarmaBot.Commands } } } - public HelpCommand(ChatCommandRouter router) - { - Router = router; - } } } diff --git a/JetKarmaBot/JetKarmaBot.cs b/JetKarmaBot/JetKarmaBot.cs index 1e1a841..77b9ecf 100644 --- a/JetKarmaBot/JetKarmaBot.cs +++ b/JetKarmaBot/JetKarmaBot.cs @@ -22,7 +22,6 @@ namespace JetKarmaBot TelegramBotClient Client { get; set; } ChatCommandRouter Commands; - Telegram.Bot.Types.User Me { get; set; } public async Task Init() { @@ -35,9 +34,8 @@ namespace JetKarmaBot Client = new TelegramBotClient(Config.ApiKey, httpProxy); Container.AddInstance(Client); - Me = await Client.GetMeAsync(); - InitCommands(Container); + await InitCommands(Container); Client.OnMessage += BotOnMessageReceived; Client.StartReceiving(); @@ -84,15 +82,20 @@ namespace JetKarmaBot (await db.Users.FindAsync(u.Id)).Username = un; } - void InitCommands(IContainer c) + async Task InitCommands(IContainer c) { - Commands = c.ResolveObject(new ChatCommandRouter(Me)); - 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())); - Commands.Add(c.ResolveObject(new CurrenciesCommand())); - Commands.Add(c.ResolveObject(new LeaderboardCommand())); + c.Add(); + c.Add(); + c.Add(); + c.Add(); + c.Add(); + c.Add(); + Commands = c.GetInstance(); + await Commands.Start(); + foreach (IChatCommand cmd in c.GetInstances()) + { + Commands.Add(cmd); + } } #endregion diff --git a/perfusion b/perfusion index 40e8f88..036e4c6 160000 --- a/perfusion +++ b/perfusion @@ -1 +1 @@ -Subproject commit 40e8f8871f42f1f32c29480505cf5ac6ba61c393 +Subproject commit 036e4c67275e859207feee215cf87d614e7198d2