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");
|
||||
CommandString cmd = ctx.Command;
|
||||
Feature feature = new Feature() { Router = this };
|
||||
ctx.Features.Add(feature);
|
||||
ctx.AddFeature(feature);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
@ -13,8 +13,8 @@ namespace JetKarmaBot.Services.Handling
|
||||
{
|
||||
using (var db = Db.GetContext())
|
||||
{
|
||||
ctx.Features.Add(db); // KarmaContext
|
||||
ctx.Features.Add(Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id))?.Locale ?? "ru-ru"]); // Locale
|
||||
ctx.AddFeature(db); // KarmaContext
|
||||
ctx.AddFeature(Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id))?.Locale ?? "ru-ru"]); // Locale
|
||||
await next(ctx);
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
@ -14,15 +14,16 @@ namespace JetKarmaBot.Services.Handling
|
||||
public ITelegramBotClient Client { get; }
|
||||
public MessageEventArgs EventArgs { 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)
|
||||
{
|
||||
Client = client;
|
||||
EventArgs = args;
|
||||
Command = cmd;
|
||||
}
|
||||
public object GetService(Type serviceType) => Features.First(x => x.GetType() == serviceType);
|
||||
public T GetFeature<T>() => (T)Features.First(x => x is T);
|
||||
public object GetService(Type serviceType) => Features[serviceType];
|
||||
public T GetFeature<T>() => (T)Features[typeof(T)];
|
||||
public void AddFeature<T>(T feat) => Features[typeof(T)] = feat;
|
||||
|
||||
//Method to reduce WET in commands
|
||||
public Task SendMessage(string text) => Client.SendTextMessageAsync(
|
||||
|
||||
@ -117,7 +117,7 @@ namespace JetKarmaBot.Services.Handling
|
||||
return;
|
||||
}
|
||||
Feature feature = new Feature();
|
||||
ctx.Features.Add(feature);
|
||||
ctx.AddFeature(feature);
|
||||
|
||||
await next(ctx);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user