Update perfusion

This commit is contained in:
Nikolay Kochulin 2019-08-14 21:24:50 +00:00
parent 9bf70017c8
commit 35dbb7fe78
5 changed files with 22 additions and 28 deletions

View File

@ -14,14 +14,14 @@ namespace JetKarmaBot
{ {
public class ChatCommandRouter public class ChatCommandRouter
{ {
Telegram.Bot.Types.User BotUser { get; } public Telegram.Bot.Types.User Me { get; private set; }
[Inject] private Logger log; [Inject] private Logger log;
[Inject] private KarmaContextFactory Db; [Inject] private KarmaContextFactory Db;
[Inject] private TelegramBotClient Client { get; set; } [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<bool> Execute(object sender, MessageEventArgs args) public async Task<bool> Execute(object sender, MessageEventArgs args)
@ -30,7 +30,7 @@ namespace JetKarmaBot
var text = args.Message.Text; var text = args.Message.Text;
if (CommandString.TryParse(text, out var cmd)) 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! // directed not at us!
log.Debug("Message not directed at us"); log.Debug("Message not directed at us");

View File

@ -43,7 +43,7 @@ namespace JetKarmaBot.Commands
return true; return true;
} }
if (Me.Id == recipient.Id) if (CommandRouter.Me.Id == recipient.Id)
{ {
await Client.SendTextMessageAsync( await Client.SendTextMessageAsync(
args.Message.Chat.Id, args.Message.Chat.Id,
@ -114,7 +114,7 @@ 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; }
User Me { get; } [Inject] ChatCommandRouter CommandRouter { get; set; }
public string Description => "Awards/revokes an award to a user."; public string Description => "Awards/revokes an award to a user.";
public string DescriptionID => "jetkarmabot.award.help"; public string DescriptionID => "jetkarmabot.award.help";
@ -128,10 +128,5 @@ namespace JetKarmaBot.Commands
DescriptionID="jetkarmabot.award.awardtypehelp" DescriptionID="jetkarmabot.award.awardtypehelp"
} }
}; };
public AwardCommand(User me)
{
Me = me;
}
} }
} }

View File

@ -13,7 +13,7 @@ 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; [Inject] 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 string Description => "Displays help text for all(one) command(s)";
@ -54,9 +54,5 @@ namespace JetKarmaBot.Commands
} }
} }
} }
public HelpCommand(ChatCommandRouter router)
{
Router = router;
}
} }
} }

View File

@ -22,7 +22,6 @@ namespace JetKarmaBot
TelegramBotClient Client { get; set; } TelegramBotClient Client { get; set; }
ChatCommandRouter Commands; ChatCommandRouter Commands;
Telegram.Bot.Types.User Me { get; set; }
public async Task Init() public async Task Init()
{ {
@ -35,9 +34,8 @@ namespace JetKarmaBot
Client = new TelegramBotClient(Config.ApiKey, httpProxy); Client = new TelegramBotClient(Config.ApiKey, httpProxy);
Container.AddInstance(Client); Container.AddInstance(Client);
Me = await Client.GetMeAsync();
InitCommands(Container); await InitCommands(Container);
Client.OnMessage += BotOnMessageReceived; Client.OnMessage += BotOnMessageReceived;
Client.StartReceiving(); Client.StartReceiving();
@ -84,15 +82,20 @@ namespace JetKarmaBot
(await db.Users.FindAsync(u.Id)).Username = un; (await db.Users.FindAsync(u.Id)).Username = un;
} }
void InitCommands(IContainer c) async Task InitCommands(IContainer c)
{ {
Commands = c.ResolveObject(new ChatCommandRouter(Me)); c.Add<HelpCommand>();
Commands.Add(c.ResolveObject(new HelpCommand(Commands))); c.Add<AwardCommand>();
Commands.Add(c.ResolveObject(new AwardCommand(Me))); c.Add<StatusCommand>();
Commands.Add(c.ResolveObject(new StatusCommand())); c.Add<LocaleCommand>();
Commands.Add(c.ResolveObject(new LocaleCommand())); c.Add<CurrenciesCommand>();
Commands.Add(c.ResolveObject(new CurrenciesCommand())); c.Add<LeaderboardCommand>();
Commands.Add(c.ResolveObject(new LeaderboardCommand())); Commands = c.GetInstance<ChatCommandRouter>();
await Commands.Start();
foreach (IChatCommand cmd in c.GetInstances<IChatCommand>())
{
Commands.Add(cmd);
}
} }
#endregion #endregion

@ -1 +1 @@
Subproject commit 40e8f8871f42f1f32c29480505cf5ac6ba61c393 Subproject commit 036e4c67275e859207feee215cf87d614e7198d2