mirror of
https://github.com/Jetsparrow/karmabot.git
synced 2026-01-21 00:56:09 +03:00
Update dependencies
This commit is contained in:
parent
dc879e2984
commit
ed7346c16d
@ -32,7 +32,7 @@ namespace JetKarmaBot.Commands
|
|||||||
}
|
}
|
||||||
|
|
||||||
string awardTypeText = null;
|
string awardTypeText = null;
|
||||||
int recipientId = default(int);
|
long recipientId = default;
|
||||||
foreach (string arg in ctx.Command.Parameters)
|
foreach (string arg in ctx.Command.Parameters)
|
||||||
{
|
{
|
||||||
if (arg.StartsWith('@'))
|
if (arg.StartsWith('@'))
|
||||||
@ -61,7 +61,7 @@ namespace JetKarmaBot.Commands
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx.EventArgs.Message.ReplyToMessage != null && recipientId == default(int))
|
if (ctx.EventArgs.Message.ReplyToMessage != null && recipientId == default)
|
||||||
{
|
{
|
||||||
recipientId = ctx.EventArgs.Message.ReplyToMessage.From.Id;
|
recipientId = ctx.EventArgs.Message.ReplyToMessage.From.Id;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,14 +2,15 @@ using JetKarmaBot.Commands;
|
|||||||
using JetKarmaBot.Models;
|
using JetKarmaBot.Models;
|
||||||
using JetKarmaBot.Services;
|
using JetKarmaBot.Services;
|
||||||
using JetKarmaBot.Services.Handling;
|
using JetKarmaBot.Services.Handling;
|
||||||
|
using NLog;
|
||||||
using Perfusion;
|
using Perfusion;
|
||||||
using System;
|
using System;
|
||||||
using System.Net;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
using Telegram.Bot.Args;
|
using Telegram.Bot.Polling;
|
||||||
|
using Telegram.Bot.Types;
|
||||||
using Telegram.Bot.Types.Enums;
|
using Telegram.Bot.Types.Enums;
|
||||||
|
|
||||||
namespace JetKarmaBot
|
namespace JetKarmaBot
|
||||||
@ -21,6 +22,8 @@ namespace JetKarmaBot
|
|||||||
[Inject] KarmaContextFactory Db { get; set; }
|
[Inject] KarmaContextFactory Db { get; set; }
|
||||||
[Inject] TimeoutManager Timeout { get; set; }
|
[Inject] TimeoutManager Timeout { get; set; }
|
||||||
[Inject] Localization Locale { get; set; }
|
[Inject] Localization Locale { get; set; }
|
||||||
|
[Inject] Logger Log { get; set; }
|
||||||
|
|
||||||
|
|
||||||
TelegramBotClient Client { get; set; }
|
TelegramBotClient Client { get; set; }
|
||||||
ChatCommandRouter Commands;
|
ChatCommandRouter Commands;
|
||||||
@ -33,17 +36,8 @@ namespace JetKarmaBot
|
|||||||
{
|
{
|
||||||
using (KarmaContext db = Db.GetContext())
|
using (KarmaContext db = Db.GetContext())
|
||||||
await db.Database.EnsureCreatedAsync();
|
await db.Database.EnsureCreatedAsync();
|
||||||
if (Config.Proxy?.Url == null)
|
|
||||||
Client = new TelegramBotClient(Config.ApiKey);
|
Client = new TelegramBotClient(Config.ApiKey);
|
||||||
else
|
|
||||||
{
|
|
||||||
var httpProxy = new WebProxy($"{Config.Proxy.Url}:{Config.Proxy.Port}")
|
|
||||||
{
|
|
||||||
Credentials = new NetworkCredential(Config.Proxy.Login, Config.Proxy.Password)
|
|
||||||
};
|
|
||||||
|
|
||||||
Client = new TelegramBotClient(Config.ApiKey, httpProxy);
|
|
||||||
}
|
|
||||||
Container.AddInstance(Client);
|
Container.AddInstance(Client);
|
||||||
|
|
||||||
timeoutWaitTaskToken = new CancellationTokenSource();
|
timeoutWaitTaskToken = new CancellationTokenSource();
|
||||||
@ -52,14 +46,17 @@ namespace JetKarmaBot
|
|||||||
await InitCommands(Container);
|
await InitCommands(Container);
|
||||||
InitChain(Container);
|
InitChain(Container);
|
||||||
|
|
||||||
Client.OnMessage += BotOnMessageReceived;
|
var receiverOptions = new ReceiverOptions { AllowedUpdates = new[] { UpdateType.Message } };
|
||||||
Client.StartReceiving();
|
Client.StartReceiving(
|
||||||
|
HandleUpdateAsync,
|
||||||
|
HandleErrorAsync,
|
||||||
|
receiverOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Stop()
|
public async Task Stop()
|
||||||
{
|
{
|
||||||
if (stopped) return;
|
if (stopped) return;
|
||||||
Client?.StopReceiving();
|
Client?.CloseAsync();
|
||||||
timeoutWaitTaskToken?.Cancel();
|
timeoutWaitTaskToken?.Cancel();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -74,18 +71,34 @@ namespace JetKarmaBot
|
|||||||
|
|
||||||
#region service
|
#region service
|
||||||
|
|
||||||
void BotOnMessageReceived(object sender, MessageEventArgs args)
|
Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var message = args.Message;
|
Log.Error(exception, "Exception while handling API message");
|
||||||
if (message == null || message.Type != MessageType.Text)
|
return Task.CompletedTask;
|
||||||
return;
|
}
|
||||||
if (!CommandString.TryParse(args.Message.Text, out var cmd))
|
|
||||||
return;
|
|
||||||
if (cmd.UserName != null && cmd.UserName != Commands.Me.Username)
|
|
||||||
return;
|
|
||||||
|
|
||||||
RequestContext ctx = new RequestContext(Client, args, cmd);
|
async Task HandleUpdateAsync(ITelegramBotClient sender, Update update, CancellationToken cancellationToken)
|
||||||
_ = Chain.Handle(ctx);
|
{
|
||||||
|
if (update.Type != UpdateType.Message || update?.Message?.Type != MessageType.Text)
|
||||||
|
return;
|
||||||
|
var message = update.Message!;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (message == null || message.Type != MessageType.Text)
|
||||||
|
return;
|
||||||
|
if (!CommandString.TryParse(message.Text, out var cmd))
|
||||||
|
return;
|
||||||
|
if (cmd.UserName != null && cmd.UserName != Commands.Me.Username)
|
||||||
|
return;
|
||||||
|
|
||||||
|
RequestContext ctx = new RequestContext(Client, update, cmd);
|
||||||
|
await Chain.Handle(ctx);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Error(e, "Exception while handling message {0}", message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task InitCommands(IContainer c)
|
async Task InitCommands(IContainer c)
|
||||||
|
|||||||
@ -1,18 +1,17 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net5.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.4">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.14">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.2.0" />
|
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
|
||||||
<PackageReference Include="Telegram.Bot" Version="14.12.0" />
|
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
|
||||||
<PackageReference Include="NLog" Version="4.6.5" />
|
<PackageReference Include="NLog" Version="5.2.5" />
|
||||||
<PackageReference Include="NLog.Config" Version="4.6.5" />
|
|
||||||
<ProjectReference Include="..\perfusion\Perfusion\Perfusion.csproj" />
|
<ProjectReference Include="..\perfusion\Perfusion\Perfusion.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@ -8,8 +8,8 @@ namespace JetKarmaBot.Models
|
|||||||
{
|
{
|
||||||
public int AwardId { get; set; }
|
public int AwardId { get; set; }
|
||||||
public long ChatId { get; set; }
|
public long ChatId { get; set; }
|
||||||
public int FromId { get; set; }
|
public long FromId { get; set; }
|
||||||
public int ToId { get; set; }
|
public long ToId { get; set; }
|
||||||
public sbyte AwardTypeId { get; set; }
|
public sbyte AwardTypeId { get; set; }
|
||||||
public sbyte Amount { get; set; }
|
public sbyte Amount { get; set; }
|
||||||
public DateTime Date { get; set; }
|
public DateTime Date { get; set; }
|
||||||
|
|||||||
@ -34,20 +34,20 @@ namespace JetKarmaBot.Models
|
|||||||
entity.ToTable("award");
|
entity.ToTable("award");
|
||||||
|
|
||||||
entity.HasIndex(e => e.AwardId)
|
entity.HasIndex(e => e.AwardId)
|
||||||
.HasName("awardid_UNIQUE")
|
.HasDatabaseName("awardid_UNIQUE")
|
||||||
.IsUnique();
|
.IsUnique();
|
||||||
|
|
||||||
entity.HasIndex(e => e.AwardTypeId)
|
entity.HasIndex(e => e.AwardTypeId)
|
||||||
.HasName("fk_awardtype_idx");
|
.HasDatabaseName("fk_awardtype_idx");
|
||||||
|
|
||||||
entity.HasIndex(e => e.ChatId)
|
entity.HasIndex(e => e.ChatId)
|
||||||
.HasName("fk_chat_idx");
|
.HasDatabaseName("fk_chat_idx");
|
||||||
|
|
||||||
entity.HasIndex(e => e.FromId)
|
entity.HasIndex(e => e.FromId)
|
||||||
.HasName("fk_from_idx");
|
.HasDatabaseName("fk_from_idx");
|
||||||
|
|
||||||
entity.HasIndex(e => e.ToId)
|
entity.HasIndex(e => e.ToId)
|
||||||
.HasName("fk_to_idx");
|
.HasDatabaseName("fk_to_idx");
|
||||||
|
|
||||||
entity.Property(e => e.AwardId)
|
entity.Property(e => e.AwardId)
|
||||||
.HasColumnName("awardid")
|
.HasColumnName("awardid")
|
||||||
@ -109,11 +109,11 @@ namespace JetKarmaBot.Models
|
|||||||
entity.ToTable("awardtype");
|
entity.ToTable("awardtype");
|
||||||
|
|
||||||
entity.HasIndex(e => e.AwardTypeId)
|
entity.HasIndex(e => e.AwardTypeId)
|
||||||
.HasName("awardtypeid_UNIQUE")
|
.HasDatabaseName("awardtypeid_UNIQUE")
|
||||||
.IsUnique();
|
.IsUnique();
|
||||||
|
|
||||||
entity.HasIndex(e => e.CommandName)
|
entity.HasIndex(e => e.CommandName)
|
||||||
.HasName("commandname_UNIQUE")
|
.HasDatabaseName("commandname_UNIQUE")
|
||||||
.IsUnique();
|
.IsUnique();
|
||||||
|
|
||||||
entity.Property(e => e.AwardTypeId)
|
entity.Property(e => e.AwardTypeId)
|
||||||
|
|||||||
@ -12,7 +12,7 @@ namespace JetKarmaBot.Models
|
|||||||
AwardsTo = new HashSet<Award>();
|
AwardsTo = new HashSet<Award>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int UserId { get; set; }
|
public long UserId { get; set; }
|
||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
public DateTime CooldownDate { get; set; }
|
public DateTime CooldownDate { get; set; }
|
||||||
[InverseProperty("From")]
|
[InverseProperty("From")]
|
||||||
|
|||||||
@ -28,8 +28,11 @@ namespace JetKarmaBot
|
|||||||
var cfg = new Config("karma.cfg.json");
|
var cfg = new Config("karma.cfg.json");
|
||||||
c.AddInstance(cfg);
|
c.AddInstance(cfg);
|
||||||
|
|
||||||
|
var connStr = cfg.ConnectionString + (cfg.ConnectionString.EndsWith(";") ? "" : ";") + "TreatTinyAsBoolean=false";
|
||||||
|
var serverVersion = ServerVersion.AutoDetect(connStr);
|
||||||
|
|
||||||
var dbOptions = new DbContextOptionsBuilder<KarmaContext>()
|
var dbOptions = new DbContextOptionsBuilder<KarmaContext>()
|
||||||
.UseMySql(cfg.ConnectionString + (cfg.ConnectionString.EndsWith(";") ? "" : ";") + "TreatTinyAsBoolean=false");
|
.UseMySql(connStr, serverVersion);
|
||||||
c.AddInfo<Logger>(new LogInfo());
|
c.AddInfo<Logger>(new LogInfo());
|
||||||
if (cfg.SqlDebug)
|
if (cfg.SqlDebug)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -6,16 +6,17 @@ using JetKarmaBot.Commands;
|
|||||||
using JetKarmaBot.Models;
|
using JetKarmaBot.Models;
|
||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
using Telegram.Bot.Args;
|
using Telegram.Bot.Args;
|
||||||
|
using Telegram.Bot.Types;
|
||||||
|
|
||||||
namespace JetKarmaBot.Services.Handling
|
namespace JetKarmaBot.Services.Handling
|
||||||
{
|
{
|
||||||
public class RequestContext : IServiceProvider
|
public class RequestContext : IServiceProvider
|
||||||
{
|
{
|
||||||
public ITelegramBotClient Client { get; }
|
public ITelegramBotClient Client { get; }
|
||||||
public MessageEventArgs EventArgs { get; }
|
public Update EventArgs { get; }
|
||||||
public CommandString Command { get; }
|
public CommandString Command { get; }
|
||||||
public Dictionary<Type, object> Features { get; } = new Dictionary<Type, object>();
|
public Dictionary<Type, object> Features { get; } = new Dictionary<Type, object>();
|
||||||
public RequestContext(ITelegramBotClient client, MessageEventArgs args, CommandString cmd)
|
public RequestContext(ITelegramBotClient client, Update args, CommandString cmd)
|
||||||
{
|
{
|
||||||
Client = client;
|
Client = client;
|
||||||
EventArgs = args;
|
EventArgs = args;
|
||||||
|
|||||||
@ -26,7 +26,7 @@ namespace JetKarmaBot.Services.Handling
|
|||||||
|
|
||||||
public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next)
|
public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next)
|
||||||
{
|
{
|
||||||
int uid = ctx.EventArgs.Message.From.Id;
|
var uid = ctx.EventArgs.Message.From.Id;
|
||||||
if (Timeout.TimeoutCache.TryGetValue(uid, out var stats))
|
if (Timeout.TimeoutCache.TryGetValue(uid, out var stats))
|
||||||
{
|
{
|
||||||
DateTime debtLimit = DateTime.Now.AddSeconds(Timeout.cfg.Timeout.DebtLimitSeconds);
|
DateTime debtLimit = DateTime.Now.AddSeconds(Timeout.cfg.Timeout.DebtLimitSeconds);
|
||||||
@ -46,8 +46,8 @@ namespace JetKarmaBot.Services.Handling
|
|||||||
[Inject] private Config cfg;
|
[Inject] private Config cfg;
|
||||||
[Inject] private Localization Locale;
|
[Inject] private Localization Locale;
|
||||||
[Inject] private Logger log;
|
[Inject] private Logger log;
|
||||||
public Dictionary<int, TimeoutStats> TimeoutCache = new Dictionary<int, TimeoutStats>();
|
public Dictionary<long, TimeoutStats> TimeoutCache = new ();
|
||||||
private async Task ApplyCost(string name, bool succeded, int uid, KarmaContext db, Feature feature)
|
private async Task ApplyCost(string name, bool succeded, long uid, KarmaContext db, Feature feature)
|
||||||
{
|
{
|
||||||
if (feature.Multiplier == 0)
|
if (feature.Multiplier == 0)
|
||||||
return;
|
return;
|
||||||
@ -67,7 +67,7 @@ namespace JetKarmaBot.Services.Handling
|
|||||||
TimeoutCache[uid].TimeoutMessaged = false;
|
TimeoutCache[uid].TimeoutMessaged = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task PopulateStats(int uid, KarmaContext db)
|
private async Task PopulateStats(long uid, KarmaContext db)
|
||||||
{
|
{
|
||||||
if (!TimeoutCache.ContainsKey(uid))
|
if (!TimeoutCache.ContainsKey(uid))
|
||||||
{
|
{
|
||||||
@ -83,7 +83,7 @@ namespace JetKarmaBot.Services.Handling
|
|||||||
log.Info("Saving timeout info to database");
|
log.Info("Saving timeout info to database");
|
||||||
using (KarmaContext db = Db.GetContext())
|
using (KarmaContext db = Db.GetContext())
|
||||||
{
|
{
|
||||||
foreach (int i in TimeoutCache.Keys)
|
foreach (var i in TimeoutCache.Keys)
|
||||||
{
|
{
|
||||||
(await db.Users.FindAsync(new object[] { i }, ct)).CooldownDate = TimeoutCache[i].CooldownDate;
|
(await db.Users.FindAsync(new object[] { i }, ct)).CooldownDate = TimeoutCache[i].CooldownDate;
|
||||||
}
|
}
|
||||||
@ -102,7 +102,7 @@ namespace JetKarmaBot.Services.Handling
|
|||||||
|
|
||||||
public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next)
|
public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next)
|
||||||
{
|
{
|
||||||
int uid = ctx.EventArgs.Message.From.Id;
|
var uid = ctx.EventArgs.Message.From.Id;
|
||||||
KarmaContext db = ctx.GetFeature<KarmaContext>();
|
KarmaContext db = ctx.GetFeature<KarmaContext>();
|
||||||
await PopulateStats(uid, db);
|
await PopulateStats(uid, db);
|
||||||
DateTime debtLimit = DateTime.Now.AddSeconds(cfg.Timeout.DebtLimitSeconds);
|
DateTime debtLimit = DateTime.Now.AddSeconds(cfg.Timeout.DebtLimitSeconds);
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
Subproject commit 5913a7e44f7e9ac63a5bfd04c837a9968cf1fe88
|
Subproject commit e4c448c0505ae081d23e46151b6628ec5a9f3a42
|
||||||
Loading…
Reference in New Issue
Block a user