[core] Massive setting system update

- Get rid of attribute crap. Now only interface is required
- Rename canUse to isAdmin in `Bot.IsUserAdminAsync` to match the actual meaning of variable
- Made `AnswerQuery` reusable by moving it from ContentStore to Core's CommonHandler
- Chat Settings edit frontend (inline keyboard)
- Rename NsfwOption to AllowNsfw and move it to ContentStore module
- Remove option backend testing commands
- Add KarmaMode option as an example of Select option type
This commit is contained in:
Andriy
2023-07-21 17:14:11 +03:00
parent 467ccd4fde
commit 27ddfd39cc
21 changed files with 307 additions and 110 deletions
+4
View File
@@ -1,5 +1,7 @@
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Options;
using Microsoft.Extensions.DependencyInjection;
using West.TelegramBot.ContentStore.Option;
using West.TelegramBot.ContentStore.Service;
namespace West.TelegramBot.ContentStore;
@@ -22,6 +24,8 @@ public class ContentStore : Module
public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IOption, AllowNsfw>();
foreach (var serviceType in MediaServiceTypes)
{
services.AddScoped(typeof(IRandomMediaService), serviceType);
+1 -1
View File
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
+4 -9
View File
@@ -1,26 +1,26 @@
#nullable enable
using BotFramework;
using BotFramework.Attributes;
using BotFramework.Enums;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Kruzya.TelegramBot.Core.Handler;
using Kruzya.TelegramBot.Core.Service;
using Microsoft.Extensions.DependencyInjection;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
using West.TelegramBot.ContentStore.Service;
namespace West.TelegramBot.ContentStore.Handler;
public class Store : BotEventHandler
public class Store : CommonHandler
{
private readonly IReputation? _reputation;
private readonly IServiceProvider _provider;
private readonly ContentStore _store;
public Store(IServiceProvider provider, ContentStore store)
public Store(CoreContext db, IServiceProvider provider, ContentStore store) : base(db)
{
_reputation = provider.GetService<IReputation>();
_provider = provider;
@@ -164,9 +164,4 @@ public class Store : BotEventHandler
return mediaService != null;
}
private async Task AnswerQuery(string id, string? message = null)
{
await Bot.AnswerCallbackQueryAsync(id, message);
}
}
+15
View File
@@ -0,0 +1,15 @@
using Kruzya.TelegramBot.Core.Options;
namespace West.TelegramBot.ContentStore.Option;
public class AllowNsfw : AbstractOption<AllowNsfw, bool>
{
public override string OptionId => "allowNSFW";
public override string OptionName => "NSFW";
public override OptionType OptionType => OptionType.OnOff;
public override bool DefaultValue => false;
public AllowNsfw(IOptionProvider optionProvider) : base(optionProvider)
{
}
}
-2
View File
@@ -5,7 +5,6 @@ global using GuessCache = Kruzya.TelegramBot.Core.Cache.MemoryCache<Telegram.Bot
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Options;
using Microsoft.Extensions.DependencyInjection;
using West.Entertainment.Options;
namespace West.Entertainment
@@ -17,7 +16,6 @@ namespace West.Entertainment
public override void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IGuessCache, GuessCache>();
services.AddScoped<NsfwOption>();
}
}
}
+1 -19
View File
@@ -12,23 +12,20 @@ using Kruzya.TelegramBot.Core.Service;
using Microsoft.Extensions.Logging;
using Telegram.Bot;
using Telegram.Bot.Types.Enums;
using West.Entertainment.Options;
namespace West.Entertainment.Handler
{
public class Common : BotEventHandler
{
private readonly UserService _userService;
private readonly NsfwOption _nsfwOption;
private readonly ILogger _logger;
private readonly CoreContext _db;
public Common(CoreContext db, ILogger<Common> logger, UserService userService, NsfwOption nsfwOption)
public Common(CoreContext db, ILogger<Common> logger, UserService userService)
{
_db = db;
_logger = logger;
_userService = userService;
_nsfwOption = nsfwOption;
}
[Command(InChat.Public, "cooldowns", CommandParseMode.Both)]
@@ -62,20 +59,5 @@ namespace West.Entertainment.Handler
await Bot.SendTextMessageAsync(Chat.Id, msg.ToString(), parseMode: ParseMode.Html);
}
[Command("test_option_get")]
public async Task HandleTestOptionGet()
{
await Bot.SendTextMessageAsync(Chat.Id, $"nsfw option is: {await _nsfwOption.GetValueAsync(Chat.Id)}");
}
[Command("test_option_set")]
public async Task HandleTestOptionSet()
{
var value = await _nsfwOption.GetValueAsync(Chat.Id);
await _nsfwOption.SetValueAsync(Chat.Id, !value);
await Bot.SendTextMessageAsync(Chat.Id, $"nsfw option is: {await _nsfwOption.GetValueAsync(Chat.Id)}");
}
}
}
@@ -1,11 +0,0 @@
using Kruzya.TelegramBot.Core.Options;
namespace West.Entertainment.Options;
[Option("allowNSFW", false)]
public class NsfwOption : AbstractOption<NsfwOption, bool>
{
public NsfwOption(IOptionProvider optionProvider) : base(optionProvider)
{
}
}
+21
View File
@@ -0,0 +1,21 @@
using Kruzya.TelegramBot.Core.Options;
namespace West.TelegramBot.Reputation.Options;
public class KarmaMode : AbstractOption<KarmaMode, string>
{
public override string OptionId => "karmaMode";
public override string OptionName => "Karma Mode";
public override OptionType OptionType => OptionType.Select;
public override string DefaultValue => "geometric";
public KarmaMode(IOptionProvider optionProvider) : base(optionProvider)
{
ChoiceList.AddRange(new []
{
new SelectChoice("geometric", "Geometric", "G"),
new SelectChoice("linear", "Linear", "L")
});
}
}
+3
View File
@@ -1,9 +1,11 @@
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Options;
using Kruzya.TelegramBot.Core.Service;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using West.TelegramBot.Reputation.Data;
using West.TelegramBot.Reputation.Options;
using West.TelegramBot.Reputation.Service;
namespace West.TelegramBot.Reputation;
@@ -20,5 +22,6 @@ public class Reputation : Module
services.AddDbContext<ReputationContext>(options => options.UseMySql(dsn, ServerVersion.AutoDetect(dsn)));
services.AddScoped<IReputation, ReputationService>();
services.AddScoped<IOption, KarmaMode>();
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>