Merge remote-tracking branch 'origin/master'

This commit is contained in:
jetsparrow 2018-12-19 16:33:38 +03:00
commit 91bb1dd383
9 changed files with 32 additions and 32 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "perfusion"]
path = perfusion
url = http://192.168.1.65/git/perfusion.git

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15 # Visual Studio 15
VisualStudioVersion = 15.0.28010.2036 VisualStudioVersion = 15.0.28010.2036
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JetKarmaBot", "JetKarmaBot\JetKarmaBot.csproj", "{729E88EE-BE5E-4D12-B83F-EDC5FC5E2D07}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JetKarmaBot", "JetKarmaBot\JetKarmaBot.csproj", "{729E88EE-BE5E-4D12-B83F-EDC5FC5E2D07}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -6,6 +6,7 @@ using Telegram.Bot;
using Telegram.Bot.Args; using Telegram.Bot.Args;
using Telegram.Bot.Types; using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.Enums;
using Perfusion;
namespace JetKarmaBot.Commands namespace JetKarmaBot.Commands
{ {
@ -59,14 +60,12 @@ namespace JetKarmaBot.Commands
} }
Db Db { get; } [Inject(true)]Db Db { get; set; }
TelegramBotClient Client { get; } [Inject(true)] TelegramBotClient Client { get; set; }
User Me { get; } User Me { get; }
public AwardCommand(Db db, TelegramBotClient client, User me) public AwardCommand(User me)
{ {
Db = db;
Client = client;
Me = me; Me = me;
} }
} }

View File

@ -1,16 +1,12 @@
using System.Collections.Generic; using System.Collections.Generic;
using Telegram.Bot.Args; using Telegram.Bot.Args;
using Perfusion;
namespace JetKarmaBot.Commands namespace JetKarmaBot.Commands
{ {
public class StartCommand : IChatCommand public class StartCommand : IChatCommand
{ {
Db Db; [Inject(true)]Db Db;
public StartCommand(Db db)
{
Db = db;
}
public IReadOnlyCollection<string> Names => new[] { "start" }; public IReadOnlyCollection<string> Names => new[] { "start" };

View File

@ -1,4 +1,5 @@
using JetKarmaBot.Commands; using JetKarmaBot.Commands;
using Perfusion;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -21,19 +22,19 @@ namespace JetKarmaBot
foreach (var u in db.Chats) foreach (var u in db.Chats)
client.SendTextMessageAsync(u.Value.ChatId, message); client.SendTextMessageAsync(u.Value.ChatId, message);
} }
public JetKarmaBot(Config cfg, Db db) public JetKarmaBot([Inject(true)]Config cfg, [Inject(true)] Container container)
{ {
this.db = db;
var httpProxy = new WebProxy($"{cfg.ProxyUrl}:{cfg.ProxyPort}") var httpProxy = new WebProxy($"{cfg.ProxyUrl}:{cfg.ProxyPort}")
{ {
Credentials = new NetworkCredential(cfg.ProxyLogin, cfg.ProxyPassword) Credentials = new NetworkCredential(cfg.ProxyLogin, cfg.ProxyPassword)
}; };
var botClient = new TelegramBotClient(cfg.ApiKey, httpProxy); var botClient = new TelegramBotClient(cfg.ApiKey, httpProxy);
container.AddInstance(botClient);
var cred = new NetworkCredential(cfg.ProxyLogin, cfg.ProxyPassword); var cred = new NetworkCredential(cfg.ProxyLogin, cfg.ProxyPassword);
client = new TelegramBotClient(cfg.ApiKey, httpProxy); client = new TelegramBotClient(cfg.ApiKey, httpProxy);
me = client.GetMeAsync().Result; me = client.GetMeAsync().Result;
InitCommands(); InitCommands(container);
client.OnMessage += BotOnMessageReceived; client.OnMessage += BotOnMessageReceived;
client.StartReceiving(); client.StartReceiving();
} }
@ -46,7 +47,7 @@ namespace JetKarmaBot
#endregion #endregion
#region service #region service
Db db { get; } [Inject(true)] Db db { get; set; }
TelegramBotClient client { get; } TelegramBotClient client { get; }
User me { get; } User me { get; }
@ -62,11 +63,11 @@ namespace JetKarmaBot
long from = message.From.Id; long from = message.From.Id;
Task.Run(() => commands.Execute(sender, messageEventArgs)); Task.Run(() => commands.Execute(sender, messageEventArgs));
} }
void InitCommands() void InitCommands(Container c)
{ {
commands = new ChatCommandRouter(); commands = new ChatCommandRouter();
commands.Add(new StartCommand(db)); commands.Add(c.ResolveObject(new StartCommand()));
commands.Add(new AwardCommand(db, client, me)); commands.Add(c.ResolveObject(new AwardCommand(me)));
} }
#endregion #endregion

View File

@ -10,6 +10,7 @@
<PackageReference Include="MySql.Data" Version="8.0.13" /> <PackageReference Include="MySql.Data" Version="8.0.13" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Telegram.Bot" Version="14.10.0" /> <PackageReference Include="Telegram.Bot" Version="14.10.0" />
<ProjectReference Include="..\perfusion\Perfusion\Perfusion.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,4 +1,5 @@
using System; using System;
using Perfusion;
namespace JetKarmaBot namespace JetKarmaBot
{ {
@ -6,23 +7,20 @@ namespace JetKarmaBot
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
Current = new App(new Config("karma.cfg.json")); Container c = new Container();
c.AddInstance(new Config("karma.cfg.json"));
Current = c.GetInstance(typeof(App)) as App;
Console.ReadKey(); Console.ReadKey();
} }
public static App Current { get; private set; } public static App Current { get; private set; }
[Inject(true)]
public App(Config cfg) public void Run(Container c)
{ {
Config = cfg; Watcher = c.GetInstance<JetKarmaBot>();
Db = new Db(Config);
Watcher = new JetKarmaBot(Config, Db);
Console.WriteLine("JetKarmaBot started!"); Console.WriteLine("JetKarmaBot started!");
} }
Config Config { get; } JetKarmaBot Watcher { get; set; }
Db Db { get; }
JetKarmaBot Watcher { get; }
} }
} }

View File

@ -1,9 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Linq; using System.Linq;
using Dapper; using Dapper;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using Perfusion;
namespace JetKarmaBot namespace JetKarmaBot
{ {
@ -105,7 +106,7 @@ namespace JetKarmaBot
#endregion #endregion
#region service #region service
public Db(Config cfg) public Db([Inject(true)]Config cfg)
{ {
Log("Initializing..."); Log("Initializing...");
Conn = new MySqlConnection(cfg.ConnectionString); Conn = new MySqlConnection(cfg.ConnectionString);

1
perfusion Submodule

@ -0,0 +1 @@
Subproject commit 470af231e3f7cf6942907d3b50473fa04dc04caf