mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
feat(store): code cleanup, make some fund with "unlucky" members
This commit is contained in:
@@ -15,8 +15,9 @@ namespace Kruzya.TelegramBot.Core.Service;
|
|||||||
|
|
||||||
public class UserService
|
public class UserService
|
||||||
{
|
{
|
||||||
private readonly List<long> _superUsers;
|
private readonly IEnumerable<long> _superUsers;
|
||||||
private readonly Dictionary<long, string> _statusMap;
|
private readonly IEnumerable<long> _badLuckUsers;
|
||||||
|
private readonly Dictionary<long, string?> _statusMap;
|
||||||
private readonly ICacheStorage<long, User> _userCache;
|
private readonly ICacheStorage<long, User> _userCache;
|
||||||
private readonly ITelegramBotClient _bot;
|
private readonly ITelegramBotClient _bot;
|
||||||
private readonly ILogger<UserService> _logger;
|
private readonly ILogger<UserService> _logger;
|
||||||
@@ -28,15 +29,13 @@ public class UserService
|
|||||||
ICacheStorage<long, User> userCache,
|
ICacheStorage<long, User> userCache,
|
||||||
ILogger<UserService> logger)
|
ILogger<UserService> logger)
|
||||||
{
|
{
|
||||||
_superUsers = configuration.GetSection("SuperUsers")
|
_superUsers = configuration.GetSection("SuperUsers").Get<List<long>>() ?? new List<long>();
|
||||||
.GetChildren()
|
_badLuckUsers = configuration.GetSection("BadLuckUsers").Get<List<long>>() ?? new List<long>();
|
||||||
.Select(q => Convert.ToInt64(q.Value))
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
_statusMap = configuration.GetSection("CustomMemberStatus")
|
_statusMap = configuration.GetSection("CustomMemberStatus")
|
||||||
.GetChildren()
|
.GetChildren()
|
||||||
.ToDictionary(x => Convert.ToInt64(x.Key), x => x.Value);
|
.ToDictionary(x => Convert.ToInt64(x.Key), x => x.Value);
|
||||||
|
|
||||||
_userCache = userCache;
|
_userCache = userCache;
|
||||||
_bot = bot.BotClient;
|
_bot = bot.BotClient;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
@@ -83,6 +82,11 @@ public class UserService
|
|||||||
return _superUsers.Contains(userId);
|
return _superUsers.Contains(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsUserUnlucky(long userId)
|
||||||
|
{
|
||||||
|
return _badLuckUsers.Contains(userId);
|
||||||
|
}
|
||||||
|
|
||||||
public string? GetUserCustomStatus(long userId)
|
public string? GetUserCustomStatus(long userId)
|
||||||
{
|
{
|
||||||
return _statusMap.ContainsKey(userId) ? _statusMap[userId] : null;
|
return _statusMap.ContainsKey(userId) ? _statusMap[userId] : null;
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ public class Store : CommonHandler
|
|||||||
|
|
||||||
private async Task PerformPurchase(IRandomMediaService mediaService, int messageId)
|
private async Task PerformPurchase(IRandomMediaService mediaService, int messageId)
|
||||||
{
|
{
|
||||||
var randomMedia = await mediaService!.GetRandomMediaAsync();
|
var randomMedia = await mediaService!.GetRandomMediaAsync(Chat, From);
|
||||||
var file = randomMedia.File;
|
var file = randomMedia.File;
|
||||||
|
|
||||||
switch (randomMedia.Type)
|
switch (randomMedia.Type)
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ public abstract class AbstractNsfwService : IRandomMediaService
|
|||||||
{
|
{
|
||||||
private readonly AllowNsfw _allowNsfw;
|
private readonly AllowNsfw _allowNsfw;
|
||||||
public abstract StoreItem StoreItem { get; }
|
public abstract StoreItem StoreItem { get; }
|
||||||
public abstract Task<RandomMedia> GetRandomMediaAsync();
|
public abstract Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user);
|
||||||
|
|
||||||
protected AbstractNsfwService(AllowNsfw allowNsfw)
|
protected AbstractNsfwService(AllowNsfw allowNsfw)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public class AnimBoobsService : AbstractNsfwService
|
|||||||
_httpClient.BaseAddress = new Uri("https://westdev.me/_boobs/");
|
_httpClient.BaseAddress = new Uri("https://westdev.me/_boobs/");
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task<RandomMedia> GetRandomMediaAsync()
|
public override async Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user)
|
||||||
{
|
{
|
||||||
var fileName = await _httpClient.GetStringAsync("index.php");
|
var fileName = await _httpClient.GetStringAsync("index.php");
|
||||||
// https://bugs.telegram.org/c/23674 workaround
|
// https://bugs.telegram.org/c/23674 workaround
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ public abstract class AnimalAsAService : IRandomMediaService
|
|||||||
_httpClient.BaseAddress = baseAddress;
|
_httpClient.BaseAddress = baseAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<RandomMedia> GetRandomMediaAsync()
|
public async Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user)
|
||||||
{
|
{
|
||||||
var resp = await _httpClient.GetAsync("images/search");
|
var resp = await _httpClient.GetAsync("images/search");
|
||||||
var catImage = JsonConvert.DeserializeObject<List<AnimalImage>>(await resp.Content.ReadAsStringAsync())![0];
|
var catImage = JsonConvert.DeserializeObject<List<AnimalImage>>(await resp.Content.ReadAsStringAsync())![0];
|
||||||
|
|||||||
@@ -1,13 +1,31 @@
|
|||||||
using Telegram.Bot.Types;
|
using System.Security.Cryptography;
|
||||||
|
using Kruzya.TelegramBot.Core.Service;
|
||||||
|
using Telegram.Bot.Types;
|
||||||
using West.TelegramBot.ContentStore.Model;
|
using West.TelegramBot.ContentStore.Model;
|
||||||
|
|
||||||
namespace West.TelegramBot.ContentStore.Service;
|
namespace West.TelegramBot.ContentStore.Service;
|
||||||
|
|
||||||
public class CapyApiService : IRandomMediaService
|
public class CapyApiService : IRandomMediaService
|
||||||
{
|
{
|
||||||
|
private readonly UserService _userService;
|
||||||
|
private const int BeaverCount = 161;
|
||||||
|
|
||||||
public StoreItem StoreItem { get; } = new("capybara", "Капибара", 60, 120);
|
public StoreItem StoreItem { get; } = new("capybara", "Капибара", 60, 120);
|
||||||
public Task<RandomMedia> GetRandomMediaAsync()
|
public Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user)
|
||||||
{
|
{
|
||||||
return Task.FromResult(new RandomMedia(new InputFileUrl($"https://api.capy.lol/v1/capybara?{new Random().Next(0, 10000)}")));
|
if (_userService.IsUserUnlucky(user.Id) && RandomNumberGenerator.GetInt32(2) == 1)
|
||||||
|
{
|
||||||
|
var beaverId = RandomNumberGenerator.GetInt32(BeaverCount);
|
||||||
|
return Task.FromResult(RandomMediaFromUrl($"https://westdev.me/_kurwa/{beaverId}.jpg"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.FromResult(RandomMediaFromUrl($"https://api.capy.lol/v1/capybara?{new Random().Next(0, 10000)}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public RandomMedia RandomMediaFromUrl(string url) => new(new InputFileUrl(url));
|
||||||
|
|
||||||
|
public CapyApiService(UserService userService)
|
||||||
|
{
|
||||||
|
_userService = userService;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,6 @@ public interface IRandomMediaService
|
|||||||
{
|
{
|
||||||
public StoreItem StoreItem { get; }
|
public StoreItem StoreItem { get; }
|
||||||
|
|
||||||
public Task<RandomMedia> GetRandomMediaAsync();
|
public Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user);
|
||||||
public Task<bool> CanView(Chat chat, User user) => Task.FromResult(true);
|
public Task<bool> CanView(Chat chat, User user) => Task.FromResult(true);
|
||||||
}
|
}
|
||||||
@@ -20,7 +20,7 @@ public abstract class AbstractKittiesService : IRandomMediaService
|
|||||||
_allowNsfw = allowNsfw;
|
_allowNsfw = allowNsfw;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<RandomMedia> GetRandomMediaAsync()
|
public async Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user)
|
||||||
{
|
{
|
||||||
var response = await _api.GetKittiesImageAsync(VideoType);
|
var response = await _api.GetKittiesImageAsync(VideoType);
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public class OBoobsService : AbstractNsfwService
|
|||||||
_httpClient.BaseAddress = new Uri("http://api.oboobs.ru");
|
_httpClient.BaseAddress = new Uri("http://api.oboobs.ru");
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task<RandomMedia> GetRandomMediaAsync()
|
public override async Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user)
|
||||||
{
|
{
|
||||||
// "/boobs/{start=0; sql offset}/{count=1; sql limit}/{order=-id;[id,rank,-rank,interest,-interest,random]}/
|
// "/boobs/{start=0; sql offset}/{count=1; sql limit}/{order=-id;[id,rank,-rank,interest,-interest,random]}/
|
||||||
var httpResp = await _httpClient.GetAsync("boobs/1/1/random");
|
var httpResp = await _httpClient.GetAsync("boobs/1/1/random");
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public abstract class SomeRandomApiAbstractService : IRandomMediaService
|
|||||||
_api = api;
|
_api = api;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<RandomMedia> GetRandomMediaAsync()
|
public async Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user)
|
||||||
{
|
{
|
||||||
var image = await _api.GetAnimalAsync(StoreItem.Id);
|
var image = await _api.GetAnimalAsync(StoreItem.Id);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user