mirror of
https://github.com/Jetsparrow/karmabot.git
synced 2026-01-21 00:56:09 +03:00
Make RequestContext.Features a dictionary
This commit is contained in:
parent
e106681c11
commit
09c07e6296
@ -31,7 +31,7 @@ namespace JetKarmaBot.Services.Handling
|
|||||||
log.Debug("Message received");
|
log.Debug("Message received");
|
||||||
CommandString cmd = ctx.Command;
|
CommandString cmd = ctx.Command;
|
||||||
Feature feature = new Feature() { Router = this };
|
Feature feature = new Feature() { Router = this };
|
||||||
ctx.Features.Add(feature);
|
ctx.AddFeature(feature);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@ -13,8 +13,8 @@ namespace JetKarmaBot.Services.Handling
|
|||||||
{
|
{
|
||||||
using (var db = Db.GetContext())
|
using (var db = Db.GetContext())
|
||||||
{
|
{
|
||||||
ctx.Features.Add(db); // KarmaContext
|
ctx.AddFeature(db); // KarmaContext
|
||||||
ctx.Features.Add(Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id))?.Locale ?? "ru-ru"]); // Locale
|
ctx.AddFeature(Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id))?.Locale ?? "ru-ru"]); // Locale
|
||||||
await next(ctx);
|
await next(ctx);
|
||||||
await db.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,15 +14,16 @@ namespace JetKarmaBot.Services.Handling
|
|||||||
public ITelegramBotClient Client { get; }
|
public ITelegramBotClient Client { get; }
|
||||||
public MessageEventArgs EventArgs { get; }
|
public MessageEventArgs EventArgs { get; }
|
||||||
public CommandString Command { get; }
|
public CommandString Command { get; }
|
||||||
public ICollection<object> Features { get; } = new List<object>();
|
public Dictionary<Type, object> Features { get; } = new Dictionary<Type, object>();
|
||||||
public RequestContext(ITelegramBotClient client, MessageEventArgs args, CommandString cmd)
|
public RequestContext(ITelegramBotClient client, MessageEventArgs args, CommandString cmd)
|
||||||
{
|
{
|
||||||
Client = client;
|
Client = client;
|
||||||
EventArgs = args;
|
EventArgs = args;
|
||||||
Command = cmd;
|
Command = cmd;
|
||||||
}
|
}
|
||||||
public object GetService(Type serviceType) => Features.First(x => x.GetType() == serviceType);
|
public object GetService(Type serviceType) => Features[serviceType];
|
||||||
public T GetFeature<T>() => (T)Features.First(x => x is T);
|
public T GetFeature<T>() => (T)Features[typeof(T)];
|
||||||
|
public void AddFeature<T>(T feat) => Features[typeof(T)] = feat;
|
||||||
|
|
||||||
//Method to reduce WET in commands
|
//Method to reduce WET in commands
|
||||||
public Task SendMessage(string text) => Client.SendTextMessageAsync(
|
public Task SendMessage(string text) => Client.SendTextMessageAsync(
|
||||||
|
|||||||
@ -117,7 +117,7 @@ namespace JetKarmaBot.Services.Handling
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Feature feature = new Feature();
|
Feature feature = new Feature();
|
||||||
ctx.Features.Add(feature);
|
ctx.AddFeature(feature);
|
||||||
|
|
||||||
await next(ctx);
|
await next(ctx);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user