mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 17:28:27 -04:00
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
caf9409192 |
@@ -113,7 +113,6 @@ docker-build:
|
||||
# Use the official docker image.
|
||||
image: docker:latest
|
||||
stage: build
|
||||
allow_failure: true
|
||||
services:
|
||||
- docker:dind
|
||||
before_script:
|
||||
|
61
CHANGELOG.md
61
CHANGELOG.md
@@ -1,67 +1,8 @@
|
||||
|
||||
# Changelog
|
||||
|
||||
Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.com/en/1.0.0/) except date format. a-c-f-r-o
|
||||
|
||||
## [4.2.12] - 30.06.2022
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed `.trivia --pokemon` showing incorrect pokemons
|
||||
|
||||
## [4.2.11] - 29.06.2022
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed `.draw` command
|
||||
|
||||
## [4.2.10] - 29.06.2022
|
||||
|
||||
- Fixed currency generation working only once
|
||||
|
||||
## [4.2.9] - 25.06.2022
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed `creds_example.yml` misssing from output directory
|
||||
|
||||
## [4.2.8] - 24.06.2022
|
||||
|
||||
### Fixed
|
||||
|
||||
- `.timely` should be fixed
|
||||
|
||||
## [4.2.7] - 24.06.2022
|
||||
|
||||
### Changed
|
||||
|
||||
- New cache abstraction added
|
||||
- 2 implemenations: redis and memory
|
||||
- All current bots will stay on redis cache, all new bots will use **in-process memory cache by default**
|
||||
- This change removes bot's hard dependency on redis
|
||||
- Configurable in `creds.yml` (please read the comments)
|
||||
- You **MUST** use 'redis' if your bot runs on more than 1 shard (2000+ servers)
|
||||
- [dev] Using new non-locking ConcurrentDictionary
|
||||
|
||||
### Fixed
|
||||
|
||||
- `.xp` will now show default user avatars too
|
||||
|
||||
### Removed
|
||||
|
||||
- Removed `.imagesreload` as images are now lazily loaded on request and then cached
|
||||
|
||||
## [4.2.6] - 22.06.2022
|
||||
|
||||
### Fixed
|
||||
|
||||
- Patron system should now properly by disabled on selfhosts by default.
|
||||
|
||||
## [4.2.5] - 18.06.2022
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed `.crypto`, you will still need coinmarketcapApiKey in `creds.yml` in order to make it run consistently as the key is shared
|
||||
|
||||
## [4.2.3] - 17.06.2022
|
||||
|
||||
### Fixed
|
||||
|
@@ -64,9 +64,7 @@ public class CmdAttribute : System.Attribute
|
||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
||||
{
|
||||
// #if DEBUG
|
||||
// if (!Debugger.IsAttached)
|
||||
// Debugger.Launch();
|
||||
// // SpinWait.SpinUntil(() => Debugger.IsAttached);
|
||||
// SpinWait.SpinUntil(() => Debugger.IsAttached);
|
||||
// #endif
|
||||
context.RegisterPostInitializationOutput(static ctx => ctx.AddSource(
|
||||
"CmdAttribute.g.cs",
|
||||
@@ -159,9 +157,7 @@ public class CmdAttribute : System.Attribute
|
||||
.Distinct();
|
||||
|
||||
var methodModels = methods
|
||||
.Select(x => MethodDeclarationToMethodModel(compilation, x!))
|
||||
.Where(static x => x is not null)
|
||||
.Cast<MethodModel>();
|
||||
.Select(x => MethodDeclarationToMethodModel(compilation, x!));
|
||||
|
||||
var groups = methodModels
|
||||
.GroupBy(static x => $"{x.Namespace}.{string.Join(".", x.Classes)}");
|
||||
@@ -181,7 +177,7 @@ public class CmdAttribute : System.Attribute
|
||||
var model = new FileModel(
|
||||
methods: elems,
|
||||
ns: elems[0].Namespace,
|
||||
classHierarchy: elems![0].Classes
|
||||
classHierarchy: elems[0].Classes
|
||||
);
|
||||
|
||||
models.Add(model);
|
||||
@@ -191,21 +187,11 @@ public class CmdAttribute : System.Attribute
|
||||
return models;
|
||||
}
|
||||
|
||||
private static MethodModel? MethodDeclarationToMethodModel(Compilation comp, MethodDeclarationSyntax decl)
|
||||
private static MethodModel MethodDeclarationToMethodModel(Compilation comp, MethodDeclarationSyntax decl)
|
||||
{
|
||||
// SpinWait.SpinUntil(static () => Debugger.IsAttached);
|
||||
|
||||
SemanticModel semanticModel;
|
||||
try
|
||||
{
|
||||
semanticModel = comp.GetSemanticModel(decl.SyntaxTree);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// for some reason this method can throw "Not part of this compilation" argument exception
|
||||
return null;
|
||||
}
|
||||
|
||||
var semanticModel = comp.GetSemanticModel(decl.SyntaxTree);
|
||||
var methodModel = new MethodModel(
|
||||
@params: decl.ParameterList.Parameters
|
||||
.Where(p => p.Type is not null)
|
||||
|
@@ -21,7 +21,7 @@ namespace NadekoBot.Tests
|
||||
var stringsSource = new LocalFileStringsSource(
|
||||
responsesPath,
|
||||
commandsPath);
|
||||
var strings = new MemoryBotStringsProvider(stringsSource);
|
||||
var strings = new LocalBotStringsProvider(stringsSource);
|
||||
|
||||
var culture = new CultureInfo("en-US");
|
||||
|
||||
|
@@ -1,93 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace NadekoBot.Tests;
|
||||
|
||||
public class ConcurrentHashSetTests
|
||||
{
|
||||
private ConcurrentHashSet<(int?, int?)> _set;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_set = new();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddTest()
|
||||
{
|
||||
var result = _set.Add((1, 2));
|
||||
|
||||
Assert.AreEqual(true, result);
|
||||
|
||||
result = _set.Add((1, 2));
|
||||
|
||||
Assert.AreEqual(false, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TryRemoveTest()
|
||||
{
|
||||
_set.Add((1, 2));
|
||||
var result = _set.TryRemove((1, 2));
|
||||
|
||||
Assert.AreEqual(true, result);
|
||||
|
||||
result = _set.TryRemove((1, 2));
|
||||
Assert.AreEqual(false, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountTest()
|
||||
{
|
||||
_set.Add((1, 2)); // 1
|
||||
_set.Add((1, 2)); // 1
|
||||
|
||||
_set.Add((2, 2)); // 2
|
||||
|
||||
_set.Add((3, 2)); // 3
|
||||
_set.Add((3, 2)); // 3
|
||||
|
||||
Assert.AreEqual(3, _set.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ClearTest()
|
||||
{
|
||||
_set.Add((1, 2));
|
||||
_set.Add((1, 3));
|
||||
_set.Add((1, 4));
|
||||
|
||||
_set.Clear();
|
||||
|
||||
Assert.AreEqual(0, _set.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ContainsTest()
|
||||
{
|
||||
_set.Add((1, 2));
|
||||
_set.Add((3, 2));
|
||||
|
||||
Assert.AreEqual(true, _set.Contains((1, 2)));
|
||||
Assert.AreEqual(true, _set.Contains((3, 2)));
|
||||
Assert.AreEqual(false, _set.Contains((2, 1)));
|
||||
Assert.AreEqual(false, _set.Contains((2, 3)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveWhereTest()
|
||||
{
|
||||
_set.Add((1, 2));
|
||||
_set.Add((1, 3));
|
||||
_set.Add((1, 4));
|
||||
_set.Add((2, 5));
|
||||
|
||||
// remove tuples which have even second item
|
||||
_set.RemoveWhere(static x => x.Item2 % 2 == 0);
|
||||
|
||||
Assert.AreEqual(2, _set.Count);
|
||||
Assert.AreEqual(true, _set.Contains((1, 3)));
|
||||
Assert.AreEqual(true, _set.Contains((2, 5)));
|
||||
}
|
||||
}
|
@@ -102,20 +102,20 @@ public sealed class Bot
|
||||
var svcs = new ServiceCollection().AddTransient(_ => _credsProvider.GetCreds()) // bot creds
|
||||
.AddSingleton(_credsProvider)
|
||||
.AddSingleton(_db) // database
|
||||
.AddRedis(_creds.RedisOptions) // redis
|
||||
.AddSingleton(Client) // discord socket client
|
||||
.AddSingleton(_commandService)
|
||||
// .AddSingleton(_interactionService)
|
||||
.AddSingleton(this)
|
||||
.AddSingleton<ISeria, JsonSeria>()
|
||||
.AddSingleton<IPubSub, RedisPubSub>()
|
||||
.AddSingleton<IConfigSeria, YamlSeria>()
|
||||
.AddBotStringsServices(_creds.TotalShards)
|
||||
.AddConfigServices()
|
||||
.AddConfigMigrators()
|
||||
.AddMemoryCache()
|
||||
// music
|
||||
.AddMusic()
|
||||
// cache
|
||||
.AddCache(_creds);
|
||||
|
||||
.AddMusic();
|
||||
// admin
|
||||
#if GLOBAL_NADEKO
|
||||
svcs.AddSingleton<ILogCommandService, DummyLogCommandService>();
|
||||
@@ -143,6 +143,13 @@ public sealed class Bot
|
||||
.AddSingleton<IReadyExecutor>(x => x.GetRequiredService<RemoteGrpcCoordinator>());
|
||||
}
|
||||
|
||||
svcs.AddSingleton<RedisLocalDataCache>()
|
||||
.AddSingleton<ILocalDataCache>(x => x.GetRequiredService<RedisLocalDataCache>())
|
||||
.AddSingleton<RedisImagesCache>()
|
||||
.AddSingleton<IImageCache>(x => x.GetRequiredService<RedisImagesCache>())
|
||||
.AddSingleton<IReadyExecutor>(x => x.GetRequiredService<RedisImagesCache>())
|
||||
.AddSingleton<IDataCache, RedisCache>();
|
||||
|
||||
svcs.Scan(scan => scan.FromAssemblyOf<IReadyExecutor>()
|
||||
.AddClasses(classes => classes.AssignableToAny(
|
||||
// services
|
||||
|
@@ -15,24 +15,22 @@ public sealed class RatelimitAttribute : PreconditionAttribute
|
||||
Seconds = seconds;
|
||||
}
|
||||
|
||||
public override async Task<PreconditionResult> CheckPermissionsAsync(
|
||||
public override Task<PreconditionResult> CheckPermissionsAsync(
|
||||
ICommandContext context,
|
||||
CommandInfo command,
|
||||
IServiceProvider services)
|
||||
{
|
||||
if (Seconds == 0)
|
||||
return PreconditionResult.FromSuccess();
|
||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||
|
||||
var cache = services.GetRequiredService<IBotCache>();
|
||||
var rem = await cache.GetRatelimitAsync(
|
||||
new($"precondition:{context.User.Id}:{command.Name}"),
|
||||
Seconds.Seconds());
|
||||
var cache = services.GetRequiredService<IDataCache>();
|
||||
var rem = cache.TryAddRatelimit(context.User.Id, command.Name, Seconds);
|
||||
|
||||
if (rem is null)
|
||||
return PreconditionResult.FromSuccess();
|
||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||
|
||||
var msgContent = $"You can use this command again in {rem.Value.TotalSeconds:F1}s.";
|
||||
|
||||
return PreconditionResult.FromError(msgContent);
|
||||
return Task.FromResult(PreconditionResult.FromError(msgContent));
|
||||
}
|
||||
}
|
@@ -1,46 +0,0 @@
|
||||
using OneOf;
|
||||
using OneOf.Types;
|
||||
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public static class BotCacheExtensions
|
||||
{
|
||||
public static async ValueTask<T?> GetOrDefaultAsync<T>(this IBotCache cache, TypedKey<T> key)
|
||||
{
|
||||
var result = await cache.GetAsync(key);
|
||||
if (result.TryGetValue(out var val))
|
||||
return val;
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
private static TypedKey<byte[]> GetImgKey(Uri uri)
|
||||
=> new($"image:{uri}");
|
||||
|
||||
public static ValueTask SetImageDataAsync(this IBotCache c, string key, byte[] data)
|
||||
=> c.SetImageDataAsync(new Uri(key), data);
|
||||
public static async ValueTask SetImageDataAsync(this IBotCache c, Uri key, byte[] data)
|
||||
=> await c.AddAsync(GetImgKey(key), data, expiry: TimeSpan.FromHours(48));
|
||||
|
||||
public static async ValueTask<OneOf<byte[], None>> GetImageDataAsync(this IBotCache c, Uri key)
|
||||
=> await c.GetAsync(GetImgKey(key));
|
||||
|
||||
public static async Task<TimeSpan?> GetRatelimitAsync(
|
||||
this IBotCache c,
|
||||
TypedKey<long> key,
|
||||
TimeSpan length)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
var nowB = now.ToBinary();
|
||||
|
||||
var cachedValue = await c.GetOrAddAsync(key,
|
||||
() => Task.FromResult(now.ToBinary()),
|
||||
expiry: length);
|
||||
|
||||
if (cachedValue == nowB)
|
||||
return null;
|
||||
|
||||
var diff = now - DateTime.FromBinary(cachedValue);
|
||||
return length - diff;
|
||||
}
|
||||
}
|
@@ -1,47 +0,0 @@
|
||||
using OneOf;
|
||||
using OneOf.Types;
|
||||
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public interface IBotCache
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds an item to the cache
|
||||
/// </summary>
|
||||
/// <param name="key">Key to add</param>
|
||||
/// <param name="value">Value to add to the cache</param>
|
||||
/// <param name="expiry">Optional expiry</param>
|
||||
/// <param name="overwrite">Whether old value should be overwritten</param>
|
||||
/// <typeparam name="T">Type of the value</typeparam>
|
||||
/// <returns>Returns whether add was sucessful. Always true unless ovewrite = false</returns>
|
||||
ValueTask<bool> AddAsync<T>(TypedKey<T> key, T value, TimeSpan? expiry = null, bool overwrite = true);
|
||||
|
||||
/// <summary>
|
||||
/// Get an element from the cache
|
||||
/// </summary>
|
||||
/// <param name="key">Key</param>
|
||||
/// <typeparam name="T">Type of the value</typeparam>
|
||||
/// <returns>Either a value or <see cref="None"/></returns>
|
||||
ValueTask<OneOf<T, None>> GetAsync<T>(TypedKey<T> key);
|
||||
|
||||
/// <summary>
|
||||
/// Remove a key from the cache
|
||||
/// </summary>
|
||||
/// <param name="key">Key to remove</param>
|
||||
/// <typeparam name="T">Type of the value</typeparam>
|
||||
/// <returns>Whether there was item</returns>
|
||||
ValueTask<bool> RemoveAsync<T>(TypedKey<T> key);
|
||||
|
||||
/// <summary>
|
||||
/// Get the key if it exists or add a new one
|
||||
/// </summary>
|
||||
/// <param name="key">Key to get and potentially add</param>
|
||||
/// <param name="createFactory">Value creation factory</param>
|
||||
/// <param name="expiry">Optional expiry</param>
|
||||
/// <typeparam name="T">Type of the value</typeparam>
|
||||
/// <returns>The retrieved or newly added value</returns>
|
||||
ValueTask<T?> GetOrAddAsync<T>(
|
||||
TypedKey<T> key,
|
||||
Func<Task<T?>> createFactory,
|
||||
TimeSpan? expiry = null);
|
||||
}
|
@@ -1,71 +0,0 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using OneOf;
|
||||
using OneOf.Types;
|
||||
|
||||
// ReSharper disable InconsistentlySynchronizedField
|
||||
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public sealed class MemoryBotCache : IBotCache
|
||||
{
|
||||
// needed for overwrites and Delete return value
|
||||
private readonly object _cacheLock = new object();
|
||||
private readonly MemoryCache _cache;
|
||||
|
||||
public MemoryBotCache()
|
||||
{
|
||||
_cache = new MemoryCache(new MemoryCacheOptions());
|
||||
}
|
||||
|
||||
public ValueTask<bool> AddAsync<T>(TypedKey<T> key, T value, TimeSpan? expiry = null, bool overwrite = true)
|
||||
{
|
||||
if (overwrite)
|
||||
{
|
||||
using var item = _cache.CreateEntry(key.Key);
|
||||
item.Value = value;
|
||||
item.AbsoluteExpirationRelativeToNow = expiry;
|
||||
return new(true);
|
||||
}
|
||||
|
||||
lock (_cacheLock)
|
||||
{
|
||||
if (_cache.TryGetValue(key.Key, out var old) && old is not null)
|
||||
return new(false);
|
||||
|
||||
using var item = _cache.CreateEntry(key.Key);
|
||||
item.Value = value;
|
||||
item.AbsoluteExpirationRelativeToNow = expiry;
|
||||
return new(true);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<T?> GetOrAddAsync<T>(
|
||||
TypedKey<T> key,
|
||||
Func<Task<T?>> createFactory,
|
||||
TimeSpan? expiry = null)
|
||||
=> await _cache.GetOrCreateAsync(key.Key,
|
||||
async ce =>
|
||||
{
|
||||
ce.AbsoluteExpirationRelativeToNow = expiry;
|
||||
var val = await createFactory();
|
||||
return val;
|
||||
});
|
||||
|
||||
public ValueTask<OneOf<T, None>> GetAsync<T>(TypedKey<T> key)
|
||||
{
|
||||
if (!_cache.TryGetValue(key.Key, out var val) || val is null)
|
||||
return new(new None());
|
||||
|
||||
return new((T)val);
|
||||
}
|
||||
|
||||
public ValueTask<bool> RemoveAsync<T>(TypedKey<T> key)
|
||||
{
|
||||
lock (_cacheLock)
|
||||
{
|
||||
var toReturn = _cache.TryGetValue(key.Key, out var old ) && old is not null;
|
||||
_cache.Remove(key.Key);
|
||||
return new(toReturn);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,119 +0,0 @@
|
||||
using OneOf;
|
||||
using OneOf.Types;
|
||||
using StackExchange.Redis;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public sealed class RedisBotCache : IBotCache
|
||||
{
|
||||
private static readonly Type[] _supportedTypes = new []
|
||||
{
|
||||
typeof(bool), typeof(int), typeof(uint), typeof(long),
|
||||
typeof(ulong), typeof(float), typeof(double),
|
||||
typeof(string), typeof(byte[]), typeof(ReadOnlyMemory<byte>), typeof(Memory<byte>),
|
||||
typeof(RedisValue),
|
||||
};
|
||||
|
||||
private static readonly JsonSerializerOptions _opts = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
NumberHandling = JsonNumberHandling.AllowReadingFromString,
|
||||
AllowTrailingCommas = true,
|
||||
IgnoreReadOnlyProperties = false,
|
||||
};
|
||||
private readonly ConnectionMultiplexer _conn;
|
||||
|
||||
public RedisBotCache(ConnectionMultiplexer conn)
|
||||
{
|
||||
_conn = conn;
|
||||
}
|
||||
|
||||
public async ValueTask<bool> AddAsync<T>(TypedKey<T> key, T value, TimeSpan? expiry = null, bool overwrite = true)
|
||||
{
|
||||
// if a null value is passed, remove the key
|
||||
if (value is null)
|
||||
{
|
||||
await RemoveAsync(key);
|
||||
return false;
|
||||
}
|
||||
|
||||
var db = _conn.GetDatabase();
|
||||
RedisValue val = IsSupportedType(typeof(T))
|
||||
? RedisValue.Unbox(value)
|
||||
: JsonSerializer.Serialize(value, _opts);
|
||||
|
||||
var success = await db.StringSetAsync(key.Key,
|
||||
val,
|
||||
expiry: expiry,
|
||||
when: overwrite ? When.Always : When.NotExists);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
public bool IsSupportedType(Type type)
|
||||
{
|
||||
if (type.IsGenericType)
|
||||
{
|
||||
var typeDef = type.GetGenericTypeDefinition();
|
||||
if (typeDef == typeof(Nullable<>))
|
||||
return IsSupportedType(type.GenericTypeArguments[0]);
|
||||
}
|
||||
|
||||
foreach (var t in _supportedTypes)
|
||||
{
|
||||
if (type == t)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async ValueTask<OneOf<T, None>> GetAsync<T>(TypedKey<T> key)
|
||||
{
|
||||
var db = _conn.GetDatabase();
|
||||
var val = await db.StringGetAsync(key.Key);
|
||||
if (val == default)
|
||||
return new None();
|
||||
|
||||
if (IsSupportedType(typeof(T)))
|
||||
return (T)((IConvertible)val).ToType(typeof(T), null);
|
||||
|
||||
return JsonSerializer.Deserialize<T>(val.ToString(), _opts)!;
|
||||
}
|
||||
|
||||
public async ValueTask<bool> RemoveAsync<T>(TypedKey<T> key)
|
||||
{
|
||||
var db = _conn.GetDatabase();
|
||||
|
||||
return await db.KeyDeleteAsync(key.Key);
|
||||
}
|
||||
|
||||
public async ValueTask<T?> GetOrAddAsync<T>(TypedKey<T> key, Func<Task<T?>> createFactory, TimeSpan? expiry = null)
|
||||
{
|
||||
var result = await GetAsync(key);
|
||||
|
||||
return await result.Match<Task<T?>>(
|
||||
v => Task.FromResult<T?>(v),
|
||||
async _ =>
|
||||
{
|
||||
var factoryValue = await createFactory();
|
||||
|
||||
if (factoryValue is null)
|
||||
return default;
|
||||
|
||||
await AddAsync(key, factoryValue, expiry);
|
||||
|
||||
// get again to make sure it's the cached value
|
||||
// and not the late factory value, in case there's a race condition
|
||||
|
||||
var newResult = await GetAsync(key);
|
||||
|
||||
// it's fine to do this, it should blow up if something went wrong.
|
||||
return newResult.Match<T?>(
|
||||
v => v,
|
||||
_ => default);
|
||||
});
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
#nullable enable
|
||||
#nullable disable
|
||||
#pragma warning disable
|
||||
// License MIT
|
||||
// Source: https://github.com/i3arnon/ConcurrentHashSet
|
||||
@@ -7,23 +7,433 @@ using System.Diagnostics;
|
||||
|
||||
namespace System.Collections.Generic;
|
||||
|
||||
[DebuggerDisplay("{_backingStore.Count}")]
|
||||
/// <summary>
|
||||
/// Represents a thread-safe hash-based unique collection.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the items in the collection.</typeparam>
|
||||
/// <remarks>
|
||||
/// All public members of <see cref="ConcurrentHashSet{T}" /> are thread-safe and may be used
|
||||
/// concurrently from multiple threads.
|
||||
/// </remarks>
|
||||
[DebuggerDisplay("Count = {Count}")]
|
||||
public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T>
|
||||
{
|
||||
private readonly ConcurrentDictionary<T, bool> _backingStore;
|
||||
|
||||
private const int DEFAULT_CAPACITY = 31;
|
||||
private const int MAX_LOCK_NUMBER = 1024;
|
||||
|
||||
private static int DefaultConcurrencyLevel
|
||||
=> PlatformHelper.ProcessorCount;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value that indicates whether the <see cref="ConcurrentHashSet{T}" /> is empty.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// true if the <see cref="ConcurrentHashSet{T}" /> is empty; otherwise,
|
||||
/// false.
|
||||
/// </value>
|
||||
public bool IsEmpty
|
||||
{
|
||||
get
|
||||
{
|
||||
var acquiredLocks = 0;
|
||||
try
|
||||
{
|
||||
AcquireAllLocks(ref acquiredLocks);
|
||||
|
||||
for (var i = 0; i < tables.CountPerLock.Length; i++)
|
||||
{
|
||||
if (tables.CountPerLock[i] != 0)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
ReleaseLocks(0, acquiredLocks);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool ICollection<T>.IsReadOnly
|
||||
=> false;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of items contained in the
|
||||
/// <see
|
||||
/// cref="ConcurrentHashSet{T}" />
|
||||
/// .
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The number of items contained in the
|
||||
/// <see
|
||||
/// cref="ConcurrentHashSet{T}" />
|
||||
/// .
|
||||
/// </value>
|
||||
/// <remarks>
|
||||
/// Count has snapshot semantics and represents the number of items in the
|
||||
/// <see
|
||||
/// cref="ConcurrentHashSet{T}" />
|
||||
/// at the moment when Count was accessed.
|
||||
/// </remarks>
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
var count = 0;
|
||||
var acquiredLocks = 0;
|
||||
try
|
||||
{
|
||||
AcquireAllLocks(ref acquiredLocks);
|
||||
|
||||
for (var i = 0; i < tables.CountPerLock.Length; i++)
|
||||
count += tables.CountPerLock[i];
|
||||
}
|
||||
finally
|
||||
{
|
||||
ReleaseLocks(0, acquiredLocks);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly IEqualityComparer<T> _comparer;
|
||||
private readonly bool _growLockArray;
|
||||
|
||||
private int budget;
|
||||
private volatile Tables tables;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the
|
||||
/// <see
|
||||
/// cref="ConcurrentHashSet{T}" />
|
||||
/// class that is empty, has the default concurrency level, has the default initial capacity, and
|
||||
/// uses the default comparer for the item type.
|
||||
/// </summary>
|
||||
public ConcurrentHashSet()
|
||||
=> _backingStore = new();
|
||||
: this(DefaultConcurrencyLevel, DEFAULT_CAPACITY, true, EqualityComparer<T>.Default)
|
||||
{
|
||||
}
|
||||
|
||||
public ConcurrentHashSet(IEnumerable<T> values, IEqualityComparer<T>? comparer = null)
|
||||
=> _backingStore = new(values.Select(x => new KeyValuePair<T, bool>(x, true)), comparer);
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the
|
||||
/// <see
|
||||
/// cref="ConcurrentHashSet{T}" />
|
||||
/// class that is empty, has the specified concurrency level and capacity, and uses the default
|
||||
/// comparer for the item type.
|
||||
/// </summary>
|
||||
/// <param name="concurrencyLevel">
|
||||
/// The estimated number of threads that will update the
|
||||
/// <see cref="ConcurrentHashSet{T}" /> concurrently.
|
||||
/// </param>
|
||||
/// <param name="capacity">
|
||||
/// The initial number of elements that the
|
||||
/// <see
|
||||
/// cref="ConcurrentHashSet{T}" />
|
||||
/// can contain.
|
||||
/// </param>
|
||||
/// <exception cref="T:System.ArgumentOutOfRangeException">
|
||||
/// <paramref name="concurrencyLevel" /> is
|
||||
/// less than 1.
|
||||
/// </exception>
|
||||
/// <exception cref="T:System.ArgumentOutOfRangeException">
|
||||
/// <paramref name="capacity" /> is less than
|
||||
/// 0.
|
||||
/// </exception>
|
||||
public ConcurrentHashSet(int concurrencyLevel, int capacity)
|
||||
: this(concurrencyLevel, capacity, false, EqualityComparer<T>.Default)
|
||||
{
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
=> _backingStore.Keys.GetEnumerator();
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConcurrentHashSet{T}" />
|
||||
/// class that contains elements copied from the specified
|
||||
/// <see
|
||||
/// cref="T:System.Collections.IEnumerable{T}" />
|
||||
/// , has the default concurrency
|
||||
/// level, has the default initial capacity, and uses the default comparer for the item type.
|
||||
/// </summary>
|
||||
/// <param name="collection">
|
||||
/// The
|
||||
/// <see
|
||||
/// cref="T:System.Collections.IEnumerable{T}" />
|
||||
/// whose elements are copied to
|
||||
/// the new
|
||||
/// <see cref="ConcurrentHashSet{T}" />.
|
||||
/// </param>
|
||||
/// <exception cref="T:System.ArgumentNullException"><paramref name="collection" /> is a null reference.</exception>
|
||||
public ConcurrentHashSet(IEnumerable<T> collection)
|
||||
: this(collection, EqualityComparer<T>.Default)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConcurrentHashSet{T}" />
|
||||
/// class that is empty, has the specified concurrency level and capacity, and uses the specified
|
||||
/// <see cref="T:System.Collections.Generic.IEqualityComparer{T}" />.
|
||||
/// </summary>
|
||||
/// <param name="comparer">
|
||||
/// The <see cref="T:System.Collections.Generic.IEqualityComparer{T}" />
|
||||
/// implementation to use when comparing items.
|
||||
/// </param>
|
||||
/// <exception cref="T:System.ArgumentNullException"><paramref name="comparer" /> is a null reference.</exception>
|
||||
public ConcurrentHashSet(IEqualityComparer<T> comparer)
|
||||
: this(DefaultConcurrencyLevel, DEFAULT_CAPACITY, true, comparer)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConcurrentHashSet{T}" />
|
||||
/// class that contains elements copied from the specified
|
||||
/// <see
|
||||
/// cref="T:System.Collections.IEnumerable" />
|
||||
/// , has the default concurrency level, has the default
|
||||
/// initial capacity, and uses the specified
|
||||
/// <see cref="T:System.Collections.Generic.IEqualityComparer{T}" />.
|
||||
/// </summary>
|
||||
/// <param name="collection">
|
||||
/// The
|
||||
/// <see
|
||||
/// cref="T:System.Collections.IEnumerable{T}" />
|
||||
/// whose elements are copied to
|
||||
/// the new
|
||||
/// <see cref="ConcurrentHashSet{T}" />.
|
||||
/// </param>
|
||||
/// <param name="comparer">
|
||||
/// The <see cref="T:System.Collections.Generic.IEqualityComparer{T}" />
|
||||
/// implementation to use when comparing items.
|
||||
/// </param>
|
||||
/// <exception cref="T:System.ArgumentNullException">
|
||||
/// <paramref name="collection" /> is a null reference
|
||||
/// (Nothing in Visual Basic). -or-
|
||||
/// <paramref name="comparer" /> is a null reference (Nothing in Visual Basic).
|
||||
/// </exception>
|
||||
public ConcurrentHashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer)
|
||||
: this(comparer)
|
||||
{
|
||||
if (collection is null)
|
||||
throw new ArgumentNullException(nameof(collection));
|
||||
|
||||
InitializeFromCollection(collection);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConcurrentHashSet{T}" />
|
||||
/// class that contains elements copied from the specified <see cref="T:System.Collections.IEnumerable" />,
|
||||
/// has the specified concurrency level, has the specified initial capacity, and uses the specified
|
||||
/// <see cref="T:System.Collections.Generic.IEqualityComparer{T}" />.
|
||||
/// </summary>
|
||||
/// <param name="concurrencyLevel">
|
||||
/// The estimated number of threads that will update the
|
||||
/// <see cref="ConcurrentHashSet{T}" /> concurrently.
|
||||
/// </param>
|
||||
/// <param name="collection">
|
||||
/// The <see cref="T:System.Collections.IEnumerable{T}" /> whose elements are copied to the new
|
||||
/// <see cref="ConcurrentHashSet{T}" />.
|
||||
/// </param>
|
||||
/// <param name="comparer">
|
||||
/// The <see cref="T:System.Collections.Generic.IEqualityComparer{T}" /> implementation to use
|
||||
/// when comparing items.
|
||||
/// </param>
|
||||
/// <exception cref="T:System.ArgumentNullException">
|
||||
/// <paramref name="collection" /> is a null reference.
|
||||
/// -or-
|
||||
/// <paramref name="comparer" /> is a null reference.
|
||||
/// </exception>
|
||||
/// <exception cref="T:System.ArgumentOutOfRangeException">
|
||||
/// <paramref name="concurrencyLevel" /> is less than 1.
|
||||
/// </exception>
|
||||
public ConcurrentHashSet(int concurrencyLevel, IEnumerable<T> collection, IEqualityComparer<T> comparer)
|
||||
: this(concurrencyLevel, DEFAULT_CAPACITY, false, comparer)
|
||||
{
|
||||
if (collection is null)
|
||||
throw new ArgumentNullException(nameof(collection));
|
||||
if (comparer is null)
|
||||
throw new ArgumentNullException(nameof(comparer));
|
||||
|
||||
InitializeFromCollection(collection);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConcurrentHashSet{T}" />
|
||||
/// class that is empty, has the specified concurrency level, has the specified initial capacity, and
|
||||
/// uses the specified <see cref="T:System.Collections.Generic.IEqualityComparer{T}" />.
|
||||
/// </summary>
|
||||
/// <param name="concurrencyLevel">
|
||||
/// The estimated number of threads that will update the
|
||||
/// <see cref="ConcurrentHashSet{T}" /> concurrently.
|
||||
/// </param>
|
||||
/// <param name="capacity">
|
||||
/// The initial number of elements that the
|
||||
/// <see
|
||||
/// cref="ConcurrentHashSet{T}" />
|
||||
/// can contain.
|
||||
/// </param>
|
||||
/// <param name="comparer">
|
||||
/// The <see cref="T:System.Collections.Generic.IEqualityComparer{T}" />
|
||||
/// implementation to use when comparing items.
|
||||
/// </param>
|
||||
/// <exception cref="T:System.ArgumentOutOfRangeException">
|
||||
/// <paramref name="concurrencyLevel" /> is less than 1. -or-
|
||||
/// <paramref name="capacity" /> is less than 0.
|
||||
/// </exception>
|
||||
/// <exception cref="T:System.ArgumentNullException"><paramref name="comparer" /> is a null reference.</exception>
|
||||
public ConcurrentHashSet(int concurrencyLevel, int capacity, IEqualityComparer<T> comparer)
|
||||
: this(concurrencyLevel, capacity, false, comparer)
|
||||
{
|
||||
}
|
||||
|
||||
private ConcurrentHashSet(
|
||||
int concurrencyLevel,
|
||||
int capacity,
|
||||
bool growLockArray,
|
||||
IEqualityComparer<T> comparer)
|
||||
{
|
||||
if (concurrencyLevel < 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(concurrencyLevel));
|
||||
if (capacity < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(capacity));
|
||||
|
||||
// The capacity should be at least as large as the concurrency level. Otherwise, we would have locks that don't guard
|
||||
// any buckets.
|
||||
if (capacity < concurrencyLevel)
|
||||
capacity = concurrencyLevel;
|
||||
|
||||
var locks = new object[concurrencyLevel];
|
||||
for (var i = 0; i < locks.Length; i++)
|
||||
locks[i] = new();
|
||||
|
||||
var countPerLock = new int[locks.Length];
|
||||
var buckets = new Node[capacity];
|
||||
tables = new(buckets, locks, countPerLock);
|
||||
|
||||
_growLockArray = growLockArray;
|
||||
budget = buckets.Length / locks.Length;
|
||||
_comparer = comparer ?? throw new ArgumentNullException(nameof(comparer));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes all items from the <see cref="ConcurrentHashSet{T}" />.
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
var locksAcquired = 0;
|
||||
try
|
||||
{
|
||||
AcquireAllLocks(ref locksAcquired);
|
||||
|
||||
var newTables = new Tables(new Node[DEFAULT_CAPACITY], tables.Locks, new int[tables.CountPerLock.Length]);
|
||||
tables = newTables;
|
||||
budget = Math.Max(1, newTables.Buckets.Length / newTables.Locks.Length);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ReleaseLocks(0, locksAcquired);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the <see cref="ConcurrentHashSet{T}" /> contains the specified
|
||||
/// item.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to locate in the <see cref="ConcurrentHashSet{T}" />.</param>
|
||||
/// <returns>true if the <see cref="ConcurrentHashSet{T}" /> contains the item; otherwise, false.</returns>
|
||||
public bool Contains(T item)
|
||||
{
|
||||
var hashcode = _comparer.GetHashCode(item!);
|
||||
|
||||
// We must capture the _buckets field in a local variable. It is set to a new table on each table resize.
|
||||
var localTables = tables;
|
||||
|
||||
var bucketNo = GetBucket(hashcode, localTables.Buckets.Length);
|
||||
|
||||
// We can get away w/out a lock here.
|
||||
// The Volatile.Read ensures that the load of the fields of 'n' doesn't move before the load from buckets[i].
|
||||
var current = Volatile.Read(ref localTables.Buckets[bucketNo]);
|
||||
|
||||
while (current is not null)
|
||||
{
|
||||
if (hashcode == current.Hashcode && _comparer.Equals(current.Item, item))
|
||||
return true;
|
||||
|
||||
current = current.Next;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ICollection<T>.Add(T item)
|
||||
=> Add(item);
|
||||
|
||||
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
|
||||
{
|
||||
if (array is null)
|
||||
throw new ArgumentNullException(nameof(array));
|
||||
if (arrayIndex < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
|
||||
|
||||
var locksAcquired = 0;
|
||||
try
|
||||
{
|
||||
AcquireAllLocks(ref locksAcquired);
|
||||
|
||||
var count = 0;
|
||||
|
||||
for (var i = 0; i < tables.Locks.Length && count >= 0; i++)
|
||||
count += tables.CountPerLock[i];
|
||||
|
||||
if (array.Length - count < arrayIndex || count < 0) //"count" itself or "count + arrayIndex" can overflow
|
||||
{
|
||||
throw new ArgumentException(
|
||||
"The index is equal to or greater than the length of the array, or the number of elements in the set is greater than the available space from index to the end of the destination array.");
|
||||
}
|
||||
|
||||
CopyToItems(array, arrayIndex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ReleaseLocks(0, locksAcquired);
|
||||
}
|
||||
}
|
||||
|
||||
bool ICollection<T>.Remove(T item)
|
||||
=> TryRemove(item);
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
=> GetEnumerator();
|
||||
|
||||
/// <summary>
|
||||
/// Returns an enumerator that iterates through the
|
||||
/// <see
|
||||
/// cref="ConcurrentHashSet{T}" />
|
||||
/// .
|
||||
/// </summary>
|
||||
/// <returns>An enumerator for the <see cref="ConcurrentHashSet{T}" />.</returns>
|
||||
/// <remarks>
|
||||
/// The enumerator returned from the collection is safe to use concurrently with
|
||||
/// reads and writes to the collection, however it does not represent a moment-in-time snapshot
|
||||
/// of the collection. The contents exposed through the enumerator may contain modifications
|
||||
/// made to the collection after <see cref="GetEnumerator" /> was called.
|
||||
/// </remarks>
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
var buckets = tables.Buckets;
|
||||
|
||||
for (var i = 0; i < buckets.Length; i++)
|
||||
{
|
||||
// The Volatile.Read ensures that the load of the fields of 'current' doesn't move before the load from buckets[i].
|
||||
var current = Volatile.Read(ref buckets[i]);
|
||||
|
||||
while (current is not null)
|
||||
{
|
||||
yield return current.Item;
|
||||
current = current.Next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the specified item to the <see cref="ConcurrentHashSet{T}" />.
|
||||
/// </summary>
|
||||
@@ -37,57 +447,375 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
||||
/// contains too many items.
|
||||
/// </exception>
|
||||
public bool Add(T item)
|
||||
=> _backingStore.TryAdd(item, true);
|
||||
=> AddInternal(item, _comparer.GetHashCode(item), true);
|
||||
|
||||
void ICollection<T>.Add(T item)
|
||||
=> Add(item);
|
||||
|
||||
public void Clear()
|
||||
=> _backingStore.Clear();
|
||||
|
||||
public bool Contains(T item)
|
||||
=> _backingStore.ContainsKey(item);
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex)
|
||||
/// <summary>
|
||||
/// Attempts to remove the item from the <see cref="ConcurrentHashSet{T}" />.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to remove.</param>
|
||||
/// <returns>true if an item was removed successfully; otherwise, false.</returns>
|
||||
public bool TryRemove(T item)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(array);
|
||||
|
||||
if (arrayIndex < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
|
||||
|
||||
if (arrayIndex >= array.Length)
|
||||
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
|
||||
|
||||
CopyToInternal(array, arrayIndex);
|
||||
}
|
||||
|
||||
private void CopyToInternal(T[] array, int arrayIndex)
|
||||
{
|
||||
var len = array.Length;
|
||||
foreach (var (k, _) in _backingStore)
|
||||
var hashcode = _comparer.GetHashCode(item);
|
||||
while (true)
|
||||
{
|
||||
if (arrayIndex >= len)
|
||||
throw new IndexOutOfRangeException(nameof(arrayIndex));
|
||||
|
||||
array[arrayIndex++] = k;
|
||||
var localTables = tables;
|
||||
|
||||
GetBucketAndLockNo(hashcode,
|
||||
out var bucketNo,
|
||||
out var lockNo,
|
||||
localTables.Buckets.Length,
|
||||
localTables.Locks.Length);
|
||||
|
||||
lock (localTables.Locks[lockNo])
|
||||
{
|
||||
// If the table just got resized, we may not be holding the right lock, and must retry.
|
||||
// This should be a rare occurrence.
|
||||
if (localTables != tables)
|
||||
continue;
|
||||
|
||||
Node previous = null;
|
||||
for (var current = localTables.Buckets[bucketNo]; current is not null; current = current.Next)
|
||||
{
|
||||
Debug.Assert((previous is null && current == localTables.Buckets[bucketNo])
|
||||
|| previous!.Next == current);
|
||||
|
||||
if (hashcode == current.Hashcode && _comparer.Equals(current.Item, item))
|
||||
{
|
||||
if (previous is null)
|
||||
Volatile.Write(ref localTables.Buckets[bucketNo], current.Next);
|
||||
else
|
||||
previous.Next = current.Next;
|
||||
|
||||
localTables.CountPerLock[lockNo]--;
|
||||
return true;
|
||||
}
|
||||
|
||||
previous = current;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ICollection<T>.Remove(T item)
|
||||
=> TryRemove(item);
|
||||
|
||||
public bool TryRemove(T item)
|
||||
=> _backingStore.TryRemove(item, out _);
|
||||
|
||||
public void RemoveWhere(Func<T, bool> predicate)
|
||||
private void InitializeFromCollection(IEnumerable<T> collection)
|
||||
{
|
||||
foreach (var elem in this.Where(predicate))
|
||||
TryRemove(elem);
|
||||
foreach (var item in collection)
|
||||
AddInternal(item, _comparer.GetHashCode(item), false);
|
||||
|
||||
if (budget == 0)
|
||||
budget = tables.Buckets.Length / tables.Locks.Length;
|
||||
}
|
||||
|
||||
public int Count
|
||||
=> _backingStore.Count;
|
||||
private bool AddInternal(T item, int hashcode, bool acquireLock)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var localTables = tables;
|
||||
GetBucketAndLockNo(hashcode,
|
||||
out var bucketNo,
|
||||
out var lockNo,
|
||||
localTables.Buckets.Length,
|
||||
localTables.Locks.Length);
|
||||
|
||||
public bool IsReadOnly
|
||||
=> false;
|
||||
var resizeDesired = false;
|
||||
var lockTaken = false;
|
||||
try
|
||||
{
|
||||
if (acquireLock)
|
||||
Monitor.Enter(localTables.Locks[lockNo], ref lockTaken);
|
||||
|
||||
// If the table just got resized, we may not be holding the right lock, and must retry.
|
||||
// This should be a rare occurrence.
|
||||
if (localTables != tables)
|
||||
continue;
|
||||
|
||||
// Try to find this item in the bucket
|
||||
Node previous = null;
|
||||
for (var current = localTables.Buckets[bucketNo]; current is not null; current = current.Next)
|
||||
{
|
||||
Debug.Assert((previous is null && current == localTables.Buckets[bucketNo])
|
||||
|| previous!.Next == current);
|
||||
if (hashcode == current.Hashcode && _comparer.Equals(current.Item, item))
|
||||
return false;
|
||||
|
||||
previous = current;
|
||||
}
|
||||
|
||||
// The item was not found in the bucket. Insert the new item.
|
||||
Volatile.Write(ref localTables.Buckets[bucketNo], new(item, hashcode, localTables.Buckets[bucketNo]));
|
||||
checked
|
||||
{
|
||||
localTables.CountPerLock[lockNo]++;
|
||||
}
|
||||
|
||||
//
|
||||
// If the number of elements guarded by this lock has exceeded the budget, resize the bucket table.
|
||||
// It is also possible that GrowTable will increase the budget but won't resize the bucket table.
|
||||
// That happens if the bucket table is found to be poorly utilized due to a bad hash function.
|
||||
//
|
||||
if (localTables.CountPerLock[lockNo] > budget)
|
||||
resizeDesired = true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (lockTaken)
|
||||
Monitor.Exit(localTables.Locks[lockNo]);
|
||||
}
|
||||
|
||||
//
|
||||
// The fact that we got here means that we just performed an insertion. If necessary, we will grow the table.
|
||||
//
|
||||
// Concurrency notes:
|
||||
// - Notice that we are not holding any locks at when calling GrowTable. This is necessary to prevent deadlocks.
|
||||
// - As a result, it is possible that GrowTable will be called unnecessarily. But, GrowTable will obtain lock 0
|
||||
// and then verify that the table we passed to it as the argument is still the current table.
|
||||
//
|
||||
if (resizeDesired)
|
||||
GrowTable(localTables);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static int GetBucket(int hashcode, int bucketCount)
|
||||
{
|
||||
var bucketNo = (hashcode & 0x7fffffff) % bucketCount;
|
||||
Debug.Assert(bucketNo >= 0 && bucketNo < bucketCount);
|
||||
return bucketNo;
|
||||
}
|
||||
|
||||
private static void GetBucketAndLockNo(
|
||||
int hashcode,
|
||||
out int bucketNo,
|
||||
out int lockNo,
|
||||
int bucketCount,
|
||||
int lockCount)
|
||||
{
|
||||
bucketNo = (hashcode & 0x7fffffff) % bucketCount;
|
||||
lockNo = bucketNo % lockCount;
|
||||
|
||||
Debug.Assert(bucketNo >= 0 && bucketNo < bucketCount);
|
||||
Debug.Assert(lockNo >= 0 && lockNo < lockCount);
|
||||
}
|
||||
|
||||
private void GrowTable(Tables localTables)
|
||||
{
|
||||
const int maxArrayLength = 0X7FEFFFFF;
|
||||
var locksAcquired = 0;
|
||||
try
|
||||
{
|
||||
// The thread that first obtains _locks[0] will be the one doing the resize operation
|
||||
AcquireLocks(0, 1, ref locksAcquired);
|
||||
|
||||
// Make sure nobody resized the table while we were waiting for lock 0:
|
||||
if (localTables != tables)
|
||||
// We assume that since the table reference is different, it was already resized (or the budget
|
||||
// was adjusted). If we ever decide to do table shrinking, or replace the table for other reasons,
|
||||
// we will have to revisit this logic.
|
||||
return;
|
||||
|
||||
// Compute the (approx.) total size. Use an Int64 accumulation variable to avoid an overflow.
|
||||
long approxCount = 0;
|
||||
for (var i = 0; i < localTables.CountPerLock.Length; i++)
|
||||
approxCount += localTables.CountPerLock[i];
|
||||
|
||||
//
|
||||
// If the bucket array is too empty, double the budget instead of resizing the table
|
||||
//
|
||||
if (approxCount < localTables.Buckets.Length / 4)
|
||||
{
|
||||
budget = 2 * budget;
|
||||
if (budget < 0)
|
||||
budget = int.MaxValue;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Compute the new table size. We find the smallest integer larger than twice the previous table size, and not divisible by
|
||||
// 2,3,5 or 7. We can consider a different table-sizing policy in the future.
|
||||
var newLength = 0;
|
||||
var maximizeTableSize = false;
|
||||
try
|
||||
{
|
||||
checked
|
||||
{
|
||||
// Double the size of the buckets table and add one, so that we have an odd integer.
|
||||
newLength = (localTables.Buckets.Length * 2) + 1;
|
||||
|
||||
// Now, we only need to check odd integers, and find the first that is not divisible
|
||||
// by 3, 5 or 7.
|
||||
while (newLength % 3 == 0 || newLength % 5 == 0 || newLength % 7 == 0)
|
||||
newLength += 2;
|
||||
|
||||
Debug.Assert(newLength % 2 != 0);
|
||||
|
||||
if (newLength > maxArrayLength)
|
||||
maximizeTableSize = true;
|
||||
}
|
||||
}
|
||||
catch (OverflowException)
|
||||
{
|
||||
maximizeTableSize = true;
|
||||
}
|
||||
|
||||
if (maximizeTableSize)
|
||||
{
|
||||
newLength = maxArrayLength;
|
||||
|
||||
// We want to make sure that GrowTable will not be called again, since table is at the maximum size.
|
||||
// To achieve that, we set the budget to int.MaxValue.
|
||||
//
|
||||
// (There is one special case that would allow GrowTable() to be called in the future:
|
||||
// calling Clear() on the ConcurrentHashSet will shrink the table and lower the budget.)
|
||||
budget = int.MaxValue;
|
||||
}
|
||||
|
||||
// Now acquire all other locks for the table
|
||||
AcquireLocks(1, localTables.Locks.Length, ref locksAcquired);
|
||||
|
||||
var newLocks = localTables.Locks;
|
||||
|
||||
// Add more locks
|
||||
if (_growLockArray && localTables.Locks.Length < MAX_LOCK_NUMBER)
|
||||
{
|
||||
newLocks = new object[localTables.Locks.Length * 2];
|
||||
Array.Copy(localTables.Locks, 0, newLocks, 0, localTables.Locks.Length);
|
||||
for (var i = localTables.Locks.Length; i < newLocks.Length; i++)
|
||||
newLocks[i] = new();
|
||||
}
|
||||
|
||||
var newBuckets = new Node[newLength];
|
||||
var newCountPerLock = new int[newLocks.Length];
|
||||
|
||||
// Copy all data into a new table, creating new nodes for all elements
|
||||
for (var i = 0; i < localTables.Buckets.Length; i++)
|
||||
{
|
||||
var current = localTables.Buckets[i];
|
||||
while (current is not null)
|
||||
{
|
||||
var next = current.Next;
|
||||
GetBucketAndLockNo(current.Hashcode,
|
||||
out var newBucketNo,
|
||||
out var newLockNo,
|
||||
newBuckets.Length,
|
||||
newLocks.Length);
|
||||
|
||||
newBuckets[newBucketNo] = new(current.Item, current.Hashcode, newBuckets[newBucketNo]);
|
||||
|
||||
checked
|
||||
{
|
||||
newCountPerLock[newLockNo]++;
|
||||
}
|
||||
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
|
||||
// Adjust the budget
|
||||
budget = Math.Max(1, newBuckets.Length / newLocks.Length);
|
||||
|
||||
// Replace tables with the new versions
|
||||
tables = new(newBuckets, newLocks, newCountPerLock);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Release all locks that we took earlier
|
||||
ReleaseLocks(0, locksAcquired);
|
||||
}
|
||||
}
|
||||
|
||||
public int RemoveWhere(Func<T, bool> predicate)
|
||||
{
|
||||
var elems = this.Where(predicate);
|
||||
var removed = 0;
|
||||
foreach (var elem in elems)
|
||||
{
|
||||
if (TryRemove(elem))
|
||||
removed++;
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
private void AcquireAllLocks(ref int locksAcquired)
|
||||
{
|
||||
// First, acquire lock 0
|
||||
AcquireLocks(0, 1, ref locksAcquired);
|
||||
|
||||
// Now that we have lock 0, the _locks array will not change (i.e., grow),
|
||||
// and so we can safely read _locks.Length.
|
||||
AcquireLocks(1, tables.Locks.Length, ref locksAcquired);
|
||||
Debug.Assert(locksAcquired == tables.Locks.Length);
|
||||
}
|
||||
|
||||
private void AcquireLocks(int fromInclusive, int toExclusive, ref int locksAcquired)
|
||||
{
|
||||
Debug.Assert(fromInclusive <= toExclusive);
|
||||
var locks = tables.Locks;
|
||||
|
||||
for (var i = fromInclusive; i < toExclusive; i++)
|
||||
{
|
||||
var lockTaken = false;
|
||||
try
|
||||
{
|
||||
Monitor.Enter(locks[i], ref lockTaken);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (lockTaken)
|
||||
locksAcquired++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ReleaseLocks(int fromInclusive, int toExclusive)
|
||||
{
|
||||
Debug.Assert(fromInclusive <= toExclusive);
|
||||
|
||||
for (var i = fromInclusive; i < toExclusive; i++)
|
||||
Monitor.Exit(tables.Locks[i]);
|
||||
}
|
||||
|
||||
private void CopyToItems(T[] array, int index)
|
||||
{
|
||||
var buckets = tables.Buckets;
|
||||
for (var i = 0; i < buckets.Length; i++)
|
||||
for (var current = buckets[i]; current is not null; current = current.Next)
|
||||
{
|
||||
array[index] = current.Item;
|
||||
index++; //this should never flow, CopyToItems is only called when there's no overflow risk
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class Tables
|
||||
{
|
||||
public readonly Node[] Buckets;
|
||||
public readonly object[] Locks;
|
||||
|
||||
public volatile int[] CountPerLock;
|
||||
|
||||
public Tables(Node[] buckets, object[] locks, int[] countPerLock)
|
||||
{
|
||||
Buckets = buckets;
|
||||
Locks = locks;
|
||||
CountPerLock = countPerLock;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class Node
|
||||
{
|
||||
public readonly int Hashcode;
|
||||
public readonly T Item;
|
||||
|
||||
public volatile Node Next;
|
||||
|
||||
public Node(T item, int hashcode, Node next)
|
||||
{
|
||||
Item = item;
|
||||
Hashcode = hashcode;
|
||||
Next = next;
|
||||
}
|
||||
}
|
||||
}
|
@@ -12,7 +12,7 @@ namespace NadekoBot.Common.Configs;
|
||||
public sealed partial class BotConfig : ICloneable<BotConfig>
|
||||
{
|
||||
[Comment(@"DO NOT CHANGE")]
|
||||
public int Version { get; set; } = 3;
|
||||
public int Version { get; set; } = 2;
|
||||
|
||||
[Comment(@"Most commands, when executed, have a small colored line
|
||||
next to the response. The color depends whether the command
|
||||
@@ -182,4 +182,4 @@ public enum ConsoleOutputType
|
||||
Normal = 0,
|
||||
Simple = 1,
|
||||
None = 2
|
||||
}
|
||||
}
|
@@ -19,10 +19,7 @@ public sealed class Creds : IBotCredentials
|
||||
public bool UsePrivilegedIntents { get; set; }
|
||||
|
||||
[Comment(@"The number of shards that the bot will be running on.
|
||||
Leave at 1 if you don't know what you're doing.
|
||||
|
||||
note: If you are planning to have more than one shard, then you must change botCache to 'redis'.
|
||||
Also, in that case you should be using NadekoBot.Coordinator to start the bot, and it will correctly override this value.")]
|
||||
Leave at 1 if you don't know what you're doing.")]
|
||||
public int TotalShards { get; set; }
|
||||
|
||||
[Comment(
|
||||
@@ -53,14 +50,8 @@ go to https://www.patreon.com/portal -> my clients -> create client")]
|
||||
|
||||
[Comment(@"Official cleverbot api key.")]
|
||||
public string CleverbotApiKey { get; set; }
|
||||
|
||||
[Comment(@"Which cache implementation should bot use.
|
||||
'memory' - Cache will be in memory of the bot's process itself. Only use this on bots with a single shard. When the bot is restarted the cache is reset.
|
||||
'redis' - Uses redis (which needs to be separately downloaded and installed). The cache will persist through bot restarts. You can configure connection string in creds.yml")]
|
||||
public BotCacheImplemenation BotCache { get; set; }
|
||||
|
||||
[Comment(@"Redis connection string. Don't change if you don't know what you're doing.
|
||||
Only used if botCache is set to 'redis'")]
|
||||
|
||||
[Comment(@"Redis connection string. Don't change if you don't know what you're doing.")]
|
||||
public string RedisOptions { get; set; }
|
||||
|
||||
[Comment(@"Database options. Don't change if you don't know what you're doing. Leave null for default values")]
|
||||
@@ -113,12 +104,12 @@ Linux default
|
||||
args: ""NadekoBot.dll -- {0}""
|
||||
Windows default
|
||||
cmd: NadekoBot.exe
|
||||
args: ""{0}""")]
|
||||
args: {0}")]
|
||||
public RestartConfig RestartCommand { get; set; }
|
||||
|
||||
public Creds()
|
||||
{
|
||||
Version = 6;
|
||||
Version = 5;
|
||||
Token = string.Empty;
|
||||
UsePrivilegedIntents = true;
|
||||
OwnerIds = new List<ulong>();
|
||||
@@ -128,7 +119,6 @@ Windows default
|
||||
Patreon = new(string.Empty, string.Empty, string.Empty, string.Empty);
|
||||
BotListToken = string.Empty;
|
||||
CleverbotApiKey = string.Empty;
|
||||
BotCache = BotCacheImplemenation.Memory;
|
||||
RedisOptions = "localhost:6379,syncTimeout=30000,responseTimeout=30000,allowAdmin=true,password=";
|
||||
Db = new()
|
||||
{
|
||||
@@ -227,10 +217,4 @@ public class GoogleApiConfig
|
||||
{
|
||||
public string SearchId { get; init; }
|
||||
public string ImageSearchId { get; init; }
|
||||
}
|
||||
|
||||
public enum BotCacheImplemenation
|
||||
{
|
||||
Memory,
|
||||
Redis
|
||||
}
|
@@ -26,7 +26,6 @@ public interface IBotCredentials
|
||||
string TwitchClientId { get; set; }
|
||||
string TwitchClientSecret { get; set; }
|
||||
GoogleApiConfig Google { get; set; }
|
||||
BotCacheImplemenation BotCache { get; set; }
|
||||
}
|
||||
|
||||
public class RestartConfig
|
||||
|
@@ -1,11 +1,9 @@
|
||||
#nullable disable
|
||||
using NadekoBot.Common.Yml;
|
||||
using Cloneable;
|
||||
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
[Cloneable]
|
||||
public partial class ImageUrls : ICloneable<ImageUrls>
|
||||
public class ImageUrls
|
||||
{
|
||||
[Comment("DO NOT CHANGE")]
|
||||
public int Version { get; set; } = 3;
|
||||
|
@@ -1,10 +0,0 @@
|
||||
using OneOf.Types;
|
||||
using OneOf;
|
||||
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public static class OneOfExtensions
|
||||
{
|
||||
public static bool TryGetValue<T>(this OneOf<T, None> oneOf, out T value)
|
||||
=> oneOf.TryPickT0(out value, out _);
|
||||
}
|
@@ -1,12 +1,11 @@
|
||||
#nullable disable
|
||||
|
||||
using System.Text.Json.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NadekoBot.Common.Pokemon;
|
||||
|
||||
public class SearchPokemon
|
||||
{
|
||||
[JsonPropertyName("num")]
|
||||
[JsonProperty("num")]
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Species { get; set; }
|
||||
|
@@ -1,13 +0,0 @@
|
||||
#nullable disable
|
||||
using NadekoBot.Services.Database.Models;
|
||||
|
||||
namespace NadekoBot.Db.Models;
|
||||
|
||||
public class StreamOnlineMessage : DbEntity
|
||||
{
|
||||
public ulong ChannelId { get; set; }
|
||||
public ulong MessageId { get; set; }
|
||||
|
||||
public FollowedStream.FType Type { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
@@ -58,9 +58,6 @@ public abstract class NadekoContext : DbContext
|
||||
public DbSet<PatronUser> Patrons { get; set; }
|
||||
|
||||
public DbSet<PatronQuota> PatronQuotas { get; set; }
|
||||
|
||||
public DbSet<StreamOnlineMessage> StreamOnlineMessages { get; set; }
|
||||
|
||||
|
||||
#region Mandatory Provider-Specific Values
|
||||
|
||||
|
@@ -1,5 +1,4 @@
|
||||
// global using System.Collections.Concurrent;
|
||||
global using NonBlocking;
|
||||
global using System.Collections.Concurrent;
|
||||
|
||||
// packages
|
||||
global using Serilog;
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,39 +0,0 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace NadekoBot.Migrations.Mysql
|
||||
{
|
||||
public partial class stondeldbcache : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "streamonlinemessages",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
|
||||
channelid = table.Column<ulong>(type: "bigint unsigned", nullable: false),
|
||||
messageid = table.Column<ulong>(type: "bigint unsigned", nullable: false),
|
||||
type = table.Column<int>(type: "int", nullable: false),
|
||||
name = table.Column<string>(type: "longtext", nullable: true)
|
||||
.Annotation("MySql:CharSet", "utf8mb4"),
|
||||
dateadded = table.Column<DateTime>(type: "datetime(6)", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("pk_streamonlinemessages", x => x.id);
|
||||
})
|
||||
.Annotation("MySql:CharSet", "utf8mb4");
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "streamonlinemessages");
|
||||
}
|
||||
}
|
||||
}
|
@@ -16,7 +16,7 @@ namespace NadekoBot.Migrations.Mysql
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "6.0.6")
|
||||
.HasAnnotation("ProductVersion", "6.0.5")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 64);
|
||||
|
||||
modelBuilder.Entity("NadekoBot.Db.Models.BankUser", b =>
|
||||
@@ -333,39 +333,6 @@ namespace NadekoBot.Migrations.Mysql
|
||||
b.ToTable("patrons", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("NadekoBot.Db.Models.StreamOnlineMessage", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("id");
|
||||
|
||||
b.Property<ulong>("ChannelId")
|
||||
.HasColumnType("bigint unsigned")
|
||||
.HasColumnName("channelid");
|
||||
|
||||
b.Property<DateTime?>("DateAdded")
|
||||
.HasColumnType("datetime(6)")
|
||||
.HasColumnName("dateadded");
|
||||
|
||||
b.Property<ulong>("MessageId")
|
||||
.HasColumnType("bigint unsigned")
|
||||
.HasColumnName("messageid");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.HasColumnType("longtext")
|
||||
.HasColumnName("name");
|
||||
|
||||
b.Property<int>("Type")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("type");
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_streamonlinemessages");
|
||||
|
||||
b.ToTable("streamonlinemessages", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("NadekoBot.Services.Database.Models.AntiAltSetting", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,37 +0,0 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace NadekoBot.Migrations.PostgreSql
|
||||
{
|
||||
public partial class stondeldbcache : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "streamonlinemessages",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<int>(type: "integer", nullable: false)
|
||||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||
channelid = table.Column<decimal>(type: "numeric(20,0)", nullable: false),
|
||||
messageid = table.Column<decimal>(type: "numeric(20,0)", nullable: false),
|
||||
type = table.Column<int>(type: "integer", nullable: false),
|
||||
name = table.Column<string>(type: "text", nullable: true),
|
||||
dateadded = table.Column<DateTime>(type: "timestamp with time zone", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("pk_streamonlinemessages", x => x.id);
|
||||
});
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "streamonlinemessages");
|
||||
}
|
||||
}
|
||||
}
|
@@ -17,7 +17,7 @@ namespace NadekoBot.Migrations.PostgreSql
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "6.0.6")
|
||||
.HasAnnotation("ProductVersion", "6.0.5")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
@@ -343,41 +343,6 @@ namespace NadekoBot.Migrations.PostgreSql
|
||||
b.ToTable("patrons", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("NadekoBot.Db.Models.StreamOnlineMessage", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<decimal>("ChannelId")
|
||||
.HasColumnType("numeric(20,0)")
|
||||
.HasColumnName("channelid");
|
||||
|
||||
b.Property<DateTime?>("DateAdded")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("dateadded");
|
||||
|
||||
b.Property<decimal>("MessageId")
|
||||
.HasColumnType("numeric(20,0)")
|
||||
.HasColumnName("messageid");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("name");
|
||||
|
||||
b.Property<int>("Type")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("type");
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_streamonlinemessages");
|
||||
|
||||
b.ToTable("streamonlinemessages", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("NadekoBot.Services.Database.Models.AntiAltSetting", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,36 +0,0 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace NadekoBot.Migrations
|
||||
{
|
||||
public partial class stondeldbcache : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "StreamOnlineMessages",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
ChannelId = table.Column<ulong>(type: "INTEGER", nullable: false),
|
||||
MessageId = table.Column<ulong>(type: "INTEGER", nullable: false),
|
||||
Type = table.Column<int>(type: "INTEGER", nullable: false),
|
||||
Name = table.Column<string>(type: "TEXT", nullable: false),
|
||||
DateAdded = table.Column<DateTime>(type: "TEXT", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_StreamOnlineMessages", x => x.Id);
|
||||
});
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "StreamOnlineMessages");
|
||||
}
|
||||
}
|
||||
}
|
@@ -15,7 +15,7 @@ namespace NadekoBot.Migrations
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "6.0.6");
|
||||
modelBuilder.HasAnnotation("ProductVersion", "6.0.5");
|
||||
|
||||
modelBuilder.Entity("NadekoBot.Db.Models.BankUser", b =>
|
||||
{
|
||||
@@ -262,33 +262,6 @@ namespace NadekoBot.Migrations
|
||||
b.ToTable("Patrons");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("NadekoBot.Db.Models.StreamOnlineMessage", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<ulong>("ChannelId")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<DateTime?>("DateAdded")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<ulong>("MessageId")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("Type")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("StreamOnlineMessages");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("NadekoBot.Services.Database.Models.AntiAltSetting", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
|
@@ -500,6 +500,14 @@ public partial class Administration
|
||||
await ReplyConfirmLocalizedAsync(strs.message_sent);
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
[OwnerOnly]
|
||||
public async partial Task ImagesReload()
|
||||
{
|
||||
await _service.ReloadImagesAsync();
|
||||
await ReplyConfirmLocalizedAsync(strs.images_loading);
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
[OwnerOnly]
|
||||
public async partial Task StringsReload()
|
||||
|
@@ -20,6 +20,7 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService
|
||||
|
||||
private ConcurrentDictionary<ulong?, ConcurrentDictionary<int, Timer>> autoCommands = new();
|
||||
|
||||
private readonly IImageCache _imgs;
|
||||
private readonly IHttpClientFactory _httpFactory;
|
||||
private readonly BotConfigService _bss;
|
||||
private readonly IPubSub _pubSub;
|
||||
@@ -27,6 +28,7 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService
|
||||
|
||||
//keys
|
||||
private readonly TypedKey<ActivityPubData> _activitySetKey;
|
||||
private readonly TypedKey<bool> _imagesReloadKey;
|
||||
private readonly TypedKey<string> _guildLeaveKey;
|
||||
|
||||
public SelfService(
|
||||
@@ -35,6 +37,7 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService
|
||||
DbService db,
|
||||
IBotStrings strings,
|
||||
IBotCredentials creds,
|
||||
IDataCache cache,
|
||||
IHttpClientFactory factory,
|
||||
BotConfigService bss,
|
||||
IPubSub pubSub,
|
||||
@@ -45,15 +48,20 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService
|
||||
_strings = strings;
|
||||
_client = client;
|
||||
_creds = creds;
|
||||
_imgs = cache.LocalImages;
|
||||
_httpFactory = factory;
|
||||
_bss = bss;
|
||||
_pubSub = pubSub;
|
||||
_eb = eb;
|
||||
_activitySetKey = new("activity.set");
|
||||
_imagesReloadKey = new("images.reload");
|
||||
_guildLeaveKey = new("guild.leave");
|
||||
|
||||
HandleStatusChanges();
|
||||
|
||||
if (_client.ShardId == 0)
|
||||
_pubSub.Sub(_imagesReloadKey, async _ => await _imgs.Reload());
|
||||
|
||||
_pubSub.Sub(_guildLeaveKey,
|
||||
async input =>
|
||||
{
|
||||
@@ -317,6 +325,9 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService
|
||||
uow.SaveChanges();
|
||||
}
|
||||
|
||||
public Task ReloadImagesAsync()
|
||||
=> _pubSub.Pub(_imagesReloadKey, true);
|
||||
|
||||
public bool ForwardMessages()
|
||||
{
|
||||
var isForwarding = false;
|
||||
|
@@ -8,7 +8,7 @@ namespace NadekoBot.Modules.Administration.Services;
|
||||
public class VcRoleService : INService
|
||||
{
|
||||
public ConcurrentDictionary<ulong, ConcurrentDictionary<ulong, IRole>> VcRoles { get; }
|
||||
public ConcurrentDictionary<ulong, System.Collections.Concurrent.ConcurrentQueue<(bool, IGuildUser, IRole)>> ToAssign { get; }
|
||||
public ConcurrentDictionary<ulong, ConcurrentQueue<(bool, IGuildUser, IRole)>> ToAssign { get; }
|
||||
private readonly DbService _db;
|
||||
private readonly DiscordSocketClient _client;
|
||||
|
||||
@@ -37,7 +37,7 @@ public class VcRoleService : INService
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
Task Selector(System.Collections.Concurrent.ConcurrentQueue<(bool, IGuildUser, IRole)> queue)
|
||||
Task Selector(ConcurrentQueue<(bool, IGuildUser, IRole)> queue)
|
||||
{
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
@@ -203,7 +203,7 @@ public class VcRoleService : INService
|
||||
|
||||
private void Assign(bool v, SocketGuildUser gusr, IRole role)
|
||||
{
|
||||
var queue = ToAssign.GetOrAdd(gusr.Guild.Id, new System.Collections.Concurrent.ConcurrentQueue<(bool, IGuildUser, IRole)>());
|
||||
var queue = ToAssign.GetOrAdd(gusr.Guild.Id, new ConcurrentQueue<(bool, IGuildUser, IRole)>());
|
||||
queue.Enqueue((v, gusr, role));
|
||||
}
|
||||
}
|
@@ -19,8 +19,8 @@ public partial class Gambling
|
||||
private static readonly char[] _fateRolls = { '-', ' ', '+' };
|
||||
private readonly IImageCache _images;
|
||||
|
||||
public DiceRollCommands(ImageCache images)
|
||||
=> _images = images;
|
||||
public DiceRollCommands(IDataCache data)
|
||||
=> _images = data.LocalImages;
|
||||
|
||||
[Cmd]
|
||||
public async partial Task Roll()
|
||||
@@ -31,10 +31,10 @@ public partial class Gambling
|
||||
var num1 = gen / 10;
|
||||
var num2 = gen % 10;
|
||||
|
||||
using var img1 = await GetDiceAsync(num1);
|
||||
using var img2 = await GetDiceAsync(num2);
|
||||
using var img1 = GetDice(num1);
|
||||
using var img2 = GetDice(num2);
|
||||
using var img = new[] { img1, img2 }.Merge(out var format);
|
||||
await using var ms = await img.ToStreamAsync(format);
|
||||
await using var ms = img.ToStream(format);
|
||||
await ctx.Channel.SendFileAsync(ms,
|
||||
$"dice.{format.FileExtensions.First()}",
|
||||
Format.Bold(ctx.User.ToString()) + " " + GetText(strs.dice_rolled(Format.Code(gen.ToString()))));
|
||||
@@ -96,7 +96,7 @@ public partial class Gambling
|
||||
else
|
||||
toInsert = dice.Count;
|
||||
|
||||
dice.Insert(toInsert, await GetDiceAsync(randomNumber));
|
||||
dice.Insert(toInsert, GetDice(randomNumber));
|
||||
values.Insert(toInsert, randomNumber);
|
||||
}
|
||||
|
||||
@@ -195,19 +195,20 @@ public partial class Gambling
|
||||
await ReplyConfirmLocalizedAsync(strs.dice_rolled(Format.Bold(rolled.ToString())));
|
||||
}
|
||||
|
||||
private async Task<Image<Rgba32>> GetDiceAsync(int num)
|
||||
private Image<Rgba32> GetDice(int num)
|
||||
{
|
||||
if (num is < 0 or > 10)
|
||||
throw new ArgumentOutOfRangeException(nameof(num));
|
||||
|
||||
if (num == 10)
|
||||
{
|
||||
using var imgOne = Image.Load(await _images.GetDiceAsync(1));
|
||||
using var imgZero = Image.Load(await _images.GetDiceAsync(0));
|
||||
var images = _images.Dice;
|
||||
using var imgOne = Image.Load(images[1]);
|
||||
using var imgZero = Image.Load(images[0]);
|
||||
return new[] { imgOne, imgZero }.Merge();
|
||||
}
|
||||
|
||||
return Image.Load(await _images.GetDiceAsync(num));
|
||||
return Image.Load(_images.Dice[num]);
|
||||
}
|
||||
}
|
||||
}
|
@@ -14,8 +14,8 @@ public partial class Gambling
|
||||
private static readonly ConcurrentDictionary<IGuild, Deck> _allDecks = new();
|
||||
private readonly IImageCache _images;
|
||||
|
||||
public DrawCommands(IImageCache images)
|
||||
=> _images = images;
|
||||
public DrawCommands(IDataCache data)
|
||||
=> _images = data.LocalImages;
|
||||
|
||||
private async Task<(Stream ImageStream, string ToSend)> InternalDraw(int num, ulong? guildId = null)
|
||||
{
|
||||
@@ -43,8 +43,7 @@ public partial class Gambling
|
||||
|
||||
var currentCard = cards.Draw();
|
||||
cardObjects.Add(currentCard);
|
||||
var cardName = currentCard.ToString().ToLowerInvariant().Replace(' ', '_');
|
||||
images.Add(Image.Load(await File.ReadAllBytesAsync($"data/images/cards/{cardName}.jpg")));
|
||||
images.Add(Image.Load(_images.GetCard(currentCard.ToString().ToLowerInvariant().Replace(' ', '_'))));
|
||||
}
|
||||
|
||||
using var img = images.Merge();
|
||||
|
@@ -1,6 +1,5 @@
|
||||
#nullable disable
|
||||
using NadekoBot.Services.Database.Models;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace NadekoBot.Modules.Gambling.Common.Events;
|
||||
|
||||
|
@@ -20,7 +20,7 @@ public class ReactionEvent : ICurrencyEvent
|
||||
private readonly bool _isPotLimited;
|
||||
private readonly ITextChannel _channel;
|
||||
private readonly ConcurrentHashSet<ulong> _awardedUsers = new();
|
||||
private readonly System.Collections.Concurrent.ConcurrentQueue<ulong> _toAward = new();
|
||||
private readonly ConcurrentQueue<ulong> _toAward = new();
|
||||
private readonly Timer _t;
|
||||
private readonly Timer _timeout;
|
||||
private readonly bool _noRecentlyJoinedServer;
|
||||
|
@@ -25,17 +25,11 @@ public partial class Gambling
|
||||
private static readonly NadekoRandom _rng = new();
|
||||
private readonly IImageCache _images;
|
||||
private readonly ICurrencyService _cs;
|
||||
private readonly ImagesConfig _ic;
|
||||
|
||||
public FlipCoinCommands(
|
||||
IImageCache images,
|
||||
ImagesConfig ic,
|
||||
ICurrencyService cs,
|
||||
GamblingConfigService gss)
|
||||
public FlipCoinCommands(IDataCache data, ICurrencyService cs, GamblingConfigService gss)
|
||||
: base(gss)
|
||||
{
|
||||
_ic = ic;
|
||||
_images = images;
|
||||
_images = data.LocalImages;
|
||||
_cs = cs;
|
||||
}
|
||||
|
||||
@@ -53,8 +47,8 @@ public partial class Gambling
|
||||
var imgs = new Image<Rgba32>[count];
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var headsArr = await _images.GetHeadsImageAsync();
|
||||
var tailsArr = await _images.GetTailsImageAsync();
|
||||
var headsArr = _images.Heads[_rng.Next(0, _images.Heads.Count)];
|
||||
var tailsArr = _images.Tails[_rng.Next(0, _images.Tails.Count)];
|
||||
if (_rng.Next(0, 10) < 5)
|
||||
{
|
||||
imgs[i] = Image.Load(headsArr);
|
||||
@@ -100,7 +94,7 @@ public partial class Gambling
|
||||
|
||||
BetFlipGuess result;
|
||||
Uri imageToSend;
|
||||
var coins = _ic.Data.Coins;
|
||||
var coins = _images.ImageUrls.Coins;
|
||||
if (_rng.Next(0, 1000) <= 499)
|
||||
{
|
||||
imageToSend = coins.Heads[_rng.Next(0, coins.Heads.Length)];
|
||||
|
@@ -38,6 +38,7 @@ public partial class Gambling : GamblingModule<GamblingService>
|
||||
|
||||
private readonly DbService _db;
|
||||
private readonly ICurrencyService _cs;
|
||||
private readonly IDataCache _cache;
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly NumberFormatInfo _enUsCulture;
|
||||
private readonly DownloadTracker _tracker;
|
||||
@@ -50,6 +51,7 @@ public partial class Gambling : GamblingModule<GamblingService>
|
||||
public Gambling(
|
||||
DbService db,
|
||||
ICurrencyService currency,
|
||||
IDataCache cache,
|
||||
DiscordSocketClient client,
|
||||
DownloadTracker tracker,
|
||||
GamblingConfigService configService,
|
||||
@@ -59,6 +61,7 @@ public partial class Gambling : GamblingModule<GamblingService>
|
||||
{
|
||||
_db = db;
|
||||
_cs = currency;
|
||||
_cache = cache;
|
||||
_client = client;
|
||||
_bank = bank;
|
||||
_ps = ps;
|
||||
@@ -121,7 +124,7 @@ public partial class Gambling : GamblingModule<GamblingService>
|
||||
return;
|
||||
}
|
||||
|
||||
if (await _service.ClaimTimelyAsync(ctx.User.Id, period) is { } rem)
|
||||
if (_cache.AddTimelyClaim(ctx.User.Id, period) is { } rem)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
var relativeTag = TimestampTag.FromDateTime(now.Add(rem), TimestampTagStyles.Relative);
|
||||
@@ -142,7 +145,7 @@ public partial class Gambling : GamblingModule<GamblingService>
|
||||
[OwnerOnly]
|
||||
public async partial Task TimelyReset()
|
||||
{
|
||||
await _service.RemoveAllTimelyClaimsAsync();
|
||||
_cache.RemoveAllTimelyClaims();
|
||||
await ReplyConfirmLocalizedAsync(strs.timely_reset);
|
||||
}
|
||||
|
||||
|
@@ -1,13 +1,16 @@
|
||||
#nullable disable
|
||||
using LinqToDB;
|
||||
using LinqToDB.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NadekoBot.Common.ModuleBehaviors;
|
||||
using NadekoBot.Db;
|
||||
using NadekoBot.Db.Models;
|
||||
using NadekoBot.Migrations;
|
||||
using NadekoBot.Modules.Gambling.Common;
|
||||
using NadekoBot.Modules.Gambling.Common.Connect4;
|
||||
using NadekoBot.Modules.Gambling.Common.Slot;
|
||||
using NadekoBot.Modules.Gambling.Common.WheelOfFortune;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NadekoBot.Modules.Gambling.Services;
|
||||
|
||||
@@ -19,7 +22,7 @@ public class GamblingService : INService, IReadyExecutor
|
||||
private readonly ICurrencyService _cs;
|
||||
private readonly Bot _bot;
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly IBotCache _cache;
|
||||
private readonly IDataCache _cache;
|
||||
private readonly GamblingConfigService _gss;
|
||||
|
||||
public GamblingService(
|
||||
@@ -27,7 +30,7 @@ public class GamblingService : INService, IReadyExecutor
|
||||
Bot bot,
|
||||
ICurrencyService cs,
|
||||
DiscordSocketClient client,
|
||||
IBotCache cache,
|
||||
IDataCache cache,
|
||||
GamblingConfigService gss)
|
||||
{
|
||||
_db = db;
|
||||
@@ -70,7 +73,6 @@ public class GamblingService : INService, IReadyExecutor
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly TypedKey<long> _curDecayKey = new("currency:last_decay");
|
||||
private async Task CurrencyDecayLoopAsync()
|
||||
{
|
||||
if (_bot.Client.ShardId != 0)
|
||||
@@ -86,16 +88,11 @@ public class GamblingService : INService, IReadyExecutor
|
||||
if (config.Decay.Percent is <= 0 or > 1 || maxDecay < 0)
|
||||
continue;
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
await using var uow = _db.GetDbContext();
|
||||
var result = await _cache.GetAsync(_curDecayKey);
|
||||
var lastCurrencyDecay = _cache.GetLastCurrencyDecay();
|
||||
|
||||
if (result.TryPickT0(out var bin, out _)
|
||||
&& (now - DateTime.FromBinary(bin) < TimeSpan.FromHours(config.Decay.HourInterval)))
|
||||
{
|
||||
if (DateTime.UtcNow - lastCurrencyDecay < TimeSpan.FromHours(config.Decay.HourInterval))
|
||||
continue;
|
||||
}
|
||||
|
||||
Log.Information(@"Decaying users' currency - decay: {ConfigDecayPercent}%
|
||||
| max: {MaxDecay}
|
||||
@@ -118,9 +115,8 @@ public class GamblingService : INService, IReadyExecutor
|
||||
: old.CurrencyAmount - maxDecay
|
||||
});
|
||||
|
||||
_cache.SetLastCurrencyDecay();
|
||||
await uow.SaveChangesAsync();
|
||||
|
||||
await _cache.AddAsync(_curDecayKey, now.ToBinary());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -165,100 +161,60 @@ public class GamblingService : INService, IReadyExecutor
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private static readonly TypedKey<EconomyResult> _ecoKey = new("nadeko:economy");
|
||||
|
||||
public async Task<EconomyResult> GetEconomyAsync()
|
||||
{
|
||||
var data = await _cache.GetOrAddAsync(_ecoKey,
|
||||
async () =>
|
||||
if (_cache.TryGetEconomy(out var data))
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var uow = _db.GetDbContext();
|
||||
var cash = uow.DiscordUser.GetTotalCurrency();
|
||||
var onePercent = uow.DiscordUser.GetTopOnePercentCurrency(_client.CurrentUser.Id);
|
||||
decimal planted = uow.PlantedCurrency.AsQueryable().Sum(x => x.Amount);
|
||||
var waifus = uow.WaifuInfo.GetTotalValue();
|
||||
var bot = uow.DiscordUser.GetUserCurrency(_client.CurrentUser.Id);
|
||||
decimal bank = await uow.GetTable<BankUser>()
|
||||
.SumAsyncLinqToDB(x => x.Balance);
|
||||
return JsonConvert.DeserializeObject<EconomyResult>(data);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
var result = new EconomyResult
|
||||
{
|
||||
Cash = cash,
|
||||
Planted = planted,
|
||||
Bot = bot,
|
||||
Waifus = waifus,
|
||||
OnePercent = onePercent,
|
||||
Bank = bank
|
||||
};
|
||||
decimal cash;
|
||||
decimal onePercent;
|
||||
decimal planted;
|
||||
decimal waifus;
|
||||
decimal bank;
|
||||
long bot;
|
||||
|
||||
return result;
|
||||
},
|
||||
TimeSpan.FromMinutes(3));
|
||||
using (var uow = _db.GetDbContext())
|
||||
{
|
||||
cash = uow.DiscordUser.GetTotalCurrency();
|
||||
onePercent = uow.DiscordUser.GetTopOnePercentCurrency(_client.CurrentUser.Id);
|
||||
planted = uow.PlantedCurrency.AsQueryable().Sum(x => x.Amount);
|
||||
waifus = uow.WaifuInfo.GetTotalValue();
|
||||
bot = uow.DiscordUser.GetUserCurrency(_client.CurrentUser.Id);
|
||||
bank = await uow.GetTable<BankUser>()
|
||||
.SumAsyncLinqToDB(x => x.Balance);
|
||||
}
|
||||
|
||||
return data;
|
||||
var result = new EconomyResult
|
||||
{
|
||||
Cash = cash,
|
||||
Planted = planted,
|
||||
Bot = bot,
|
||||
Waifus = waifus,
|
||||
OnePercent = onePercent,
|
||||
Bank = bank
|
||||
};
|
||||
|
||||
_cache.SetEconomy(JsonConvert.SerializeObject(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
public Task<WheelOfFortuneGame.Result> WheelOfFortuneSpinAsync(ulong userId, long bet)
|
||||
=> new WheelOfFortuneGame(userId, bet, _gss.Data, _cs).SpinAsync();
|
||||
|
||||
|
||||
private static readonly SemaphoreSlim _timelyLock = new (1, 1);
|
||||
|
||||
private static TypedKey<Dictionary<ulong, long>> _timelyKey
|
||||
= new("timely:claims");
|
||||
public async Task<TimeSpan?> ClaimTimelyAsync(ulong userId, int period)
|
||||
public struct EconomyResult
|
||||
{
|
||||
if (period == 0)
|
||||
return null;
|
||||
|
||||
await _timelyLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
// get the dictionary from the cache or get a new one
|
||||
var dict = (await _cache.GetOrAddAsync(_timelyKey,
|
||||
() => Task.FromResult(new Dictionary<ulong, long>())))!;
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var nowB = now.ToBinary();
|
||||
|
||||
// try to get users last claim
|
||||
if (!dict.TryGetValue(userId, out var lastB))
|
||||
lastB = dict[userId] = now.ToBinary();
|
||||
|
||||
var diff = now - DateTime.FromBinary(lastB);
|
||||
|
||||
// if its now, or too long ago => success
|
||||
if (lastB == nowB || diff > period.Hours())
|
||||
{
|
||||
// update the cache
|
||||
dict[userId] = nowB;
|
||||
await _cache.AddAsync(_timelyKey, dict);
|
||||
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// otherwise return the remaining time
|
||||
return period.Hours() - diff;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_timelyLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RemoveAllTimelyClaimsAsync()
|
||||
=> await _cache.RemoveAsync(_timelyKey);
|
||||
|
||||
|
||||
public readonly struct EconomyResult
|
||||
{
|
||||
public decimal Cash { get; init; }
|
||||
public decimal Planted { get; init; }
|
||||
public decimal Waifus { get; init; }
|
||||
public decimal OnePercent { get; init; }
|
||||
public decimal Bank { get; init; }
|
||||
public long Bot { get; init; }
|
||||
public decimal Cash { get; set; }
|
||||
public decimal Planted { get; set; }
|
||||
public decimal Waifus { get; set; }
|
||||
public decimal OnePercent { get; set; }
|
||||
public decimal Bank { get; set; }
|
||||
public long Bot { get; set; }
|
||||
}
|
||||
}
|
@@ -16,7 +16,7 @@ namespace NadekoBot.Modules.Gambling.Services;
|
||||
public class PlantPickService : INService, IExecNoCommand
|
||||
{
|
||||
//channelId/last generation
|
||||
public ConcurrentDictionary<ulong, long> LastGenerations { get; } = new();
|
||||
public ConcurrentDictionary<ulong, DateTime> LastGenerations { get; } = new();
|
||||
private readonly DbService _db;
|
||||
private readonly IBotStrings _strings;
|
||||
private readonly IImageCache _images;
|
||||
@@ -34,7 +34,7 @@ public class PlantPickService : INService, IExecNoCommand
|
||||
DbService db,
|
||||
CommandHandler cmd,
|
||||
IBotStrings strings,
|
||||
IImageCache images,
|
||||
IDataCache cache,
|
||||
FontProvider fonts,
|
||||
ICurrencyService cs,
|
||||
CommandHandler cmdHandler,
|
||||
@@ -43,7 +43,7 @@ public class PlantPickService : INService, IExecNoCommand
|
||||
{
|
||||
_db = db;
|
||||
_strings = strings;
|
||||
_images = images;
|
||||
_images = cache.LocalImages;
|
||||
_fonts = fonts;
|
||||
_cs = cs;
|
||||
_cmdHandler = cmdHandler;
|
||||
@@ -110,21 +110,30 @@ public class PlantPickService : INService, IExecNoCommand
|
||||
/// <param name="pass">Optional password to add to top left corner.</param>
|
||||
/// <param name="extension">Extension of the file, defaults to png</param>
|
||||
/// <returns>Stream of the currency image</returns>
|
||||
public async Task<(Stream, string)> GetRandomCurrencyImageAsync(string pass)
|
||||
public Stream GetRandomCurrencyImage(string pass, out string extension)
|
||||
{
|
||||
var curImg = await _images.GetCurrencyImageAsync();
|
||||
// get a random currency image bytes
|
||||
var rng = new NadekoRandom();
|
||||
var curImg = _images.Currency[rng.Next(0, _images.Currency.Count)];
|
||||
|
||||
if (string.IsNullOrWhiteSpace(pass))
|
||||
{
|
||||
// determine the extension
|
||||
using var load = _ = Image.Load(curImg, out var format);
|
||||
using (_ = Image.Load(curImg, out var format))
|
||||
{
|
||||
extension = format.FileExtensions.FirstOrDefault() ?? "png";
|
||||
}
|
||||
|
||||
// return the image
|
||||
return (curImg.ToStream(), format.FileExtensions.FirstOrDefault() ?? "png");
|
||||
return curImg.ToStream();
|
||||
}
|
||||
|
||||
// get the image stream and extension
|
||||
return AddPassword(curImg, pass);
|
||||
var (s, ext) = AddPassword(curImg, pass);
|
||||
// set the out extension parameter to the extension we've got
|
||||
extension = ext;
|
||||
// return the image
|
||||
return s;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -175,15 +184,15 @@ public class PlantPickService : INService, IExecNoCommand
|
||||
try
|
||||
{
|
||||
var config = _gss.Data;
|
||||
var lastGeneration = LastGenerations.GetOrAdd(channel.Id, DateTime.MinValue.ToBinary());
|
||||
var lastGeneration = LastGenerations.GetOrAdd(channel.Id, DateTime.MinValue);
|
||||
var rng = new NadekoRandom();
|
||||
|
||||
if (DateTime.UtcNow - TimeSpan.FromSeconds(config.Generation.GenCooldown)
|
||||
< DateTime.FromBinary(lastGeneration)) //recently generated in this channel, don't generate again
|
||||
< lastGeneration) //recently generated in this channel, don't generate again
|
||||
return;
|
||||
|
||||
var num = rng.Next(1, 101) + (config.Generation.Chance * 100);
|
||||
if (num > 100 && LastGenerations.TryUpdate(channel.Id, DateTime.UtcNow.ToBinary(), lastGeneration))
|
||||
if (num > 100 && LastGenerations.TryUpdate(channel.Id, DateTime.UtcNow, lastGeneration))
|
||||
{
|
||||
var dropAmount = config.Generation.MinAmount;
|
||||
var dropAmountMax = config.Generation.MaxAmount;
|
||||
@@ -205,10 +214,10 @@ public class PlantPickService : INService, IExecNoCommand
|
||||
var pw = config.Generation.HasPassword ? GenerateCurrencyPassword().ToUpperInvariant() : null;
|
||||
|
||||
IUserMessage sent;
|
||||
var (stream, ext) = await GetRandomCurrencyImageAsync(pw);
|
||||
|
||||
await using (stream)
|
||||
await using (var stream = GetRandomCurrencyImage(pw, out var ext))
|
||||
{
|
||||
sent = await channel.SendFileAsync(stream, $"currency_image.{ext}", toSend);
|
||||
}
|
||||
|
||||
await AddPlantToDatabase(channel.GuildId,
|
||||
channel.Id,
|
||||
@@ -269,7 +278,7 @@ public class PlantPickService : INService, IExecNoCommand
|
||||
if (amount > 0)
|
||||
// give the picked currency to the user
|
||||
await _cs.AddAsync(uid, amount, new("currency", "collect"));
|
||||
await uow.SaveChangesAsync();
|
||||
uow.SaveChanges();
|
||||
}
|
||||
|
||||
try
|
||||
@@ -307,14 +316,11 @@ public class PlantPickService : INService, IExecNoCommand
|
||||
msgToSend += " " + GetText(gid, strs.pick_sn(prefix));
|
||||
|
||||
//get the image
|
||||
var (stream, ext) = await GetRandomCurrencyImageAsync(pass);
|
||||
await using var stream = GetRandomCurrencyImage(pass, out var ext);
|
||||
// send it
|
||||
await using (stream)
|
||||
{
|
||||
var msg = await ch.SendFileAsync(stream, $"img.{ext}", msgToSend);
|
||||
// return sent message's id (in order to be able to delete it when it's picked)
|
||||
return msg.Id;
|
||||
}
|
||||
var msg = await ch.SendFileAsync(stream, $"img.{ext}", msgToSend);
|
||||
// return sent message's id (in order to be able to delete it when it's picked)
|
||||
return msg.Id;
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
@@ -32,13 +32,13 @@ public partial class Gambling
|
||||
private readonly DbService _db;
|
||||
|
||||
public SlotCommands(
|
||||
ImageCache images,
|
||||
IDataCache data,
|
||||
FontProvider fonts,
|
||||
DbService db,
|
||||
GamblingConfigService gamb)
|
||||
: base(gamb)
|
||||
{
|
||||
_images = images;
|
||||
_images = data.LocalImages;
|
||||
_fonts = fonts;
|
||||
_db = db;
|
||||
}
|
||||
@@ -130,8 +130,7 @@ public partial class Gambling
|
||||
?? 0;
|
||||
}
|
||||
|
||||
var slotBg = await _images.GetSlotBgAsync();
|
||||
using (var bgImage = Image.Load<Rgba32>(slotBg, out _))
|
||||
using (var bgImage = Image.Load<Rgba32>(_images.SlotBackground, out _))
|
||||
{
|
||||
var numbers = new int[3];
|
||||
result.Rolls.CopyTo(numbers, 0);
|
||||
@@ -185,7 +184,7 @@ public partial class Gambling
|
||||
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
using var img = Image.Load(await _images.GetSlotEmojiAsync(numbers[i]));
|
||||
using var img = Image.Load(_images.SlotEmojis[numbers[i]]);
|
||||
bgImage.Mutate(x => x.DrawImage(img, new Point(148 + (105 * i), 217), 1f));
|
||||
}
|
||||
|
||||
@@ -202,7 +201,7 @@ public partial class Gambling
|
||||
msg = GetText(strs.slot_jackpot(30));
|
||||
}
|
||||
|
||||
await using (var imgStream = await bgImage.ToStreamAsync())
|
||||
await using (var imgStream = bgImage.ToStream())
|
||||
{
|
||||
await ctx.Channel.SendFileAsync(imgStream,
|
||||
"result.png",
|
||||
|
@@ -1,6 +1,5 @@
|
||||
#nullable disable
|
||||
using LinqToDB;
|
||||
using LinqToDB.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NadekoBot.Common.ModuleBehaviors;
|
||||
using NadekoBot.Db;
|
||||
@@ -15,7 +14,7 @@ public class WaifuService : INService, IReadyExecutor
|
||||
{
|
||||
private readonly DbService _db;
|
||||
private readonly ICurrencyService _cs;
|
||||
private readonly IBotCache _cache;
|
||||
private readonly IDataCache _cache;
|
||||
private readonly GamblingConfigService _gss;
|
||||
private readonly IBotCredentials _creds;
|
||||
private readonly DiscordSocketClient _client;
|
||||
@@ -23,7 +22,7 @@ public class WaifuService : INService, IReadyExecutor
|
||||
public WaifuService(
|
||||
DbService db,
|
||||
ICurrencyService cs,
|
||||
IBotCache cache,
|
||||
IDataCache cache,
|
||||
GamblingConfigService gss,
|
||||
IBotCredentials creds,
|
||||
DiscordSocketClient client)
|
||||
@@ -237,13 +236,8 @@ public class WaifuService : INService, IReadyExecutor
|
||||
var newAff = target is null ? null : uow.GetOrCreateUser(target);
|
||||
if (w?.Affinity?.UserId == target?.Id)
|
||||
{
|
||||
return (null, false, null);
|
||||
}
|
||||
|
||||
remaining = await _cache.GetRatelimitAsync(GetAffinityKey(user.Id),
|
||||
30.Minutes());
|
||||
|
||||
if (remaining is not null)
|
||||
else if (!_cache.TryAddAffinityCooldown(user.Id, out remaining))
|
||||
{
|
||||
}
|
||||
else if (w is null)
|
||||
@@ -300,12 +294,6 @@ public class WaifuService : INService, IReadyExecutor
|
||||
return uow.WaifuInfo.GetWaifuUserId(ownerId, name);
|
||||
}
|
||||
|
||||
private static TypedKey<long> GetDivorceKey(ulong userId)
|
||||
=> new($"waifu:divorce_cd:{userId}");
|
||||
|
||||
private static TypedKey<long> GetAffinityKey(ulong userId)
|
||||
=> new($"waifu:affinity:{userId}");
|
||||
|
||||
public async Task<(WaifuInfo, DivorceResult, long, TimeSpan?)> DivorceWaifuAsync(IUser user, ulong targetId)
|
||||
{
|
||||
DivorceResult result;
|
||||
@@ -317,15 +305,10 @@ public class WaifuService : INService, IReadyExecutor
|
||||
w = uow.WaifuInfo.ByWaifuUserId(targetId);
|
||||
if (w?.Claimer is null || w.Claimer.UserId != user.Id)
|
||||
result = DivorceResult.NotYourWife;
|
||||
else if (!_cache.TryAddDivorceCooldown(user.Id, out remaining))
|
||||
result = DivorceResult.Cooldown;
|
||||
else
|
||||
{
|
||||
remaining = await _cache.GetRatelimitAsync(GetDivorceKey(user.Id), 6.Hours());
|
||||
if (remaining is TimeSpan rem)
|
||||
{
|
||||
result = DivorceResult.Cooldown;
|
||||
return (w, result, amount, rem);
|
||||
}
|
||||
|
||||
amount = w.Price / 2;
|
||||
|
||||
if (w.Affinity?.UserId == user.Id)
|
||||
@@ -503,13 +486,13 @@ public class WaifuService : INService, IReadyExecutor
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private static readonly TypedKey<long> _waifuDecayKey = $"waifu:last_decay";
|
||||
public async Task OnReadyAsync()
|
||||
{
|
||||
// only decay waifu values from shard 0
|
||||
if (_client.ShardId != 0)
|
||||
return;
|
||||
|
||||
var redisKey = $"{_creds.RedisKey()}_last_waifu_decay";
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
@@ -521,31 +504,28 @@ public class WaifuService : INService, IReadyExecutor
|
||||
if (multi is < 0f or > 1f || decayInterval < 0)
|
||||
continue;
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var nowB = now.ToBinary();
|
||||
|
||||
var result = await _cache.GetAsync(_waifuDecayKey);
|
||||
|
||||
if (result.TryGetValue(out var val))
|
||||
var val = await _cache.Redis.GetDatabase().StringGetAsync(redisKey);
|
||||
if (val != default)
|
||||
{
|
||||
var lastDecay = DateTime.FromBinary(val);
|
||||
var lastDecay = DateTime.FromBinary((long)val);
|
||||
var toWait = decayInterval.Hours() - (DateTime.UtcNow - lastDecay);
|
||||
|
||||
if (toWait > 0.Hours())
|
||||
continue;
|
||||
}
|
||||
|
||||
await _cache.AddAsync(_waifuDecayKey, nowB);
|
||||
await _cache.Redis.GetDatabase().StringSetAsync(redisKey, DateTime.UtcNow.ToBinary());
|
||||
|
||||
await using var uow = _db.GetDbContext();
|
||||
|
||||
await uow.GetTable<WaifuInfo>()
|
||||
await uow.WaifuInfo
|
||||
.Where(x => x.Price > minPrice && x.ClaimerId == null)
|
||||
.UpdateAsync(old => new()
|
||||
{
|
||||
Price = (long)(old.Price * multi)
|
||||
});
|
||||
|
||||
await uow.SaveChangesAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@@ -14,9 +14,9 @@ public partial class Games : NadekoModule<GamesService>
|
||||
private readonly IHttpClientFactory _httpFactory;
|
||||
private readonly Random _rng = new();
|
||||
|
||||
public Games(IImageCache images, IHttpClientFactory factory)
|
||||
public Games(IDataCache data, IHttpClientFactory factory)
|
||||
{
|
||||
_images = images;
|
||||
_images = data.LocalImages;
|
||||
_httpFactory = factory;
|
||||
}
|
||||
|
||||
|
@@ -28,12 +28,11 @@ public class GirlRating
|
||||
Roll = roll;
|
||||
Advice = advice; // convenient to have it here, even though atm there are only few different ones.
|
||||
|
||||
Stream = new(async () =>
|
||||
Stream = new(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var bgBytes = await _images.GetRategirlBgAsync();
|
||||
using var img = Image.Load(bgBytes);
|
||||
using var img = Image.Load(_images.RategirlMatrix);
|
||||
const int minx = 35;
|
||||
const int miny = 385;
|
||||
const int length = 345;
|
||||
@@ -41,8 +40,7 @@ public class GirlRating
|
||||
var pointx = (int)(minx + (length * (Hot / 10)));
|
||||
var pointy = (int)(miny - (length * ((Crazy - 4) / 6)));
|
||||
|
||||
var dotBytes = await _images.GetRategirlDotAsync();
|
||||
using (var pointImg = Image.Load(dotBytes))
|
||||
using (var pointImg = Image.Load(_images.RategirlDot))
|
||||
{
|
||||
img.Mutate(x => x.DrawImage(pointImg, new(pointx - 10, pointy - 10), new GraphicsOptions()));
|
||||
}
|
||||
|
@@ -9,14 +9,14 @@ public partial class Games
|
||||
[Group]
|
||||
public partial class TriviaCommands : NadekoModule<GamesService>
|
||||
{
|
||||
private readonly ILocalDataCache _cache;
|
||||
private readonly IDataCache _cache;
|
||||
private readonly ICurrencyService _cs;
|
||||
private readonly GamesConfigService _gamesConfig;
|
||||
private readonly DiscordSocketClient _client;
|
||||
|
||||
public TriviaCommands(
|
||||
DiscordSocketClient client,
|
||||
ILocalDataCache cache,
|
||||
IDataCache cache,
|
||||
ICurrencyService cs,
|
||||
GamesConfigService gamesConfig)
|
||||
{
|
||||
|
@@ -17,7 +17,7 @@ public class TriviaGame
|
||||
public bool GameActive { get; private set; }
|
||||
public bool ShouldStopGame { get; private set; }
|
||||
private readonly SemaphoreSlim _guessLock = new(1, 1);
|
||||
private readonly ILocalDataCache _cache;
|
||||
private readonly IDataCache _cache;
|
||||
private readonly IBotStrings _strings;
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly GamesConfig _config;
|
||||
@@ -35,7 +35,7 @@ public class TriviaGame
|
||||
IBotStrings strings,
|
||||
DiscordSocketClient client,
|
||||
GamesConfig config,
|
||||
ILocalDataCache cache,
|
||||
IDataCache cache,
|
||||
ICurrencyService cs,
|
||||
IGuild guild,
|
||||
ITextChannel channel,
|
||||
@@ -70,7 +70,7 @@ public class TriviaGame
|
||||
showHowToQuit = !showHowToQuit;
|
||||
|
||||
// load question
|
||||
CurrentQuestion = await _questionPool.GetRandomQuestionAsync(OldQuestions, _options.IsPokemon);
|
||||
CurrentQuestion = _questionPool.GetRandomQuestion(OldQuestions, _options.IsPokemon);
|
||||
if (string.IsNullOrWhiteSpace(CurrentQuestion?.Answer)
|
||||
|| string.IsNullOrWhiteSpace(CurrentQuestion.Question))
|
||||
{
|
||||
|
@@ -4,15 +4,6 @@ using System.Text.RegularExpressions;
|
||||
// THANKS @ShoMinamimoto for suggestions and coding help
|
||||
namespace NadekoBot.Modules.Games.Common.Trivia;
|
||||
|
||||
public sealed class TriviaQuestionModel
|
||||
{
|
||||
public string Category { get; init; }
|
||||
public string Question { get; init; }
|
||||
public string ImageUrl { get; init; }
|
||||
public string AnswerImageUrl { get; init; }
|
||||
public string Answer { get; init; }
|
||||
}
|
||||
|
||||
public class TriviaQuestion
|
||||
{
|
||||
public const int MAX_STRING_LENGTH = 22;
|
||||
@@ -26,30 +17,29 @@ public class TriviaQuestion
|
||||
new(22, 3)
|
||||
};
|
||||
|
||||
public string Category
|
||||
=> _qModel.Category;
|
||||
|
||||
public string Question
|
||||
=> _qModel.Question;
|
||||
|
||||
public string ImageUrl
|
||||
=> _qModel.ImageUrl;
|
||||
|
||||
public string AnswerImageUrl
|
||||
=> _qModel.AnswerImageUrl ?? ImageUrl;
|
||||
|
||||
public string Answer
|
||||
=> _qModel.Answer;
|
||||
public string Category { get; set; }
|
||||
public string Question { get; set; }
|
||||
public string ImageUrl { get; set; }
|
||||
public string AnswerImageUrl { get; set; }
|
||||
public string Answer { get; set; }
|
||||
|
||||
public string CleanAnswer
|
||||
=> cleanAnswer ?? (cleanAnswer = Clean(Answer));
|
||||
|
||||
private string cleanAnswer;
|
||||
private readonly TriviaQuestionModel _qModel;
|
||||
|
||||
public TriviaQuestion(TriviaQuestionModel qModel)
|
||||
public TriviaQuestion(
|
||||
string q,
|
||||
string a,
|
||||
string c,
|
||||
string img = null,
|
||||
string answerImage = null)
|
||||
{
|
||||
_qModel = qModel;
|
||||
Question = q;
|
||||
Answer = a;
|
||||
Category = c;
|
||||
ImageUrl = img;
|
||||
AnswerImageUrl = answerImage ?? img;
|
||||
}
|
||||
|
||||
public string GetHint()
|
||||
|
@@ -1,48 +1,45 @@
|
||||
#nullable disable
|
||||
namespace NadekoBot.Modules.Games.Common.Trivia;
|
||||
|
||||
public class TriviaQuestionPool
|
||||
{
|
||||
private readonly ILocalDataCache _cache;
|
||||
private TriviaQuestion[] Pool
|
||||
=> _cache.LocalData.TriviaQuestions;
|
||||
|
||||
private IReadOnlyDictionary<int, string> Map
|
||||
=> _cache.LocalData.PokemonMap;
|
||||
|
||||
private readonly IDataCache _cache;
|
||||
private readonly int _maxPokemonId;
|
||||
|
||||
private readonly NadekoRandom _rng = new();
|
||||
|
||||
public TriviaQuestionPool(ILocalDataCache cache)
|
||||
public TriviaQuestionPool(IDataCache cache)
|
||||
{
|
||||
_cache = cache;
|
||||
_maxPokemonId = 721; //xd
|
||||
}
|
||||
|
||||
public async Task<TriviaQuestion?> GetRandomQuestionAsync(HashSet<TriviaQuestion> exclude, bool isPokemon)
|
||||
public TriviaQuestion GetRandomQuestion(HashSet<TriviaQuestion> exclude, bool isPokemon)
|
||||
{
|
||||
if (Pool.Length == 0)
|
||||
return null;
|
||||
|
||||
if (isPokemon)
|
||||
{
|
||||
var pokes = await _cache.GetPokemonMapAsync();
|
||||
|
||||
if (pokes is null or { Count: 0 })
|
||||
return default;
|
||||
|
||||
var num = _rng.Next(1, _maxPokemonId + 1);
|
||||
return new(new()
|
||||
{
|
||||
Question = "Who's That Pokémon?",
|
||||
Answer = pokes[num].ToTitleCase(),
|
||||
Category = "Pokemon",
|
||||
ImageUrl = $@"https://nadeko.bot/images/pokemon/shadows/{num}.png",
|
||||
AnswerImageUrl = $@"https://nadeko.bot/images/pokemon/real/{num}.png"
|
||||
});
|
||||
return new("Who's That Pokémon?",
|
||||
Map[num].ToTitleCase(),
|
||||
"Pokemon",
|
||||
$@"https://nadeko.bot/images/pokemon/shadows/{num}.png",
|
||||
$@"https://nadeko.bot/images/pokemon/real/{num}.png");
|
||||
}
|
||||
|
||||
TriviaQuestion randomQuestion;
|
||||
var pool = await _cache.GetTriviaQuestionsAsync();
|
||||
|
||||
if(pool is null)
|
||||
return default;
|
||||
|
||||
while (exclude.Contains(randomQuestion = new(pool[_rng.Next(0, pool.Length)])))
|
||||
while (exclude.Contains(randomQuestion = Pool[_rng.Next(0, Pool.Length)]))
|
||||
{
|
||||
// if too many questions are excluded, clear the exclusion list and start over
|
||||
if (exclude.Count > pool.Length / 10 * 9)
|
||||
if (exclude.Count > Pool.Length / 10 * 9)
|
||||
{
|
||||
exclude.Clear();
|
||||
break;
|
||||
|
209
src/NadekoBot/Modules/Music/_Common/Impl/RedisTrackCacher.cs
Normal file
209
src/NadekoBot/Modules/Music/_Common/Impl/RedisTrackCacher.cs
Normal file
@@ -0,0 +1,209 @@
|
||||
using StackExchange.Redis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace NadekoBot.Modules.Music;
|
||||
|
||||
public sealed class RedisTrackCacher : ITrackCacher
|
||||
{
|
||||
private readonly ConnectionMultiplexer _multiplexer;
|
||||
|
||||
public RedisTrackCacher(ConnectionMultiplexer multiplexer)
|
||||
=> _multiplexer = multiplexer;
|
||||
|
||||
public async Task<string?> GetOrCreateStreamLink(
|
||||
string id,
|
||||
MusicPlatform platform,
|
||||
Func<Task<(string StreamUrl, TimeSpan Expiry)>> streamUrlFactory)
|
||||
{
|
||||
var trackStreamKey = CreateStreamKey(id, platform);
|
||||
|
||||
var value = await GetStreamFromCacheInternalAsync(trackStreamKey);
|
||||
|
||||
// if there is no cached value
|
||||
if (value == default)
|
||||
{
|
||||
// otherwise retrieve and cache a new value, and run this method again
|
||||
var success = await CreateAndCacheStreamUrlAsync(trackStreamKey, streamUrlFactory);
|
||||
if (!success)
|
||||
return null;
|
||||
|
||||
return await GetOrCreateStreamLink(id, platform, streamUrlFactory);
|
||||
}
|
||||
|
||||
// cache new one for future use
|
||||
_ = Task.Run(() => CreateAndCacheStreamUrlAsync(trackStreamKey, streamUrlFactory));
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static string CreateStreamKey(string id, MusicPlatform platform)
|
||||
=> $"track:stream:{platform}:{id}";
|
||||
|
||||
private async Task<bool> CreateAndCacheStreamUrlAsync(
|
||||
string trackStreamKey,
|
||||
Func<Task<(string StreamUrl, TimeSpan Expiry)>> factory)
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await factory();
|
||||
if (data == default)
|
||||
return false;
|
||||
|
||||
await CacheStreamUrlInternalAsync(trackStreamKey, data.StreamUrl, data.Expiry);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Error resolving stream link for {TrackCacheKey}", trackStreamKey);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Task CacheStreamUrlAsync(
|
||||
string id,
|
||||
MusicPlatform platform,
|
||||
string url,
|
||||
TimeSpan expiry)
|
||||
=> CacheStreamUrlInternalAsync(CreateStreamKey(id, platform), url, expiry);
|
||||
|
||||
private async Task CacheStreamUrlInternalAsync(string trackStreamKey, string url, TimeSpan expiry)
|
||||
{
|
||||
// keys need to be expired after an hour
|
||||
// to make sure client doesn't get an expired stream url
|
||||
// to achieve this, track keys will be just pointers to real data
|
||||
// but that data will expire
|
||||
|
||||
var db = _multiplexer.GetDatabase();
|
||||
var dataKey = $"entry:{Guid.NewGuid()}:{trackStreamKey}";
|
||||
await db.StringSetAsync(dataKey, url, expiry);
|
||||
await db.ListRightPushAsync(trackStreamKey, dataKey);
|
||||
}
|
||||
|
||||
private async Task<string?> GetStreamFromCacheInternalAsync(string trackStreamKey)
|
||||
{
|
||||
// Job of the method which retrieves keys is to pop the elements
|
||||
// from the list of cached trackurls until it finds a non-expired key
|
||||
|
||||
var db = _multiplexer.GetDatabase();
|
||||
while (true)
|
||||
{
|
||||
string? dataKey = await db.ListLeftPopAsync(trackStreamKey);
|
||||
if (dataKey == default)
|
||||
return null;
|
||||
|
||||
var streamUrl = await db.StringGetAsync(dataKey);
|
||||
if (streamUrl == default)
|
||||
continue;
|
||||
|
||||
return streamUrl;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static string CreateCachedDataKey(string id, MusicPlatform platform)
|
||||
=> $"track:data:{platform}:{id}";
|
||||
|
||||
public Task CacheTrackDataAsync(ICachableTrackData data)
|
||||
{
|
||||
var db = _multiplexer.GetDatabase();
|
||||
|
||||
var trackDataKey = CreateCachedDataKey(data.Id, data.Platform);
|
||||
var dataString = JsonSerializer.Serialize((object)data);
|
||||
// cache for 1 day
|
||||
return db.StringSetAsync(trackDataKey, dataString, TimeSpan.FromDays(1));
|
||||
}
|
||||
|
||||
public async Task<ICachableTrackData?> GetCachedDataByIdAsync(string id, MusicPlatform platform)
|
||||
{
|
||||
var db = _multiplexer.GetDatabase();
|
||||
|
||||
var trackDataKey = CreateCachedDataKey(id, platform);
|
||||
var data = await db.StringGetAsync(trackDataKey);
|
||||
if (data == default)
|
||||
return null;
|
||||
|
||||
return JsonSerializer.Deserialize<CachableTrackData>(data);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static string CreateCachedQueryDataKey(string query, MusicPlatform platform)
|
||||
=> $"track:query_to_id:{platform}:{query}";
|
||||
|
||||
public async Task<ICachableTrackData?> GetCachedDataByQueryAsync(string query, MusicPlatform platform)
|
||||
{
|
||||
query = Uri.EscapeDataString(query.Trim());
|
||||
|
||||
var db = _multiplexer.GetDatabase();
|
||||
var queryDataKey = CreateCachedQueryDataKey(query, platform);
|
||||
|
||||
var trackId = await db.StringGetAsync(queryDataKey);
|
||||
if (trackId == default)
|
||||
return null;
|
||||
|
||||
return await GetCachedDataByIdAsync(trackId, platform);
|
||||
}
|
||||
|
||||
public async Task CacheTrackDataByQueryAsync(string query, ICachableTrackData data)
|
||||
{
|
||||
query = Uri.EscapeDataString(query.Trim());
|
||||
|
||||
// first cache the data
|
||||
await CacheTrackDataAsync(data);
|
||||
|
||||
// then map the query to cached data's id
|
||||
var db = _multiplexer.GetDatabase();
|
||||
|
||||
var queryDataKey = CreateCachedQueryDataKey(query, data.Platform);
|
||||
await db.StringSetAsync(queryDataKey, data.Id, TimeSpan.FromDays(7));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static string CreateCachedPlaylistKey(string playlistId, MusicPlatform platform)
|
||||
=> $"playlist:{platform}:{playlistId}";
|
||||
|
||||
public async Task<IReadOnlyCollection<string>> GetPlaylistTrackIdsAsync(string playlistId, MusicPlatform platform)
|
||||
{
|
||||
var db = _multiplexer.GetDatabase();
|
||||
var key = CreateCachedPlaylistKey(playlistId, platform);
|
||||
var vals = await db.ListRangeAsync(key);
|
||||
if (vals == default || vals.Length == 0)
|
||||
return Array.Empty<string>();
|
||||
|
||||
return vals.Select(x => x.ToString()).ToList();
|
||||
}
|
||||
|
||||
public async Task CachePlaylistTrackIdsAsync(string playlistId, MusicPlatform platform, IEnumerable<string> ids)
|
||||
{
|
||||
var db = _multiplexer.GetDatabase();
|
||||
var key = CreateCachedPlaylistKey(playlistId, platform);
|
||||
await db.ListRightPushAsync(key, ids.Select(x => (RedisValue)x).ToArray());
|
||||
await db.KeyExpireAsync(key, TimeSpan.FromDays(7));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static string CreateCachedPlaylistQueryKey(string query, MusicPlatform platform)
|
||||
=> $"playlist:query:{platform}:{query}";
|
||||
|
||||
public Task CachePlaylistIdByQueryAsync(string query, MusicPlatform platform, string playlistId)
|
||||
{
|
||||
query = Uri.EscapeDataString(query.Trim());
|
||||
var key = CreateCachedPlaylistQueryKey(query, platform);
|
||||
var db = _multiplexer.GetDatabase();
|
||||
return db.StringSetAsync(key, playlistId, TimeSpan.FromDays(7));
|
||||
}
|
||||
|
||||
public async Task<string?> GetPlaylistIdByQueryAsync(string query, MusicPlatform platform)
|
||||
{
|
||||
query = Uri.EscapeDataString(query.Trim());
|
||||
var key = CreateCachedPlaylistQueryKey(query, platform);
|
||||
|
||||
var val = await _multiplexer.GetDatabase().StringGetAsync(key);
|
||||
if (val == default)
|
||||
return null;
|
||||
|
||||
return val;
|
||||
}
|
||||
}
|
@@ -1,105 +0,0 @@
|
||||
namespace NadekoBot.Modules.Music;
|
||||
|
||||
public sealed class TrackCacher : ITrackCacher
|
||||
{
|
||||
private readonly IBotCache _cache;
|
||||
|
||||
public TrackCacher(IBotCache cache)
|
||||
=> _cache = cache;
|
||||
|
||||
|
||||
private TypedKey<string> GetStreamLinkKey(MusicPlatform platform, string id)
|
||||
=> new($"music:stream:{platform}:{id}");
|
||||
|
||||
public async Task<string?> GetOrCreateStreamLink(
|
||||
string id,
|
||||
MusicPlatform platform,
|
||||
Func<Task<(string StreamUrl, TimeSpan Expiry)>> streamUrlFactory)
|
||||
{
|
||||
var key = GetStreamLinkKey(platform, id);
|
||||
|
||||
var streamUrl = await _cache.GetOrDefaultAsync(key);
|
||||
await _cache.RemoveAsync(key);
|
||||
|
||||
if (streamUrl == default)
|
||||
{
|
||||
(streamUrl, _) = await streamUrlFactory();
|
||||
}
|
||||
|
||||
// make a new one for later use
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
(streamUrl, var expiry) = await streamUrlFactory();
|
||||
await CacheStreamUrlAsync(id, platform, streamUrl, expiry);
|
||||
});
|
||||
|
||||
return streamUrl;
|
||||
}
|
||||
|
||||
public async Task CacheStreamUrlAsync(
|
||||
string id,
|
||||
MusicPlatform platform,
|
||||
string url,
|
||||
TimeSpan expiry)
|
||||
=> await _cache.AddAsync(GetStreamLinkKey(platform, id), url, expiry);
|
||||
|
||||
// track data by id
|
||||
private TypedKey<CachableTrackData> GetTrackDataKey(MusicPlatform platform, string id)
|
||||
=> new($"music:track:{platform}:{id}");
|
||||
public async Task CacheTrackDataAsync(ICachableTrackData data)
|
||||
=> await _cache.AddAsync(GetTrackDataKey(data.Platform, data.Id), ToCachableTrackData(data));
|
||||
|
||||
private CachableTrackData ToCachableTrackData(ICachableTrackData data)
|
||||
=> new CachableTrackData()
|
||||
{
|
||||
Id = data.Id,
|
||||
Platform = data.Platform,
|
||||
Thumbnail = data.Thumbnail,
|
||||
Title = data.Title,
|
||||
Url = data.Url,
|
||||
};
|
||||
|
||||
public async Task<ICachableTrackData?> GetCachedDataByIdAsync(string id, MusicPlatform platform)
|
||||
=> await _cache.GetOrDefaultAsync(GetTrackDataKey(platform, id));
|
||||
|
||||
|
||||
// track data by query
|
||||
private TypedKey<CachableTrackData> GetTrackDataQueryKey(MusicPlatform platform, string query)
|
||||
=> new($"music:track:{platform}:q:{query}");
|
||||
|
||||
public async Task CacheTrackDataByQueryAsync(string query, ICachableTrackData data)
|
||||
=> await Task.WhenAll(
|
||||
_cache.AddAsync(GetTrackDataQueryKey(data.Platform, query), ToCachableTrackData(data)).AsTask(),
|
||||
_cache.AddAsync(GetTrackDataKey(data.Platform, data.Id), ToCachableTrackData(data)).AsTask());
|
||||
|
||||
public async Task<ICachableTrackData?> GetCachedDataByQueryAsync(string query, MusicPlatform platform)
|
||||
=> await _cache.GetOrDefaultAsync(GetTrackDataQueryKey(platform, query));
|
||||
|
||||
|
||||
// playlist track ids by playlist id
|
||||
private TypedKey<List<string>> GetPlaylistTracksCacheKey(string playlist, MusicPlatform platform)
|
||||
=> new($"music:playlist_tracks:{platform}:{playlist}");
|
||||
|
||||
public async Task CachePlaylistTrackIdsAsync(string playlistId, MusicPlatform platform, IEnumerable<string> ids)
|
||||
=> await _cache.AddAsync(GetPlaylistTracksCacheKey(playlistId, platform), ids.ToList());
|
||||
|
||||
public async Task<IReadOnlyCollection<string>> GetPlaylistTrackIdsAsync(string playlistId, MusicPlatform platform)
|
||||
{
|
||||
var result = await _cache.GetAsync(GetPlaylistTracksCacheKey(playlistId, platform));
|
||||
if (result.TryGetValue(out var val))
|
||||
return val;
|
||||
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
|
||||
|
||||
// playlist id by query
|
||||
private TypedKey<string> GetPlaylistCacheKey(string query, MusicPlatform platform)
|
||||
=> new($"music:playlist_id:{platform}:{query}");
|
||||
|
||||
public async Task CachePlaylistIdByQueryAsync(string query, MusicPlatform platform, string playlistId)
|
||||
=> await _cache.AddAsync(GetPlaylistCacheKey(query, platform), playlistId);
|
||||
|
||||
public async Task<string?> GetPlaylistIdByQueryAsync(string query, MusicPlatform platform)
|
||||
=> await _cache.GetOrDefaultAsync(GetPlaylistCacheKey(query, platform));
|
||||
}
|
@@ -4,7 +4,6 @@ using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace NadekoBot.Modules.Nsfw;
|
||||
|
||||
#if !GLOBAL_NADEKO
|
||||
[NoPublicBot]
|
||||
public partial class NSFW : NadekoModule<ISearchImagesService>
|
||||
{
|
||||
@@ -437,5 +436,4 @@ public partial class NSFW : NadekoModule<ISearchImagesService>
|
||||
.WithFooter(
|
||||
$"{data.Rating} ({data.Provider}) | {string.Join(" | ", data.Tags.Where(x => !string.IsNullOrWhiteSpace(x)).Take(5))}"));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
@@ -50,7 +50,7 @@ public sealed class FilterService : IExecOnMessage
|
||||
new(configs.SelectMany(gc => gc.FilterLinksChannelIds.Select(fci => fci.ChannelId)));
|
||||
|
||||
var dict = configs.ToDictionary(gc => gc.GuildId,
|
||||
gc => new ConcurrentHashSet<string>(gc.FilteredWords.Select(fw => fw.Word).Distinct()));
|
||||
gc => new ConcurrentHashSet<string>(gc.FilteredWords.Select(fw => fw.Word)));
|
||||
|
||||
ServerFilteredWords = new(dict);
|
||||
|
||||
|
@@ -1,38 +1,34 @@
|
||||
#nullable disable
|
||||
using System.Text.Json.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NadekoBot.Modules.Searches.Common;
|
||||
|
||||
public class AnimeResult
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public int Id { get; set; }
|
||||
|
||||
[JsonPropertyName("airing_status")]
|
||||
public string AiringStatusParsed { get; set; }
|
||||
|
||||
[JsonPropertyName("title_english")]
|
||||
public string TitleEnglish { get; set; }
|
||||
|
||||
[JsonPropertyName("total_episodes")]
|
||||
public int TotalEpisodes { get; set; }
|
||||
|
||||
[JsonPropertyName("description")]
|
||||
public string Description { get; set; }
|
||||
|
||||
[JsonPropertyName("image_url_lge")]
|
||||
public string ImageUrlLarge { get; set; }
|
||||
|
||||
[JsonPropertyName("genres")]
|
||||
public string[] Genres { get; set; }
|
||||
|
||||
[JsonPropertyName("average_score")]
|
||||
public float AverageScore { get; set; }
|
||||
|
||||
|
||||
public string AiringStatus
|
||||
=> AiringStatusParsed.ToTitleCase();
|
||||
|
||||
|
||||
[JsonProperty("airing_status")]
|
||||
public string AiringStatusParsed { get; set; }
|
||||
|
||||
[JsonProperty("title_english")]
|
||||
public string TitleEnglish { get; set; }
|
||||
|
||||
[JsonProperty("total_episodes")]
|
||||
public int TotalEpisodes { get; set; }
|
||||
|
||||
public string Description { get; set; }
|
||||
|
||||
[JsonProperty("image_url_lge")]
|
||||
public string ImageUrlLarge { get; set; }
|
||||
|
||||
public string[] Genres { get; set; }
|
||||
|
||||
[JsonProperty("average_score")]
|
||||
public string AverageScore { get; set; }
|
||||
|
||||
public string Link
|
||||
=> "http://anilist.co/anime/" + Id;
|
||||
|
||||
|
@@ -1,15 +1,17 @@
|
||||
#nullable disable
|
||||
using AngleSharp;
|
||||
using AngleSharp.Html.Dom;
|
||||
using NadekoBot.Modules.Searches.Common;
|
||||
using System.Net.Http.Json;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NadekoBot.Modules.Searches.Services;
|
||||
|
||||
public class AnimeSearchService : INService
|
||||
{
|
||||
private readonly IBotCache _cache;
|
||||
private readonly IDataCache _cache;
|
||||
private readonly IHttpClientFactory _httpFactory;
|
||||
|
||||
public AnimeSearchService(IBotCache cache, IHttpClientFactory httpFactory)
|
||||
public AnimeSearchService(IDataCache cache, IHttpClientFactory httpFactory)
|
||||
{
|
||||
_cache = cache;
|
||||
_httpFactory = httpFactory;
|
||||
@@ -19,25 +21,24 @@ public class AnimeSearchService : INService
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(query))
|
||||
throw new ArgumentNullException(nameof(query));
|
||||
|
||||
TypedKey<AnimeResult> GetKey(string link)
|
||||
=> new TypedKey<AnimeResult>($"anime2:{link}");
|
||||
|
||||
try
|
||||
{
|
||||
var suffix = Uri.EscapeDataString(query.Replace("/", " ", StringComparison.InvariantCulture));
|
||||
var link = $"https://aniapi.nadeko.bot/anime/{suffix}";
|
||||
var link = "https://aniapi.nadeko.bot/anime/"
|
||||
+ Uri.EscapeDataString(query.Replace("/", " ", StringComparison.InvariantCulture));
|
||||
link = link.ToLowerInvariant();
|
||||
var result = await _cache.GetAsync(GetKey(link));
|
||||
if (!result.TryPickT0(out var data, out _))
|
||||
var (ok, data) = await _cache.TryGetAnimeDataAsync(link);
|
||||
if (!ok)
|
||||
{
|
||||
using var http = _httpFactory.CreateClient();
|
||||
data = await http.GetFromJsonAsync<AnimeResult>(link);
|
||||
using (var http = _httpFactory.CreateClient())
|
||||
{
|
||||
data = await http.GetStringAsync(link);
|
||||
}
|
||||
|
||||
await _cache.AddAsync(GetKey(link), data, expiry: TimeSpan.FromHours(12));
|
||||
await _cache.SetAnimeDataAsync(link, data);
|
||||
}
|
||||
|
||||
return data;
|
||||
|
||||
return JsonConvert.DeserializeObject<AnimeResult>(data);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -45,31 +46,95 @@ public class AnimeSearchService : INService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<NovelResult> GetNovelData(string query)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(query))
|
||||
throw new ArgumentNullException(nameof(query));
|
||||
|
||||
query = query.Replace(" ", "-", StringComparison.InvariantCulture);
|
||||
try
|
||||
{
|
||||
var link = "https://www.novelupdates.com/series/"
|
||||
+ Uri.EscapeDataString(query.Replace(" ", "-").Replace("/", " "));
|
||||
link = link.ToLowerInvariant();
|
||||
var (ok, data) = await _cache.TryGetNovelDataAsync(link);
|
||||
if (!ok)
|
||||
{
|
||||
var config = Configuration.Default.WithDefaultLoader();
|
||||
using var document = await BrowsingContext.New(config).OpenAsync(link);
|
||||
var imageElem = document.QuerySelector("div.seriesimg > img");
|
||||
if (imageElem is null)
|
||||
return null;
|
||||
var imageUrl = ((IHtmlImageElement)imageElem).Source;
|
||||
|
||||
var descElem = document.QuerySelector("div#editdescription > p");
|
||||
var desc = descElem.InnerHtml;
|
||||
|
||||
var genres = document.QuerySelector("div#seriesgenre")
|
||||
.Children.Select(x => x as IHtmlAnchorElement)
|
||||
.Where(x => x is not null)
|
||||
.Select(x => $"[{x.InnerHtml}]({x.Href})")
|
||||
.ToArray();
|
||||
|
||||
var authors = document.QuerySelector("div#showauthors")
|
||||
.Children.Select(x => x as IHtmlAnchorElement)
|
||||
.Where(x => x is not null)
|
||||
.Select(x => $"[{x.InnerHtml}]({x.Href})")
|
||||
.ToArray();
|
||||
|
||||
var score = ((IHtmlSpanElement)document.QuerySelector("h5.seriesother > span.uvotes")).InnerHtml;
|
||||
|
||||
var status = document.QuerySelector("div#editstatus").InnerHtml;
|
||||
var title = document.QuerySelector("div.w-blog-content > div.seriestitlenu").InnerHtml;
|
||||
|
||||
var obj = new NovelResult
|
||||
{
|
||||
Description = desc,
|
||||
Authors = authors,
|
||||
Genres = genres,
|
||||
ImageUrl = imageUrl,
|
||||
Link = link,
|
||||
Score = score,
|
||||
Status = status,
|
||||
Title = title
|
||||
};
|
||||
|
||||
await _cache.SetNovelDataAsync(link, JsonConvert.SerializeObject(obj));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
return JsonConvert.DeserializeObject<NovelResult>(data);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Error getting novel data");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MangaResult> GetMangaData(string query)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(query))
|
||||
throw new ArgumentNullException(nameof(query));
|
||||
|
||||
TypedKey<MangaResult> GetKey(string link)
|
||||
=> new TypedKey<MangaResult>($"manga2:{link}");
|
||||
|
||||
try
|
||||
{
|
||||
var link = "https://aniapi.nadeko.bot/manga/"
|
||||
+ Uri.EscapeDataString(query.Replace("/", " ", StringComparison.InvariantCulture));
|
||||
link = link.ToLowerInvariant();
|
||||
|
||||
var result = await _cache.GetAsync(GetKey(link));
|
||||
if (!result.TryPickT0(out var data, out _))
|
||||
var (ok, data) = await _cache.TryGetAnimeDataAsync(link);
|
||||
if (!ok)
|
||||
{
|
||||
using var http = _httpFactory.CreateClient();
|
||||
data = await http.GetFromJsonAsync<MangaResult>(link);
|
||||
using (var http = _httpFactory.CreateClient())
|
||||
{
|
||||
data = await http.GetStringAsync(link);
|
||||
}
|
||||
|
||||
await _cache.AddAsync(GetKey(link), data, expiry: TimeSpan.FromHours(3));
|
||||
await _cache.SetAnimeDataAsync(link, data);
|
||||
}
|
||||
|
||||
|
||||
return data;
|
||||
return JsonConvert.DeserializeObject<MangaResult>(data);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
@@ -1,36 +1,32 @@
|
||||
#nullable disable
|
||||
using System.Text.Json.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NadekoBot.Modules.Searches.Common;
|
||||
|
||||
public class MangaResult
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public int Id { get; set; }
|
||||
|
||||
[JsonPropertyName("publishing_status")]
|
||||
[JsonProperty("publishing_status")]
|
||||
public string PublishingStatus { get; set; }
|
||||
|
||||
[JsonPropertyName("image_url_lge")]
|
||||
[JsonProperty("image_url_lge")]
|
||||
public string ImageUrlLge { get; set; }
|
||||
|
||||
[JsonPropertyName("title_english")]
|
||||
[JsonProperty("title_english")]
|
||||
public string TitleEnglish { get; set; }
|
||||
|
||||
[JsonPropertyName("total_chapters")]
|
||||
[JsonProperty("total_chapters")]
|
||||
public int TotalChapters { get; set; }
|
||||
|
||||
[JsonPropertyName("total_volumes")]
|
||||
[JsonProperty("total_volumes")]
|
||||
public int TotalVolumes { get; set; }
|
||||
|
||||
[JsonPropertyName("description")]
|
||||
public string Description { get; set; }
|
||||
|
||||
[JsonPropertyName("genres")]
|
||||
public string[] Genres { get; set; }
|
||||
|
||||
[JsonPropertyName("average_score")]
|
||||
public float AverageScore { get; set; }
|
||||
[JsonProperty("average_score")]
|
||||
public string AverageScore { get; set; }
|
||||
|
||||
public string Link
|
||||
=> "http://anilist.co/manga/" + Id;
|
||||
|
@@ -15,13 +15,13 @@ namespace NadekoBot.Modules.Searches.Services;
|
||||
|
||||
public class CryptoService : INService
|
||||
{
|
||||
private readonly IBotCache _cache;
|
||||
private readonly IDataCache _cache;
|
||||
private readonly IHttpClientFactory _httpFactory;
|
||||
private readonly IBotCredentials _creds;
|
||||
|
||||
private readonly SemaphoreSlim _getCryptoLock = new(1, 1);
|
||||
|
||||
public CryptoService(IBotCache cache, IHttpClientFactory httpFactory, IBotCredentials creds)
|
||||
public CryptoService(IDataCache cache, IHttpClientFactory httpFactory, IBotCredentials creds)
|
||||
{
|
||||
_cache = cache;
|
||||
_httpFactory = httpFactory;
|
||||
@@ -40,8 +40,7 @@ public class CryptoService : INService
|
||||
Span<PointF> points = new PointF[gElement.ChildNodes.Count];
|
||||
var cnt = 0;
|
||||
|
||||
bool GetValuesFromAttributes(
|
||||
XmlAttributeCollection attrs,
|
||||
bool GetValuesFromAttributes(XmlAttributeCollection attrs,
|
||||
out float x1,
|
||||
out float y1,
|
||||
out float x2,
|
||||
@@ -57,7 +56,7 @@ public class CryptoService : INService
|
||||
&& attrs["y2"]?.Value is string y2Str
|
||||
&& float.TryParse(y2Str, NumberStyles.Any, CultureInfo.InvariantCulture, out y2);
|
||||
}
|
||||
|
||||
|
||||
foreach (XmlElement x in gElement.ChildNodes)
|
||||
{
|
||||
if (x.Name != "line")
|
||||
@@ -68,22 +67,22 @@ public class CryptoService : INService
|
||||
points[cnt++] = new(x1, y1);
|
||||
// this point will be set twice to the same value
|
||||
// on all points except the last one
|
||||
if (cnt + 1 < points.Length)
|
||||
if(cnt + 1 < points.Length)
|
||||
points[cnt + 1] = new(x2, y2);
|
||||
}
|
||||
}
|
||||
|
||||
if (cnt == 0)
|
||||
return Array.Empty<PointF>();
|
||||
|
||||
|
||||
return points.Slice(0, cnt).ToArray();
|
||||
}
|
||||
|
||||
|
||||
private SixLabors.ImageSharp.Image<Rgba32> GenerateSparklineChart(PointF[] points, bool up)
|
||||
{
|
||||
const int width = 164;
|
||||
const int height = 48;
|
||||
|
||||
|
||||
var img = new Image<Rgba32>(width, height, Color.Transparent);
|
||||
var color = up
|
||||
? Color.Green
|
||||
@@ -93,10 +92,10 @@ public class CryptoService : INService
|
||||
{
|
||||
x.DrawLines(color, 2, points);
|
||||
});
|
||||
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
|
||||
public async Task<(CmcResponseData? Data, CmcResponseData? Nearest)> GetCryptoData(string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
@@ -131,20 +130,20 @@ public class CryptoService : INService
|
||||
await _getCryptoLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var data = await _cache.GetOrAddAsync(new("nadeko:crypto_data"),
|
||||
async () =>
|
||||
var fullStrData = await _cache.GetOrAddCachedDataAsync("nadeko:crypto_data",
|
||||
async _ =>
|
||||
{
|
||||
try
|
||||
{
|
||||
using var http = _httpFactory.CreateClient();
|
||||
var data = await http.GetFromJsonAsync<CryptoResponse>(
|
||||
var strData = await http.GetFromJsonAsync<CryptoResponse>(
|
||||
"https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?"
|
||||
+ $"CMC_PRO_API_KEY={_creds.CoinmarketcapApiKey}"
|
||||
+ "&start=1"
|
||||
+ "&limit=5000"
|
||||
+ "&convert=USD");
|
||||
|
||||
return data;
|
||||
return JsonSerializer.Serialize(strData);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -152,12 +151,13 @@ public class CryptoService : INService
|
||||
return default;
|
||||
}
|
||||
},
|
||||
"",
|
||||
TimeSpan.FromHours(2));
|
||||
|
||||
if (data is null)
|
||||
if (fullStrData is null)
|
||||
return default;
|
||||
|
||||
return data.Data;
|
||||
|
||||
return JsonSerializer.Deserialize<CryptoResponse>(fullStrData)?.Data ?? new();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -170,33 +170,44 @@ public class CryptoService : INService
|
||||
}
|
||||
}
|
||||
|
||||
private TypedKey<byte[]> GetSparklineKey(int id)
|
||||
=> new($"crypto:sparkline:{id}");
|
||||
|
||||
public async Task<Stream?> GetSparklineAsync(int id, bool up)
|
||||
{
|
||||
var key = $"crypto:sparkline:{id}";
|
||||
|
||||
// attempt to get from cache
|
||||
var db = _cache.Redis.GetDatabase();
|
||||
byte[] bytes = await db.StringGetAsync(key);
|
||||
// if it succeeds, return it
|
||||
if (bytes is { Length: > 0 })
|
||||
{
|
||||
return bytes.ToStream();
|
||||
}
|
||||
|
||||
// if it fails, generate a new one
|
||||
var points = await DownloadSparklinePointsAsync(id);
|
||||
if (points is null)
|
||||
return default;
|
||||
|
||||
var sparkline = GenerateSparklineChart(points, up);
|
||||
|
||||
// add to cache for 1h and return it
|
||||
|
||||
var stream = sparkline.ToStream();
|
||||
await db.StringSetAsync(key, stream.ToArray(), expiry: TimeSpan.FromHours(1));
|
||||
return stream;
|
||||
}
|
||||
|
||||
private async Task<PointF[]?> DownloadSparklinePointsAsync(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var bytes = await _cache.GetOrAddAsync(GetSparklineKey(id),
|
||||
async () =>
|
||||
{
|
||||
// if it fails, generate a new one
|
||||
var points = await DownloadSparklinePointsAsync(id);
|
||||
var sparkline = GenerateSparklineChart(points, up);
|
||||
|
||||
using var stream = await sparkline.ToStreamAsync();
|
||||
return stream.ToArray();
|
||||
},
|
||||
TimeSpan.FromHours(1));
|
||||
|
||||
if (bytes is { Length: > 0 })
|
||||
{
|
||||
return bytes.ToStream();
|
||||
}
|
||||
|
||||
return default;
|
||||
using var http = _httpFactory.CreateClient();
|
||||
var str = await http.GetStringAsync(
|
||||
$"https://s3.coinmarketcap.com/generated/sparklines/web/7d/usd/{id}.svg");
|
||||
var points = GetSparklinePointsFromSvgText(str);
|
||||
return points;
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch(Exception ex)
|
||||
{
|
||||
Log.Warning(ex,
|
||||
"Exception occurred while downloading sparkline points: {ErrorMessage}",
|
||||
@@ -204,13 +215,4 @@ public class CryptoService : INService
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<PointF[]> DownloadSparklinePointsAsync(int id)
|
||||
{
|
||||
using var http = _httpFactory.CreateClient();
|
||||
var str = await http.GetStringAsync(
|
||||
$"https://s3.coinmarketcap.com/generated/sparklines/web/7d/usd/{id}.svg");
|
||||
var points = GetSparklinePointsFromSvgText(str);
|
||||
return points;
|
||||
}
|
||||
}
|
@@ -9,9 +9,15 @@ public partial class Searches
|
||||
[Group]
|
||||
public partial class PokemonSearchCommands : NadekoModule<SearchesService>
|
||||
{
|
||||
private readonly ILocalDataCache _cache;
|
||||
public IReadOnlyDictionary<string, SearchPokemon> Pokemons
|
||||
=> _cache.LocalData.Pokemons;
|
||||
|
||||
public PokemonSearchCommands(ILocalDataCache cache)
|
||||
public IReadOnlyDictionary<string, SearchPokemonAbility> PokemonAbilities
|
||||
=> _cache.LocalData.PokemonAbilities;
|
||||
|
||||
private readonly IDataCache _cache;
|
||||
|
||||
public PokemonSearchCommands(IDataCache cache)
|
||||
=> _cache = cache;
|
||||
|
||||
[Cmd]
|
||||
@@ -21,7 +27,7 @@ public partial class Searches
|
||||
if (string.IsNullOrWhiteSpace(pokemon))
|
||||
return;
|
||||
|
||||
foreach (var kvp in await _cache.GetPokemonsAsync())
|
||||
foreach (var kvp in Pokemons)
|
||||
{
|
||||
if (kvp.Key.ToUpperInvariant() == pokemon.ToUpperInvariant())
|
||||
{
|
||||
@@ -52,7 +58,7 @@ public partial class Searches
|
||||
ability = ability?.Trim().ToUpperInvariant().Replace(" ", "", StringComparison.InvariantCulture);
|
||||
if (string.IsNullOrWhiteSpace(ability))
|
||||
return;
|
||||
foreach (var kvp in await _cache.GetPokemonAbilitiesAsync())
|
||||
foreach (var kvp in PokemonAbilities)
|
||||
{
|
||||
if (kvp.Key.ToUpperInvariant() == ability)
|
||||
{
|
||||
|
@@ -10,14 +10,14 @@ public partial class Searches
|
||||
public partial class SearchCommands : NadekoModule
|
||||
{
|
||||
private readonly ISearchServiceFactory _searchFactory;
|
||||
private readonly IBotCache _cache;
|
||||
private readonly ConnectionMultiplexer _redis;
|
||||
|
||||
public SearchCommands(
|
||||
ISearchServiceFactory searchFactory,
|
||||
IBotCache cache)
|
||||
ConnectionMultiplexer redis)
|
||||
{
|
||||
_searchFactory = searchFactory;
|
||||
_cache = cache;
|
||||
_redis = redis;
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
@@ -127,17 +127,18 @@ public partial class Searches
|
||||
await ctx.Channel.EmbedAsync(null, embeds: embeds);
|
||||
}
|
||||
|
||||
private TypedKey<string> GetYtCacheKey(string query)
|
||||
=> new($"search:youtube:{query}");
|
||||
|
||||
private async Task AddYoutubeUrlToCacheAsync(string query, string url)
|
||||
=> await _cache.AddAsync(GetYtCacheKey(query), url, expiry: 1.Hours());
|
||||
{
|
||||
var db = _redis.GetDatabase();
|
||||
await db.StringSetAsync($"search:youtube:{query}", url, expiry: 1.Hours());
|
||||
}
|
||||
|
||||
private async Task<VideoInfo?> GetYoutubeUrlFromCacheAsync(string query)
|
||||
{
|
||||
var result = await _cache.GetAsync(GetYtCacheKey(query));
|
||||
var db = _redis.GetDatabase();
|
||||
var url = await db.StringGetAsync($"search:youtube:{query}");
|
||||
|
||||
if (!result.TryGetValue(out var url) || string.IsNullOrWhiteSpace(url))
|
||||
if (string.IsNullOrWhiteSpace(url))
|
||||
return null;
|
||||
|
||||
return new VideoInfo()
|
||||
|
@@ -1,4 +1,6 @@
|
||||
#nullable disable
|
||||
using AngleSharp.Html.Dom;
|
||||
using AngleSharp.Html.Parser;
|
||||
using Html2Markdown;
|
||||
using NadekoBot.Modules.Searches.Common;
|
||||
using Newtonsoft.Json;
|
||||
@@ -8,6 +10,7 @@ using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Drawing.Processing;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using System.Net;
|
||||
using Color = SixLabors.ImageSharp.Color;
|
||||
using Image = SixLabors.ImageSharp.Image;
|
||||
|
||||
@@ -28,9 +31,9 @@ public class SearchesService : INService
|
||||
private readonly IHttpClientFactory _httpFactory;
|
||||
private readonly IGoogleApiService _google;
|
||||
private readonly IImageCache _imgs;
|
||||
private readonly IBotCache _c;
|
||||
private readonly IDataCache _cache;
|
||||
private readonly FontProvider _fonts;
|
||||
private readonly IBotCredsProvider _creds;
|
||||
private readonly IBotCredentials _creds;
|
||||
private readonly NadekoRandom _rng;
|
||||
private readonly List<string> _yomamaJokes;
|
||||
|
||||
@@ -39,16 +42,15 @@ public class SearchesService : INService
|
||||
|
||||
public SearchesService(
|
||||
IGoogleApiService google,
|
||||
IImageCache images,
|
||||
IBotCache c,
|
||||
IDataCache cache,
|
||||
IHttpClientFactory factory,
|
||||
FontProvider fonts,
|
||||
IBotCredsProvider creds)
|
||||
IBotCredentials creds)
|
||||
{
|
||||
_httpFactory = factory;
|
||||
_google = google;
|
||||
_imgs = images;
|
||||
_c = c;
|
||||
_imgs = cache.LocalImages;
|
||||
_cache = cache;
|
||||
_fonts = fonts;
|
||||
_creds = creds;
|
||||
_rng = new();
|
||||
@@ -74,28 +76,36 @@ public class SearchesService : INService
|
||||
}
|
||||
|
||||
public async Task<Stream> GetRipPictureAsync(string text, Uri imgUrl)
|
||||
=> (await GetRipPictureFactory(text, imgUrl)).ToStream();
|
||||
{
|
||||
var data = await _cache.GetOrAddCachedDataAsync($"nadeko_rip_{text}_{imgUrl}",
|
||||
GetRipPictureFactory,
|
||||
(text, imgUrl),
|
||||
TimeSpan.FromDays(1));
|
||||
|
||||
return data.ToStream();
|
||||
}
|
||||
|
||||
private void DrawAvatar(Image bg, Image avatarImage)
|
||||
=> bg.Mutate(x => x.Grayscale().DrawImage(avatarImage, new(83, 139), new GraphicsOptions()));
|
||||
|
||||
public async Task<byte[]> GetRipPictureFactory(string text, Uri avatarUrl)
|
||||
public async Task<byte[]> GetRipPictureFactory((string text, Uri avatarUrl) arg)
|
||||
{
|
||||
using var bg = Image.Load<Rgba32>(await _imgs.GetRipBgAsync());
|
||||
var result = await _c.GetImageDataAsync(avatarUrl);
|
||||
if (!result.TryPickT0(out var data, out _))
|
||||
var (text, avatarUrl) = arg;
|
||||
using var bg = Image.Load<Rgba32>(_imgs.Rip.ToArray());
|
||||
var (succ, data) = (false, (byte[])null); //await _cache.TryGetImageDataAsync(avatarUrl);
|
||||
if (!succ)
|
||||
{
|
||||
using var http = _httpFactory.CreateClient();
|
||||
data = await http.GetByteArrayAsync(avatarUrl);
|
||||
using (var avatarImg = Image.Load<Rgba32>(data))
|
||||
{
|
||||
avatarImg.Mutate(x => x.Resize(85, 85).ApplyRoundedCorners(42));
|
||||
await using var avStream = await avatarImg.ToStreamAsync();
|
||||
await using var avStream = avatarImg.ToStream();
|
||||
data = avStream.ToArray();
|
||||
DrawAvatar(bg, avatarImg);
|
||||
}
|
||||
|
||||
await _c.SetImageDataAsync(avatarUrl, data);
|
||||
await _cache.SetImageDataAsync(avatarUrl, data);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -118,7 +128,7 @@ public class SearchesService : INService
|
||||
new(25, 225)));
|
||||
|
||||
//flowa
|
||||
using (var flowers = Image.Load(await _imgs.GetRipOverlayAsync()))
|
||||
using (var flowers = Image.Load(_imgs.RipOverlay.ToArray()))
|
||||
{
|
||||
bg.Mutate(x => x.DrawImage(flowers, new(0, 0), new GraphicsOptions()));
|
||||
}
|
||||
@@ -127,12 +137,13 @@ public class SearchesService : INService
|
||||
return stream.ToArray();
|
||||
}
|
||||
|
||||
public async Task<WeatherData> GetWeatherDataAsync(string query)
|
||||
public Task<WeatherData> GetWeatherDataAsync(string query)
|
||||
{
|
||||
query = query.Trim().ToLowerInvariant();
|
||||
|
||||
return await _c.GetOrAddAsync(new($"nadeko_weather_{query}"),
|
||||
async () => await GetWeatherDataFactory(query),
|
||||
return _cache.GetOrAddCachedDataAsync($"nadeko_weather_{query}",
|
||||
GetWeatherDataFactory,
|
||||
query,
|
||||
TimeSpan.FromHours(3));
|
||||
}
|
||||
|
||||
@@ -173,28 +184,26 @@ public class SearchesService : INService
|
||||
if (string.IsNullOrEmpty(query))
|
||||
return (default, TimeErrors.InvalidInput);
|
||||
|
||||
|
||||
var locIqKey = _creds.GetCreds().LocationIqApiKey;
|
||||
var tzDbKey = _creds.GetCreds().TimezoneDbApiKey;
|
||||
if (string.IsNullOrWhiteSpace(locIqKey) || string.IsNullOrWhiteSpace(tzDbKey))
|
||||
if (string.IsNullOrWhiteSpace(_creds.LocationIqApiKey) || string.IsNullOrWhiteSpace(_creds.TimezoneDbApiKey))
|
||||
return (default, TimeErrors.ApiKeyMissing);
|
||||
|
||||
try
|
||||
{
|
||||
using var http = _httpFactory.CreateClient();
|
||||
var res = await _c.GetOrAddAsync(new($"searches:geo:{query}"),
|
||||
async () =>
|
||||
var res = await _cache.GetOrAddCachedDataAsync($"geo_{query}",
|
||||
_ =>
|
||||
{
|
||||
var url = "https://eu1.locationiq.com/v1/search.php?"
|
||||
+ (string.IsNullOrWhiteSpace(locIqKey)
|
||||
+ (string.IsNullOrWhiteSpace(_creds.LocationIqApiKey)
|
||||
? "key="
|
||||
: $"key={locIqKey}&")
|
||||
: $"key={_creds.LocationIqApiKey}&")
|
||||
+ $"q={Uri.EscapeDataString(query)}&"
|
||||
+ "format=json";
|
||||
|
||||
var res = await http.GetStringAsync(url);
|
||||
var res = http.GetStringAsync(url);
|
||||
return res;
|
||||
},
|
||||
"",
|
||||
TimeSpan.FromHours(1));
|
||||
|
||||
var responses = JsonConvert.DeserializeObject<LocationIqResponse[]>(res);
|
||||
@@ -208,7 +217,7 @@ public class SearchesService : INService
|
||||
|
||||
using var req = new HttpRequestMessage(HttpMethod.Get,
|
||||
"http://api.timezonedb.com/v2.1/get-time-zone?"
|
||||
+ $"key={tzDbKey}"
|
||||
+ $"key={_creds.TimezoneDbApiKey}"
|
||||
+ $"&format=json"
|
||||
+ $"&by=position"
|
||||
+ $"&lat={geoData.Lat}"
|
||||
@@ -306,8 +315,9 @@ public class SearchesService : INService
|
||||
public async Task<MtgData> GetMtgCardAsync(string search)
|
||||
{
|
||||
search = search.Trim().ToLowerInvariant();
|
||||
var data = await _c.GetOrAddAsync(new($"mtg:{search}"),
|
||||
async () => await GetMtgCardFactory(search),
|
||||
var data = await _cache.GetOrAddCachedDataAsync($"nadeko_mtg_{search}",
|
||||
GetMtgCardFactory,
|
||||
search,
|
||||
TimeSpan.FromDays(1));
|
||||
|
||||
if (data is null || data.Length == 0)
|
||||
@@ -358,11 +368,12 @@ public class SearchesService : INService
|
||||
return await cards.Select(GetMtgDataAsync).WhenAll();
|
||||
}
|
||||
|
||||
public async Task<HearthstoneCardData> GetHearthstoneCardDataAsync(string name)
|
||||
public Task<HearthstoneCardData> GetHearthstoneCardDataAsync(string name)
|
||||
{
|
||||
name = name.ToLowerInvariant();
|
||||
return await _c.GetOrAddAsync($"hearthstone:{name}",
|
||||
() => HearthstoneCardDataFactory(name),
|
||||
return _cache.GetOrAddCachedDataAsync($"nadeko_hearthstone_{name}",
|
||||
HearthstoneCardDataFactory,
|
||||
name,
|
||||
TimeSpan.FromDays(1));
|
||||
}
|
||||
|
||||
@@ -370,7 +381,7 @@ public class SearchesService : INService
|
||||
{
|
||||
using var http = _httpFactory.CreateClient();
|
||||
http.DefaultRequestHeaders.Clear();
|
||||
http.DefaultRequestHeaders.Add("x-rapidapi-key", _creds.GetCreds().RapidApiKey);
|
||||
http.DefaultRequestHeaders.Add("x-rapidapi-key", _creds.RapidApiKey);
|
||||
try
|
||||
{
|
||||
var response = await http.GetStringAsync("https://omgvamp-hearthstone-v1.p.rapidapi.com/"
|
||||
@@ -399,22 +410,16 @@ public class SearchesService : INService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<OmdbMovie> GetMovieDataAsync(string name)
|
||||
public Task<OmdbMovie> GetMovieDataAsync(string name)
|
||||
{
|
||||
name = name.Trim().ToLowerInvariant();
|
||||
return await _c.GetOrAddAsync(new($"movie:{name}"),
|
||||
() => GetMovieDataFactory(name),
|
||||
TimeSpan.FromDays(1));
|
||||
return _cache.GetOrAddCachedDataAsync($"nadeko_movie_{name}", GetMovieDataFactory, name, TimeSpan.FromDays(1));
|
||||
}
|
||||
|
||||
private async Task<OmdbMovie> GetMovieDataFactory(string name)
|
||||
{
|
||||
using var http = _httpFactory.CreateClient();
|
||||
var res = await http.GetStringAsync(string.Format("https://omdbapi.nadeko.bot/"
|
||||
+ "?t={0}"
|
||||
+ "&y="
|
||||
+ "&plot=full"
|
||||
+ "&r=json",
|
||||
var res = await http.GetStringAsync(string.Format("https://omdbapi.nadeko.bot/?t={0}&y=&plot=full&r=json",
|
||||
name.Trim().Replace(' ', '+')));
|
||||
var movie = JsonConvert.DeserializeObject<OmdbMovie>(res);
|
||||
if (movie?.Title is null)
|
||||
@@ -427,11 +432,10 @@ public class SearchesService : INService
|
||||
{
|
||||
const string steamGameIdsKey = "steam_names_to_appid";
|
||||
|
||||
var gamesMap = await _c.GetOrAddAsync(new(steamGameIdsKey),
|
||||
async () =>
|
||||
var gamesMap = await _cache.GetOrAddCachedDataAsync(steamGameIdsKey,
|
||||
async _ =>
|
||||
{
|
||||
using var http = _httpFactory.CreateClient();
|
||||
|
||||
// https://api.steampowered.com/ISteamApps/GetAppList/v2/
|
||||
var gamesStr = await http.GetStringAsync("https://api.steampowered.com/ISteamApps/GetAppList/v2/");
|
||||
var apps = JsonConvert
|
||||
@@ -442,18 +446,23 @@ public class SearchesService : INService
|
||||
{
|
||||
apps = new List<SteamGameId>()
|
||||
}
|
||||
})!
|
||||
})
|
||||
.applist.apps;
|
||||
|
||||
return apps.OrderBy(x => x.Name, StringComparer.OrdinalIgnoreCase)
|
||||
.GroupBy(x => x.Name)
|
||||
.ToDictionary(x => x.Key, x => x.First().AppId);
|
||||
//await db.HashSetAsync("steam_game_ids", apps.Select(app => new HashEntry(app.Name.Trim().ToLowerInvariant(), app.AppId)).ToArray());
|
||||
//await db.StringSetAsync("steam_game_ids", gamesStr, TimeSpan.FromHours(24));
|
||||
//await db.KeyExpireAsync("steam_game_ids", TimeSpan.FromHours(24), CommandFlags.FireAndForget);
|
||||
},
|
||||
default(string),
|
||||
TimeSpan.FromHours(24));
|
||||
|
||||
if (gamesMap is null)
|
||||
return -1;
|
||||
|
||||
|
||||
|
||||
query = query.Trim();
|
||||
|
||||
var keyList = gamesMap.Keys.ToList();
|
||||
|
@@ -1,6 +1,4 @@
|
||||
#nullable disable
|
||||
using LinqToDB;
|
||||
using LinqToDB.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NadekoBot.Common.ModuleBehaviors;
|
||||
using NadekoBot.Db;
|
||||
@@ -8,98 +6,10 @@ using NadekoBot.Db.Models;
|
||||
using NadekoBot.Modules.Searches.Common;
|
||||
using NadekoBot.Modules.Searches.Common.StreamNotifications;
|
||||
using NadekoBot.Services.Database.Models;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace NadekoBot.Modules.Searches.Services;
|
||||
|
||||
public sealed class StreamOnlineMessageDeleterService : INService, IReadyExecutor
|
||||
{
|
||||
private readonly StreamNotificationService _notifService;
|
||||
private readonly DbService _db;
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly IPubSub _pubSub;
|
||||
|
||||
public StreamOnlineMessageDeleterService(
|
||||
StreamNotificationService notifService,
|
||||
DbService db,
|
||||
IPubSub pubSub,
|
||||
DiscordSocketClient client)
|
||||
{
|
||||
_notifService = notifService;
|
||||
_db = db;
|
||||
_client = client;
|
||||
_pubSub = pubSub;
|
||||
}
|
||||
|
||||
public async Task OnReadyAsync()
|
||||
{
|
||||
_notifService.OnlineMessagesSent += OnOnlineMessagesSent;
|
||||
|
||||
if(_client.ShardId == 0)
|
||||
await _pubSub.Sub(_notifService.StreamsOfflineKey, OnStreamsOffline);
|
||||
}
|
||||
|
||||
private async Task OnOnlineMessagesSent(FollowedStream.FType type, string name, IReadOnlyCollection<(ulong, ulong)> pairs)
|
||||
{
|
||||
await using var ctx = _db.GetDbContext();
|
||||
foreach (var (channelId, messageId) in pairs)
|
||||
{
|
||||
await ctx.GetTable<StreamOnlineMessage>()
|
||||
.InsertAsync(() => new()
|
||||
{
|
||||
Name = name,
|
||||
Type = type,
|
||||
MessageId = messageId,
|
||||
ChannelId = channelId,
|
||||
DateAdded = DateTime.UtcNow,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async ValueTask OnStreamsOffline(List<StreamData> streamDatas)
|
||||
{
|
||||
if (_client.ShardId != 0)
|
||||
return;
|
||||
|
||||
var pairs = await GetMessagesToDelete(streamDatas);
|
||||
|
||||
foreach (var (channelId, messageId) in pairs)
|
||||
{
|
||||
try
|
||||
{
|
||||
var textChannel = await _client.GetChannelAsync(channelId) as ITextChannel;
|
||||
if (textChannel is null)
|
||||
continue;
|
||||
|
||||
await textChannel.DeleteMessageAsync(messageId);
|
||||
}
|
||||
catch
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<(ulong, ulong)>> GetMessagesToDelete(List<StreamData> streamDatas)
|
||||
{
|
||||
await using var ctx = _db.GetDbContext();
|
||||
|
||||
var toReturn = new List<(ulong, ulong)>();
|
||||
foreach (var sd in streamDatas)
|
||||
{
|
||||
var key = sd.CreateKey();
|
||||
var toDelete = await ctx.GetTable<StreamOnlineMessage>()
|
||||
.Where(x => (x.Type == key.Type && x.Name == key.Name)
|
||||
|| Sql.DateDiff(Sql.DateParts.Day, x.DateAdded, DateTime.UtcNow) > 1)
|
||||
.DeleteWithOutputAsync();
|
||||
|
||||
toReturn.AddRange(toDelete.Select(x => (x.ChannelId, x.MessageId)));
|
||||
}
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class StreamNotificationService : INService, IReadyExecutor
|
||||
{
|
||||
private readonly DbService _db;
|
||||
@@ -119,22 +29,18 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
|
||||
private readonly IPubSub _pubSub;
|
||||
private readonly IEmbedBuilderService _eb;
|
||||
|
||||
public TypedKey<List<StreamData>> StreamsOnlineKey { get; }
|
||||
public TypedKey<List<StreamData>> StreamsOfflineKey { get; }
|
||||
private readonly TypedKey<List<StreamData>> _streamsOnlineKey;
|
||||
private readonly TypedKey<List<StreamData>> _streamsOfflineKey;
|
||||
|
||||
private readonly TypedKey<FollowStreamPubData> _streamFollowKey;
|
||||
private readonly TypedKey<FollowStreamPubData> _streamUnfollowKey;
|
||||
|
||||
public event Func<
|
||||
FollowedStream.FType,
|
||||
string,
|
||||
IReadOnlyCollection<(ulong, ulong)>,
|
||||
Task> OnlineMessagesSent = static delegate { return Task.CompletedTask; };
|
||||
private readonly ConnectionMultiplexer _redis;
|
||||
|
||||
public StreamNotificationService(
|
||||
DbService db,
|
||||
DiscordSocketClient client,
|
||||
IBotStrings strings,
|
||||
ConnectionMultiplexer redis,
|
||||
IBotCredsProvider creds,
|
||||
IHttpClientFactory httpFactory,
|
||||
Bot bot,
|
||||
@@ -146,11 +52,11 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
|
||||
_strings = strings;
|
||||
_pubSub = pubSub;
|
||||
_eb = eb;
|
||||
|
||||
_streamTracker = new(httpFactory, creds);
|
||||
_redis = redis;
|
||||
_streamTracker = new(httpFactory, creds, redis, creds.GetCreds().RedisKey(), client.ShardId == 0);
|
||||
|
||||
StreamsOnlineKey = new("streams.online");
|
||||
StreamsOfflineKey = new("streams.offline");
|
||||
_streamsOnlineKey = new("streams.online");
|
||||
_streamsOfflineKey = new("streams.offline");
|
||||
|
||||
_streamFollowKey = new("stream.follow");
|
||||
_streamUnfollowKey = new("stream.unfollow");
|
||||
@@ -194,7 +100,7 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
|
||||
var allFollowedStreams = uow.Set<FollowedStream>().AsQueryable().ToList();
|
||||
|
||||
foreach (var fs in allFollowedStreams)
|
||||
_streamTracker.AddLastData(fs.CreateKey(), null, false);
|
||||
_streamTracker.CacheAddData(fs.CreateKey(), null, false);
|
||||
|
||||
_trackCounter = allFollowedStreams.GroupBy(x => new
|
||||
{
|
||||
@@ -206,8 +112,8 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
|
||||
}
|
||||
}
|
||||
|
||||
_pubSub.Sub(StreamsOfflineKey, HandleStreamsOffline);
|
||||
_pubSub.Sub(StreamsOnlineKey, HandleStreamsOnline);
|
||||
_pubSub.Sub(_streamsOfflineKey, HandleStreamsOffline);
|
||||
_pubSub.Sub(_streamsOnlineKey, HandleStreamsOnline);
|
||||
|
||||
if (client.ShardId == 0)
|
||||
{
|
||||
@@ -280,7 +186,7 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
|
||||
/// </summary>
|
||||
private ValueTask HandleFollowStream(FollowStreamPubData info)
|
||||
{
|
||||
_streamTracker.AddLastData(info.Key, null, false);
|
||||
_streamTracker.CacheAddData(info.Key, null, false);
|
||||
lock (_shardLock)
|
||||
{
|
||||
var key = info.Key;
|
||||
@@ -345,8 +251,45 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
|
||||
.WhenAll();
|
||||
}
|
||||
}
|
||||
|
||||
if (_client.ShardId == 0)
|
||||
{
|
||||
foreach (var stream in offlineStreams)
|
||||
{
|
||||
await DeleteOnlineMessages(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DeleteOnlineMessages(StreamData stream)
|
||||
{
|
||||
var db = _redis.GetDatabase();
|
||||
var data = await db.ListRangeAsync($"streams_online_del:{stream.CreateKey()}");
|
||||
await db.KeyDeleteAsync($"streams_online_del:{stream.CreateKey()}");
|
||||
|
||||
foreach (string pair in data)
|
||||
{
|
||||
var pairArr = pair.Split(',');
|
||||
if (pairArr.Length != 2)
|
||||
continue;
|
||||
|
||||
if (!ulong.TryParse(pairArr[0], out var chId) || !ulong.TryParse(pairArr[1], out var msgId))
|
||||
continue;
|
||||
|
||||
try
|
||||
{
|
||||
var textChannel = await _client.GetChannelAsync(chId) as ITextChannel;
|
||||
if (textChannel is null)
|
||||
continue;
|
||||
|
||||
await textChannel.DeleteMessageAsync(msgId);
|
||||
}
|
||||
catch
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private async ValueTask HandleStreamsOnline(List<StreamData> onlineStreams)
|
||||
{
|
||||
@@ -388,11 +331,14 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
|
||||
{
|
||||
var pairs = messages
|
||||
.Where(x => x != default)
|
||||
.Select(x => (x.Item1, x.Item2))
|
||||
.ToList();
|
||||
.Select(x => (RedisValue)$"{x.Item1},{x.Item2}")
|
||||
.ToArray();
|
||||
|
||||
if (pairs.Count > 0)
|
||||
await OnlineMessagesSent(key.Type, key.Name, pairs);
|
||||
if (pairs.Length > 0)
|
||||
{
|
||||
var db = _redis.GetDatabase();
|
||||
await db.ListRightPushAsync($"streams_online_del:{key}", pairs);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -403,10 +349,10 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
|
||||
}
|
||||
|
||||
private Task OnStreamsOnline(List<StreamData> data)
|
||||
=> _pubSub.Pub(StreamsOnlineKey, data);
|
||||
=> _pubSub.Pub(_streamsOnlineKey, data);
|
||||
|
||||
private Task OnStreamsOffline(List<StreamData> data)
|
||||
=> _pubSub.Pub(StreamsOfflineKey, data);
|
||||
=> _pubSub.Pub(_streamsOfflineKey, data);
|
||||
|
||||
private Task ClientOnJoinedGuild(GuildConfig guildConfig)
|
||||
{
|
||||
|
@@ -16,11 +16,11 @@ public class CmcQuote
|
||||
[JsonPropertyName("volume_24h")]
|
||||
public double Volume24h { get; set; }
|
||||
|
||||
// [JsonPropertyName("volume_change_24h")]
|
||||
// public double VolumeChange24h { get; set; }
|
||||
//
|
||||
// [JsonPropertyName("percent_change_1h")]
|
||||
// public double PercentChange1h { get; set; }
|
||||
[JsonPropertyName("volume_change_24h")]
|
||||
public double VolumeChange24h { get; set; }
|
||||
|
||||
[JsonPropertyName("percent_change_1h")]
|
||||
public double PercentChange1h { get; set; }
|
||||
|
||||
[JsonPropertyName("percent_change_24h")]
|
||||
public double PercentChange24h { get; set; }
|
||||
@@ -33,6 +33,12 @@ public class CmcQuote
|
||||
|
||||
[JsonPropertyName("market_cap_dominance")]
|
||||
public double MarketCapDominance { get; set; }
|
||||
|
||||
[JsonPropertyName("fully_diluted_market_cap")]
|
||||
public double FullyDilutedMarketCap { get; set; }
|
||||
|
||||
[JsonPropertyName("last_updated")]
|
||||
public DateTime LastUpdated { get; set; }
|
||||
}
|
||||
|
||||
public class CmcResponseData
|
||||
@@ -52,6 +58,9 @@ public class CmcResponseData
|
||||
[JsonPropertyName("cmc_rank")]
|
||||
public int CmcRank { get; set; }
|
||||
|
||||
[JsonPropertyName("num_market_pairs")]
|
||||
public int NumMarketPairs { get; set; }
|
||||
|
||||
[JsonPropertyName("circulating_supply")]
|
||||
public double? CirculatingSupply { get; set; }
|
||||
|
||||
@@ -61,6 +70,15 @@ public class CmcResponseData
|
||||
[JsonPropertyName("max_supply")]
|
||||
public double? MaxSupply { get; set; }
|
||||
|
||||
[JsonPropertyName("last_updated")]
|
||||
public DateTime LastUpdated { get; set; }
|
||||
|
||||
[JsonPropertyName("date_added")]
|
||||
public DateTime DateAdded { get; set; }
|
||||
|
||||
[JsonPropertyName("tags")]
|
||||
public List<string> Tags { get; set; }
|
||||
|
||||
[JsonPropertyName("quote")]
|
||||
public Dictionary<string, CmcQuote> Quote { get; set; }
|
||||
}
|
@@ -6,9 +6,10 @@ namespace NadekoBot.Modules.Searches.Common;
|
||||
|
||||
public readonly struct StreamDataKey
|
||||
{
|
||||
public FollowedStream.FType Type { get; init; }
|
||||
public string Name { get; init; }
|
||||
public FollowedStream.FType Type { get; }
|
||||
public string Name { get; }
|
||||
|
||||
[JsonConstructor]
|
||||
public StreamDataKey(FollowedStream.FType type, string name)
|
||||
{
|
||||
Type = type;
|
||||
|
@@ -1,5 +1,7 @@
|
||||
using NadekoBot.Db.Models;
|
||||
using NadekoBot.Modules.Searches.Common.StreamNotifications.Providers;
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace NadekoBot.Modules.Searches.Common.StreamNotifications;
|
||||
|
||||
@@ -7,22 +9,30 @@ public class NotifChecker
|
||||
{
|
||||
public event Func<List<StreamData>, Task> OnStreamsOffline = _ => Task.CompletedTask;
|
||||
public event Func<List<StreamData>, Task> OnStreamsOnline = _ => Task.CompletedTask;
|
||||
private readonly ConnectionMultiplexer _multi;
|
||||
private readonly string _key;
|
||||
|
||||
private readonly IReadOnlyDictionary<FollowedStream.FType, Provider> _streamProviders;
|
||||
private readonly Dictionary<FollowedStream.FType, Provider> _streamProviders;
|
||||
private readonly HashSet<(FollowedStream.FType, string)> _offlineBuffer;
|
||||
private readonly ConcurrentDictionary<StreamDataKey, StreamData?> _cache = new();
|
||||
|
||||
public NotifChecker(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
IBotCredsProvider credsProvider)
|
||||
IBotCredsProvider credsProvider,
|
||||
ConnectionMultiplexer multi,
|
||||
string uniqueCacheKey,
|
||||
bool isMaster)
|
||||
{
|
||||
_streamProviders = new Dictionary<FollowedStream.FType, Provider>()
|
||||
_multi = multi;
|
||||
_key = $"{uniqueCacheKey}_followed_streams_data";
|
||||
_streamProviders = new()
|
||||
{
|
||||
{ FollowedStream.FType.Twitch, new TwitchHelixProvider(httpClientFactory, credsProvider) },
|
||||
{ FollowedStream.FType.Picarto, new PicartoProvider(httpClientFactory) },
|
||||
{ FollowedStream.FType.Trovo, new TrovoProvider(httpClientFactory, credsProvider) }
|
||||
};
|
||||
_offlineBuffer = new();
|
||||
if (isMaster)
|
||||
CacheClearAllData();
|
||||
}
|
||||
|
||||
// gets all streams which have been failing for more than the provided timespan
|
||||
@@ -51,7 +61,7 @@ public class NotifChecker
|
||||
{
|
||||
try
|
||||
{
|
||||
var allStreamData = GetAllData();
|
||||
var allStreamData = CacheGetAllData();
|
||||
|
||||
var oldStreamDataDict = allStreamData
|
||||
// group by type
|
||||
@@ -91,7 +101,7 @@ public class NotifChecker
|
||||
|| !typeDict.TryGetValue(key.Name, out var oldData)
|
||||
|| oldData is null)
|
||||
{
|
||||
AddLastData(key, newData, true);
|
||||
CacheAddData(key, newData, true);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -99,7 +109,7 @@ public class NotifChecker
|
||||
if (string.IsNullOrWhiteSpace(newData.Game))
|
||||
newData.Game = oldData.Game;
|
||||
|
||||
AddLastData(key, newData, true);
|
||||
CacheAddData(key, newData, true);
|
||||
|
||||
// if the stream is offline, we need to check if it was
|
||||
// marked as offline once previously
|
||||
@@ -148,22 +158,39 @@ public class NotifChecker
|
||||
}
|
||||
});
|
||||
|
||||
public bool AddLastData(StreamDataKey key, StreamData? data, bool replace)
|
||||
public bool CacheAddData(StreamDataKey key, StreamData? data, bool replace)
|
||||
{
|
||||
if (replace)
|
||||
{
|
||||
_cache[key] = data;
|
||||
return true;
|
||||
}
|
||||
|
||||
return _cache.TryAdd(key, data);
|
||||
var db = _multi.GetDatabase();
|
||||
return db.HashSet(_key,
|
||||
JsonConvert.SerializeObject(key),
|
||||
JsonConvert.SerializeObject(data),
|
||||
replace ? When.Always : When.NotExists);
|
||||
}
|
||||
|
||||
public void DeleteLastData(StreamDataKey key)
|
||||
=> _cache.TryRemove(key, out _);
|
||||
public void CacheDeleteData(StreamDataKey key)
|
||||
{
|
||||
var db = _multi.GetDatabase();
|
||||
db.HashDelete(_key, JsonConvert.SerializeObject(key));
|
||||
}
|
||||
|
||||
public Dictionary<StreamDataKey, StreamData?> GetAllData()
|
||||
=> _cache.ToDictionary(x => x.Key, x => x.Value);
|
||||
public void CacheClearAllData()
|
||||
{
|
||||
var db = _multi.GetDatabase();
|
||||
db.KeyDelete(_key);
|
||||
}
|
||||
|
||||
public Dictionary<StreamDataKey, StreamData?> CacheGetAllData()
|
||||
{
|
||||
var db = _multi.GetDatabase();
|
||||
if (!db.KeyExists(_key))
|
||||
return new();
|
||||
|
||||
return db.HashGetAll(_key)
|
||||
.ToDictionary(entry => JsonConvert.DeserializeObject<StreamDataKey>(entry.Name),
|
||||
entry => entry.Value.IsNullOrEmpty
|
||||
? default
|
||||
: JsonConvert.DeserializeObject<StreamData>(entry.Value));
|
||||
}
|
||||
|
||||
public async Task<StreamData?> GetStreamDataByUrlAsync(string url)
|
||||
{
|
||||
@@ -207,9 +234,9 @@ public class NotifChecker
|
||||
|
||||
// if stream is found, add it to the cache for tracking only if it doesn't already exist
|
||||
// because stream will be checked and events will fire in a loop. We don't want to override old state
|
||||
return AddLastData(data.CreateKey(), data, false);
|
||||
return CacheAddData(data.CreateKey(), data, false);
|
||||
}
|
||||
|
||||
public void UntrackStreamByKey(in StreamDataKey key)
|
||||
=> DeleteLastData(key);
|
||||
=> CacheDeleteData(key);
|
||||
}
|
@@ -7,7 +7,7 @@ namespace NadekoBot.Modules.Utility.Patronage;
|
||||
public partial class PatronConfigData : ICloneable<PatronConfigData>
|
||||
{
|
||||
[Comment("DO NOT CHANGE")]
|
||||
public int Version { get; set; } = 2;
|
||||
public int Version { get; set; } = 1;
|
||||
|
||||
[Comment("Whether the patronage feature is enabled")]
|
||||
public bool IsEnabled { get; set; }
|
||||
|
@@ -18,19 +18,5 @@ public class PatronageConfig : ConfigServiceBase<PatronConfigData>
|
||||
x => x.IsEnabled,
|
||||
bool.TryParse,
|
||||
ConfigPrinters.ToString);
|
||||
|
||||
Migrate();
|
||||
}
|
||||
|
||||
private void Migrate()
|
||||
{
|
||||
ModifyConfig(c =>
|
||||
{
|
||||
if (c.Version == 1)
|
||||
{
|
||||
c.Version = 2;
|
||||
c.IsEnabled = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@@ -3,7 +3,6 @@ using LinqToDB.EntityFrameworkCore;
|
||||
using NadekoBot.Common.ModuleBehaviors;
|
||||
using NadekoBot.Db.Models;
|
||||
using OneOf;
|
||||
using OneOf.Types;
|
||||
using StackExchange.Redis;
|
||||
using CommandInfo = Discord.Commands.CommandInfo;
|
||||
|
||||
@@ -31,11 +30,9 @@ public sealed class PatronageService
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly ISubscriptionHandler _subsHandler;
|
||||
private readonly IEmbedBuilderService _eb;
|
||||
private static readonly TypedKey<long> _quotaKey
|
||||
= new($"quota:last_hourly_reset");
|
||||
|
||||
private readonly IBotCache _cache;
|
||||
private readonly IBotCredsProvider _creds;
|
||||
private readonly ConnectionMultiplexer _redis;
|
||||
private readonly IBotCredentials _creds;
|
||||
private readonly TypedKey<bool> _quotaKey;
|
||||
|
||||
public PatronageService(
|
||||
PatronageConfig pConf,
|
||||
@@ -43,16 +40,18 @@ public sealed class PatronageService
|
||||
DiscordSocketClient client,
|
||||
ISubscriptionHandler subsHandler,
|
||||
IEmbedBuilderService eb,
|
||||
IBotCache cache,
|
||||
IBotCredsProvider creds)
|
||||
ConnectionMultiplexer redis,
|
||||
IBotCredentials creds)
|
||||
{
|
||||
_pConf = pConf;
|
||||
_db = db;
|
||||
_client = client;
|
||||
_subsHandler = subsHandler;
|
||||
_eb = eb;
|
||||
_cache = cache;
|
||||
_redis = redis;
|
||||
_creds = creds;
|
||||
|
||||
_quotaKey = new TypedKey<bool>($"{_creds.RedisKey()}:quota:last_hourly_reset");
|
||||
}
|
||||
|
||||
public Task OnReadyAsync()
|
||||
@@ -101,10 +100,11 @@ public sealed class PatronageService
|
||||
var now = DateTime.UtcNow;
|
||||
var lastRun = DateTime.MinValue;
|
||||
|
||||
var result = await _cache.GetAsync(_quotaKey);
|
||||
if (result.TryGetValue(out var lastVal) && lastVal != default)
|
||||
var rdb = _redis.GetDatabase();
|
||||
var lastVal = await rdb.StringGetAsync(_quotaKey.Key);
|
||||
if (lastVal != default)
|
||||
{
|
||||
lastRun = DateTime.FromBinary(lastVal);
|
||||
lastRun = DateTime.FromBinary((long)lastVal);
|
||||
}
|
||||
|
||||
var nowDate = now.ToDateOnly();
|
||||
@@ -129,6 +129,8 @@ public sealed class PatronageService
|
||||
HourlyCount = 0,
|
||||
DailyCount = 0,
|
||||
});
|
||||
|
||||
await rdb.StringSetAsync(_quotaKey.Key, true);
|
||||
}
|
||||
else if (now.Hour != lastRun.Hour) // if it's not, just reset hourly quotas
|
||||
{
|
||||
@@ -140,7 +142,7 @@ public sealed class PatronageService
|
||||
}
|
||||
|
||||
// assumes that the code above runs in less than an hour
|
||||
await _cache.AddAsync(_quotaKey, now.ToBinary());
|
||||
await rdb.StringSetAsync(_quotaKey.Key, now.ToBinary());
|
||||
await tran.CommitAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -498,7 +500,7 @@ public sealed class PatronageService
|
||||
if (!confData.IsEnabled)
|
||||
return default;
|
||||
|
||||
if (_creds.GetCreds().IsOwner(userId))
|
||||
if (_creds.IsOwner(userId))
|
||||
return default;
|
||||
|
||||
// get user tier
|
||||
@@ -556,9 +558,7 @@ public sealed class PatronageService
|
||||
data.TryGetValue(QuotaPer.PerMonth, out var monthly) ? monthly : null
|
||||
);
|
||||
|
||||
return quotaCheckResult.Match<OneOf<Success, InsufficientTier, QuotaLimit>>(
|
||||
_ => new Success(),
|
||||
x => x);
|
||||
return quotaCheckResult.Match(_ => default, x => x);
|
||||
}
|
||||
|
||||
private bool TryGetTierDataOrLower<T>(
|
||||
|
@@ -1,24 +1,23 @@
|
||||
#nullable disable
|
||||
using NadekoBot.Common.ModuleBehaviors;
|
||||
using NadekoBot.Modules.Utility.Common;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NadekoBot.Modules.Utility.Services;
|
||||
|
||||
public class ConverterService : INService, IReadyExecutor
|
||||
{
|
||||
private static readonly TypedKey<List<ConvertUnit>> _convertKey =
|
||||
new("convert:units");
|
||||
public ConvertUnit[] Units
|
||||
=> _cache.Redis.GetDatabase().StringGet("converter_units").ToString().MapJson<ConvertUnit[]>();
|
||||
|
||||
private readonly TimeSpan _updateInterval = new(12, 0, 0);
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly IBotCache _cache;
|
||||
private readonly IDataCache _cache;
|
||||
private readonly IHttpClientFactory _httpFactory;
|
||||
|
||||
public ConverterService(
|
||||
DiscordSocketClient client,
|
||||
IBotCache cache,
|
||||
IDataCache cache,
|
||||
IHttpClientFactory factory)
|
||||
{
|
||||
_client = client;
|
||||
@@ -49,7 +48,7 @@ public class ConverterService : INService, IReadyExecutor
|
||||
{
|
||||
using var http = _httpFactory.CreateClient();
|
||||
var res = await http.GetStringAsync("https://convertapi.nadeko.bot/latest");
|
||||
return JsonSerializer.Deserialize<Rates>(res);
|
||||
return JsonConvert.DeserializeObject<Rates>(res);
|
||||
}
|
||||
|
||||
private async Task UpdateCurrency()
|
||||
@@ -62,38 +61,29 @@ public class ConverterService : INService, IReadyExecutor
|
||||
Modifier = decimal.One,
|
||||
UnitType = unitTypeString
|
||||
};
|
||||
var units = currencyRates.ConversionRates.Select(u => new ConvertUnit
|
||||
var range = currencyRates.ConversionRates.Select(u => new ConvertUnit
|
||||
{
|
||||
Triggers = new[] { u.Key },
|
||||
Modifier = u.Value,
|
||||
UnitType = unitTypeString
|
||||
})
|
||||
.ToList();
|
||||
.ToArray();
|
||||
|
||||
var stream = File.OpenRead("data/units.json");
|
||||
var defaultUnits = await JsonSerializer.DeserializeAsync<ConvertUnit[]>(stream);
|
||||
if(defaultUnits is not null)
|
||||
units.AddRange(defaultUnits);
|
||||
|
||||
units.Add(baseType);
|
||||
|
||||
await _cache.AddAsync(_convertKey, units);
|
||||
var fileData = JsonConvert.DeserializeObject<ConvertUnit[]>(File.ReadAllText("data/units.json"))
|
||||
?.Where(x => x.UnitType != "currency");
|
||||
if (fileData is null)
|
||||
return;
|
||||
|
||||
var data = JsonConvert.SerializeObject(range.Append(baseType).Concat(fileData).ToList());
|
||||
_cache.Redis.GetDatabase().StringSet("converter_units", data);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<ConvertUnit>> GetUnitsAsync()
|
||||
=> (await _cache.GetAsync(_convertKey)).TryGetValue(out var list)
|
||||
? list
|
||||
: Array.Empty<ConvertUnit>();
|
||||
}
|
||||
|
||||
public class Rates
|
||||
{
|
||||
[JsonPropertyName("base")]
|
||||
public string Base { get; set; }
|
||||
|
||||
[JsonPropertyName("date")]
|
||||
public DateTime Date { get; set; }
|
||||
|
||||
[JsonPropertyName("rates")]
|
||||
[JsonProperty("rates")]
|
||||
public Dictionary<string, decimal> ConversionRates { get; set; }
|
||||
}
|
@@ -11,7 +11,7 @@ public partial class Utility
|
||||
[Cmd]
|
||||
public async partial Task ConvertList()
|
||||
{
|
||||
var units = await _service.GetUnitsAsync();
|
||||
var units = _service.Units;
|
||||
|
||||
var embed = _eb.Create().WithTitle(GetText(strs.convertlist)).WithOkColor();
|
||||
|
||||
@@ -29,10 +29,9 @@ public partial class Utility
|
||||
[Priority(0)]
|
||||
public async partial Task Convert(string origin, string target, decimal value)
|
||||
{
|
||||
var units = await _service.GetUnitsAsync();
|
||||
var originUnit = units.FirstOrDefault(x
|
||||
var originUnit = _service.Units.FirstOrDefault(x
|
||||
=> x.Triggers.Select(y => y.ToUpperInvariant()).Contains(origin.ToUpperInvariant()));
|
||||
var targetUnit = units.FirstOrDefault(x
|
||||
var targetUnit = _service.Units.FirstOrDefault(x
|
||||
=> x.Triggers.Select(y => y.ToUpperInvariant()).Contains(target.ToUpperInvariant()));
|
||||
if (originUnit is null || targetUnit is null)
|
||||
{
|
||||
|
@@ -1,7 +1,4 @@
|
||||
#nullable disable
|
||||
using Microsoft.CodeAnalysis.CSharp.Scripting;
|
||||
using Microsoft.CodeAnalysis.Scripting;
|
||||
using NadekoBot.Modules.Utility.Services;
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
@@ -35,7 +32,6 @@ public partial class Utility : NadekoModule
|
||||
private readonly IBotCredentials _creds;
|
||||
private readonly DownloadTracker _tracker;
|
||||
private readonly IHttpClientFactory _httpFactory;
|
||||
private readonly VerboseErrorsService _veService;
|
||||
|
||||
public Utility(
|
||||
DiscordSocketClient client,
|
||||
@@ -43,8 +39,7 @@ public partial class Utility : NadekoModule
|
||||
IStatsService stats,
|
||||
IBotCredentials creds,
|
||||
DownloadTracker tracker,
|
||||
IHttpClientFactory httpFactory,
|
||||
VerboseErrorsService veService)
|
||||
IHttpClientFactory httpFactory)
|
||||
{
|
||||
_client = client;
|
||||
_coord = coord;
|
||||
@@ -52,7 +47,6 @@ public partial class Utility : NadekoModule
|
||||
_creds = creds;
|
||||
_tracker = tracker;
|
||||
_httpFactory = httpFactory;
|
||||
_veService = veService;
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
@@ -488,17 +482,44 @@ public partial class Utility : NadekoModule
|
||||
sem.Release();
|
||||
}
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
[RequireContext(ContextType.Guild)]
|
||||
[UserPerm(GuildPerm.ManageMessages)]
|
||||
public async partial Task VerboseError(bool? newstate = null)
|
||||
{
|
||||
var state = _veService.ToggleVerboseErrors(ctx.Guild.Id, newstate);
|
||||
|
||||
if (state)
|
||||
await ReplyConfirmLocalizedAsync(strs.verbose_errors_enabled);
|
||||
else
|
||||
await ReplyConfirmLocalizedAsync(strs.verbose_errors_disabled);
|
||||
}
|
||||
|
||||
// [NadekoCommand, Usage, Description, Aliases]
|
||||
// [RequireContext(ContextType.Guild)]
|
||||
// public async Task CreateMyInvite(CreateInviteType type = CreateInviteType.Any)
|
||||
// {
|
||||
// if (type == CreateInviteType.Any)
|
||||
// {
|
||||
// if (_inviteService.TryGetInvite(type, out var code))
|
||||
// {
|
||||
// await ReplyErrorLocalizedAsync(strs.your_invite($"https://discord.gg/{code}"));
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// var invite = await ((ITextChannel) ctx.Channel).CreateInviteAsync(isUnique: true);
|
||||
// }
|
||||
//
|
||||
// [NadekoCommand, Usage, Description, Aliases]
|
||||
// [RequireContext(ContextType.Guild)]
|
||||
// public async partial Task InviteLb(int page = 1)
|
||||
// {
|
||||
// if (--page < 0)
|
||||
// return;
|
||||
//
|
||||
// var inviteUsers = await _inviteService.GetInviteUsersAsync(ctx.Guild.Id);
|
||||
//
|
||||
// var embed = _eb.Create()
|
||||
// .WithOkColor();
|
||||
//
|
||||
// await ctx.SendPaginatedConfirmAsync(page, (curPage) =>
|
||||
// {
|
||||
// var items = inviteUsers.Skip(curPage * 9).Take(9);
|
||||
// var i = 0;
|
||||
// foreach (var item in items)
|
||||
// embed.AddField($"#{curPage * 9 + ++i} {item.UserName} [{item.User.Id}]", item.InvitedUsers);
|
||||
//
|
||||
// return embed;
|
||||
// }, inviteUsers.Count, 9);
|
||||
// }
|
||||
}
|
@@ -1,76 +0,0 @@
|
||||
#nullable disable
|
||||
using Microsoft.CodeAnalysis.CSharp.Scripting;
|
||||
using Microsoft.CodeAnalysis.Scripting;
|
||||
|
||||
namespace NadekoBot.Modules.Utility;
|
||||
|
||||
public partial class Utility
|
||||
{
|
||||
[Group]
|
||||
public partial class EvalCommands : NadekoModule
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
|
||||
public EvalCommands(IServiceProvider services)
|
||||
{
|
||||
_services = services;
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
[NoPublicBot]
|
||||
[OwnerOnly]
|
||||
public async partial Task Eval([Leftover] string scriptText)
|
||||
{
|
||||
_ = ctx.Channel.TriggerTypingAsync();
|
||||
|
||||
if (scriptText.StartsWith("```cs"))
|
||||
scriptText = scriptText[5..];
|
||||
else if (scriptText.StartsWith("```"))
|
||||
scriptText = scriptText[3..];
|
||||
|
||||
if (scriptText.EndsWith("```"))
|
||||
scriptText = scriptText[..^3];
|
||||
|
||||
var script = CSharpScript.Create(scriptText,
|
||||
ScriptOptions.Default
|
||||
.WithReferences(this.GetType().Assembly)
|
||||
.WithImports(
|
||||
"System",
|
||||
"NadekoBot",
|
||||
"NadekoBot.Extensions",
|
||||
"Microsoft.Extensions.DependencyInjection",
|
||||
"NadekoBot.Common",
|
||||
"System.Text",
|
||||
"System.Text.Json"),
|
||||
globalsType: typeof(EvalGlobals));
|
||||
|
||||
try
|
||||
{
|
||||
var result = await script.RunAsync(new EvalGlobals()
|
||||
{
|
||||
ctx = this.ctx,
|
||||
guild = this.ctx.Guild,
|
||||
channel = this.ctx.Channel,
|
||||
user = this.ctx.User,
|
||||
self = this,
|
||||
services = _services
|
||||
});
|
||||
|
||||
var output = result.ReturnValue?.ToString();
|
||||
if (!string.IsNullOrWhiteSpace(output))
|
||||
{
|
||||
var eb = _eb.Create(ctx)
|
||||
.WithOkColor()
|
||||
.AddField("Code", scriptText)
|
||||
.AddField("Output", output.TrimTo(512)!);
|
||||
|
||||
_ = ctx.Channel.EmbedAsync(eb);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await SendErrorAsync(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,13 +0,0 @@
|
||||
// ReSharper disable InconsistentNaming
|
||||
#nullable disable
|
||||
namespace NadekoBot.Modules.Utility;
|
||||
|
||||
public class EvalGlobals
|
||||
{
|
||||
public ICommandContext ctx;
|
||||
public Utility.EvalCommands self;
|
||||
public IUser user;
|
||||
public IMessageChannel channel;
|
||||
public IGuild guild;
|
||||
public IServiceProvider services;
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
#nullable disable
|
||||
using NadekoBot.Modules.Utility.Services;
|
||||
|
||||
namespace NadekoBot.Modules.Utility;
|
||||
|
||||
public partial class Utility
|
||||
{
|
||||
[Group]
|
||||
public partial class VerboseErrorCommands : NadekoModule<VerboseErrorsService>
|
||||
{
|
||||
[Cmd]
|
||||
[RequireContext(ContextType.Guild)]
|
||||
[UserPerm(GuildPerm.ManageMessages)]
|
||||
public async partial Task VerboseError(bool? newstate = null)
|
||||
{
|
||||
var state = _service.ToggleVerboseErrors(ctx.Guild.Id, newstate);
|
||||
|
||||
if (state)
|
||||
await ReplyConfirmLocalizedAsync(strs.verbose_errors_enabled);
|
||||
else
|
||||
await ReplyConfirmLocalizedAsync(strs.verbose_errors_disabled);
|
||||
}
|
||||
}
|
||||
}
|
@@ -23,8 +23,10 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
public const int XP_REQUIRED_LVL_1 = 36;
|
||||
|
||||
private readonly DbService _db;
|
||||
private readonly CommandHandler _cmd;
|
||||
private readonly IImageCache _images;
|
||||
private readonly IBotStrings _strings;
|
||||
private readonly IDataCache _cache;
|
||||
private readonly FontProvider _fonts;
|
||||
private readonly IBotCredentials _creds;
|
||||
private readonly ICurrencyService _cs;
|
||||
@@ -37,21 +39,20 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
private readonly ConcurrentDictionary<ulong, ConcurrentHashSet<ulong>> _excludedChannels;
|
||||
private readonly ConcurrentHashSet<ulong> _excludedServers;
|
||||
|
||||
private readonly System.Collections.Concurrent.ConcurrentQueue<UserCacheItem> _addMessageXp = new();
|
||||
private readonly ConcurrentQueue<UserCacheItem> _addMessageXp = new();
|
||||
private XpTemplate template;
|
||||
private readonly DiscordSocketClient _client;
|
||||
|
||||
private readonly TypedKey<bool> _xpTemplateReloadKey;
|
||||
private readonly IPatronageService _ps;
|
||||
private readonly IBotCache _c;
|
||||
|
||||
public XpService(
|
||||
DiscordSocketClient client,
|
||||
CommandHandler cmd,
|
||||
Bot bot,
|
||||
DbService db,
|
||||
IBotStrings strings,
|
||||
IImageCache images,
|
||||
IBotCache c,
|
||||
IDataCache cache,
|
||||
FontProvider fonts,
|
||||
IBotCredentials creds,
|
||||
ICurrencyService cs,
|
||||
@@ -62,8 +63,10 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
IPatronageService ps)
|
||||
{
|
||||
_db = db;
|
||||
_images = images;
|
||||
_cmd = cmd;
|
||||
_images = cache.LocalImages;
|
||||
_strings = strings;
|
||||
_cache = cache;
|
||||
_fonts = fonts;
|
||||
_creds = creds;
|
||||
_cs = cs;
|
||||
@@ -76,7 +79,6 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
_client = client;
|
||||
_xpTemplateReloadKey = new("xp.template.reload");
|
||||
_ps = ps;
|
||||
_c = c;
|
||||
|
||||
InternalReloadXpTemplate();
|
||||
|
||||
@@ -451,10 +453,10 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
|
||||
private Task Client_OnGuildAvailable(SocketGuild guild)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
Task.Run(() =>
|
||||
{
|
||||
foreach (var channel in guild.VoiceChannels)
|
||||
await ScanChannelForVoiceXp(channel);
|
||||
ScanChannelForVoiceXp(channel);
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
@@ -465,33 +467,33 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
if (socketUser is not SocketGuildUser user || user.IsBot)
|
||||
return Task.CompletedTask;
|
||||
|
||||
_ = Task.Run(async () =>
|
||||
_ = Task.Run(() =>
|
||||
{
|
||||
if (before.VoiceChannel is not null)
|
||||
await ScanChannelForVoiceXp(before.VoiceChannel);
|
||||
ScanChannelForVoiceXp(before.VoiceChannel);
|
||||
|
||||
if (after.VoiceChannel is not null && after.VoiceChannel != before.VoiceChannel)
|
||||
await ScanChannelForVoiceXp(after.VoiceChannel);
|
||||
ScanChannelForVoiceXp(after.VoiceChannel);
|
||||
else if (after.VoiceChannel is null)
|
||||
// In this case, the user left the channel and the previous for loops didn't catch
|
||||
// it because it wasn't in any new channel. So we need to get rid of it.
|
||||
await UserLeftVoiceChannel(user, before.VoiceChannel);
|
||||
UserLeftVoiceChannel(user, before.VoiceChannel);
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task ScanChannelForVoiceXp(SocketVoiceChannel channel)
|
||||
private void ScanChannelForVoiceXp(SocketVoiceChannel channel)
|
||||
{
|
||||
if (ShouldTrackVoiceChannel(channel))
|
||||
{
|
||||
foreach (var user in channel.Users)
|
||||
await ScanUserForVoiceXp(user, channel);
|
||||
ScanUserForVoiceXp(user, channel);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var user in channel.Users)
|
||||
await UserLeftVoiceChannel(user, channel);
|
||||
UserLeftVoiceChannel(user, channel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -500,12 +502,12 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="channel"></param>
|
||||
private async Task ScanUserForVoiceXp(SocketGuildUser user, SocketVoiceChannel channel)
|
||||
private void ScanUserForVoiceXp(SocketGuildUser user, SocketVoiceChannel channel)
|
||||
{
|
||||
if (UserParticipatingInVoiceChannel(user) && ShouldTrackXp(user, channel.Id))
|
||||
await UserJoinedVoiceChannel(user);
|
||||
UserJoinedVoiceChannel(user);
|
||||
else
|
||||
await UserLeftVoiceChannel(user, channel);
|
||||
UserLeftVoiceChannel(user, channel);
|
||||
}
|
||||
|
||||
private bool ShouldTrackVoiceChannel(SocketVoiceChannel channel)
|
||||
@@ -514,31 +516,32 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
private bool UserParticipatingInVoiceChannel(SocketGuildUser user)
|
||||
=> !user.IsDeafened && !user.IsMuted && !user.IsSelfDeafened && !user.IsSelfMuted;
|
||||
|
||||
private TypedKey<long> GetVoiceXpKey(ulong userId)
|
||||
=> new($"xp:vc_join:{userId}");
|
||||
|
||||
private async Task UserJoinedVoiceChannel(SocketGuildUser user)
|
||||
private void UserJoinedVoiceChannel(SocketGuildUser user)
|
||||
{
|
||||
var key = $"{_creds.RedisKey()}_user_xp_vc_join_{user.Id}";
|
||||
var value = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||||
|
||||
await _c.AddAsync(GetVoiceXpKey(user.Id),
|
||||
value,
|
||||
TimeSpan.FromMinutes(_xpConfig.Data.VoiceMaxMinutes),
|
||||
overwrite: false);
|
||||
_cache.Redis.GetDatabase()
|
||||
.StringSet(key,
|
||||
value,
|
||||
TimeSpan.FromMinutes(_xpConfig.Data.VoiceMaxMinutes),
|
||||
when: When.NotExists);
|
||||
}
|
||||
|
||||
private async Task UserLeftVoiceChannel(SocketGuildUser user, SocketVoiceChannel channel)
|
||||
private void UserLeftVoiceChannel(SocketGuildUser user, SocketVoiceChannel channel)
|
||||
{
|
||||
var key = GetVoiceXpKey(user.Id);
|
||||
var result = await _c.GetAsync(key);
|
||||
if (!await _c.RemoveAsync(key))
|
||||
return;
|
||||
var key = $"{_creds.RedisKey()}_user_xp_vc_join_{user.Id}";
|
||||
var value = _cache.Redis.GetDatabase().StringGet(key);
|
||||
_cache.Redis.GetDatabase().KeyDelete(key);
|
||||
|
||||
// Allow for if this function gets called multiple times when a user leaves a channel.
|
||||
if (!result.TryGetValue(out var unixTime))
|
||||
if (value.IsNull)
|
||||
return;
|
||||
|
||||
var dateStart = DateTimeOffset.FromUnixTimeSeconds(unixTime);
|
||||
if (!value.TryParse(out long startUnixTime))
|
||||
return;
|
||||
|
||||
var dateStart = DateTimeOffset.FromUnixTimeSeconds(startUnixTime);
|
||||
var dateEnd = DateTimeOffset.UtcNow;
|
||||
var minutes = (dateEnd - dateStart).TotalMinutes;
|
||||
var xp = _xpConfig.Data.VoiceXpPerMinute * minutes;
|
||||
@@ -574,7 +577,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
if (arg.Author is not SocketGuildUser user || user.IsBot)
|
||||
return Task.CompletedTask;
|
||||
|
||||
_ = Task.Run(async () =>
|
||||
_ = Task.Run(() =>
|
||||
{
|
||||
if (!ShouldTrackXp(user, arg.Channel.Id))
|
||||
return;
|
||||
@@ -590,7 +593,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
if (xp <= 0)
|
||||
return;
|
||||
|
||||
if (!await SetUserRewardedAsync(user.Id))
|
||||
if (!SetUserRewarded(user.Id))
|
||||
return;
|
||||
|
||||
_addMessageXp.Enqueue(new()
|
||||
@@ -647,14 +650,16 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
return Enumerable.Empty<ulong>();
|
||||
}
|
||||
|
||||
private static TypedKey<bool> GetUserRewKey(ulong userId)
|
||||
=> new($"xp:user_gain:{userId}");
|
||||
private bool SetUserRewarded(ulong userId)
|
||||
{
|
||||
var r = _cache.Redis.GetDatabase();
|
||||
var key = $"{_creds.RedisKey()}_user_xp_gain_{userId}";
|
||||
|
||||
private async Task<bool> SetUserRewardedAsync(ulong userId)
|
||||
=> await _c.AddAsync(GetUserRewKey(userId),
|
||||
return r.StringSet(key,
|
||||
true,
|
||||
expiry: TimeSpan.FromMinutes(_xpConfig.Data.MessageXpCooldown),
|
||||
overwrite: false);
|
||||
TimeSpan.FromMinutes(_xpConfig.Data.MessageXpCooldown),
|
||||
when: When.NotExists);
|
||||
}
|
||||
|
||||
public async Task<FullUserStats> GetUserStatsAsync(IGuildUser user)
|
||||
{
|
||||
@@ -777,7 +782,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
}
|
||||
}.WithFallbackFonts(_fonts.FallBackFonts);
|
||||
|
||||
using var img = Image.Load<Rgba32>(await GetXpBackgroundAsync(stats.User.UserId), out var imageFormat);
|
||||
using var img = Image.Load<Rgba32>(_images.XpBackground, out var imageFormat);
|
||||
if (template.User.Name.Show)
|
||||
{
|
||||
var fontSize = (int)(template.User.Name.FontSize * 0.9);
|
||||
@@ -974,8 +979,8 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
{
|
||||
var avatarUrl = stats.User.RealAvatarUrl();
|
||||
|
||||
var result = await _c.GetImageDataAsync(avatarUrl);
|
||||
if (!result.TryPickT0(out var data, out _))
|
||||
var (succ, data) = await _cache.TryGetImageDataAsync(avatarUrl);
|
||||
if (!succ)
|
||||
{
|
||||
using (var http = _httpFactory.CreateClient())
|
||||
{
|
||||
@@ -994,7 +999,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
}
|
||||
}
|
||||
|
||||
await _c.SetImageDataAsync(avatarUrl, data);
|
||||
await _cache.SetImageDataAsync(avatarUrl, data);
|
||||
}
|
||||
|
||||
using var toDraw = Image.Load(data);
|
||||
@@ -1028,13 +1033,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
return output;
|
||||
});
|
||||
|
||||
private async Task<byte[]> GetXpBackgroundAsync(ulong userId)
|
||||
{
|
||||
var img = await _images.GetXpBackgroundImageAsync();
|
||||
return img;
|
||||
}
|
||||
|
||||
// #if GLOBAL_NADEKO
|
||||
// #if GLOBAL_NADEKO
|
||||
private async Task DrawFrame(Image<Rgba32> img, ulong userId)
|
||||
{
|
||||
var patron = await _ps.GetPatronAsync(userId);
|
||||
@@ -1104,8 +1103,8 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
try
|
||||
{
|
||||
var imgUrl = new Uri(stats.User.Club.ImageUrl);
|
||||
var result = await _c.GetImageDataAsync(imgUrl);
|
||||
if (!result.TryPickT0(out var data, out _))
|
||||
var (succ, data) = await _cache.TryGetImageDataAsync(imgUrl);
|
||||
if (!succ)
|
||||
{
|
||||
using (var http = _httpFactory.CreateClient())
|
||||
using (var temp = await http.GetAsync(imgUrl, HttpCompletionOption.ResponseHeadersRead))
|
||||
@@ -1128,7 +1127,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
||||
}
|
||||
}
|
||||
|
||||
await _c.SetImageDataAsync(imgUrl, data);
|
||||
await _cache.SetImageDataAsync(imgUrl, data);
|
||||
}
|
||||
|
||||
using var toDraw = Image.Load(data);
|
||||
|
@@ -39,7 +39,6 @@
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Html2Markdown" Version="5.0.2.561" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.2.0" />
|
||||
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
|
||||
@@ -53,7 +52,6 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.SyndicationFeed.ReaderWriter" Version="1.0.2" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
<PackageReference Include="NonBlocking" Version="2.0.0" />
|
||||
<PackageReference Include="OneOf" Version="3.0.216" />
|
||||
<PackageReference Include="Scrutor" Version="4.1.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
|
||||
@@ -118,7 +116,7 @@
|
||||
<None Update="nadeko_icon.ico;libopus.so;libsodium.so;libsodium.dll;opus.dll">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="creds.yml;creds_example.yml">
|
||||
<None Update="creds.yml">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
35
src/NadekoBot/Services/IDataCache.cs
Normal file
35
src/NadekoBot/Services/IDataCache.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
#nullable disable
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace NadekoBot.Services;
|
||||
|
||||
public interface IDataCache
|
||||
{
|
||||
ConnectionMultiplexer Redis { get; }
|
||||
IImageCache LocalImages { get; }
|
||||
ILocalDataCache LocalData { get; }
|
||||
|
||||
Task<(bool Success, byte[] Data)> TryGetImageDataAsync(Uri key);
|
||||
Task<(bool Success, string Data)> TryGetAnimeDataAsync(string key);
|
||||
Task<(bool Success, string Data)> TryGetNovelDataAsync(string key);
|
||||
Task SetImageDataAsync(Uri key, byte[] data);
|
||||
Task SetAnimeDataAsync(string link, string data);
|
||||
Task SetNovelDataAsync(string link, string data);
|
||||
TimeSpan? AddTimelyClaim(ulong id, int period);
|
||||
TimeSpan? TryAddRatelimit(ulong id, string name, int expireIn);
|
||||
void RemoveAllTimelyClaims();
|
||||
bool TryAddAffinityCooldown(ulong userId, out TimeSpan? time);
|
||||
bool TryAddDivorceCooldown(ulong userId, out TimeSpan? time);
|
||||
bool TryGetEconomy(out string data);
|
||||
void SetEconomy(string data);
|
||||
|
||||
Task<TOut> GetOrAddCachedDataAsync<TParam, TOut>(
|
||||
string key,
|
||||
Func<TParam, Task<TOut>> factory,
|
||||
TParam param,
|
||||
TimeSpan expiry)
|
||||
where TOut : class;
|
||||
|
||||
DateTime GetLastCurrencyDecay();
|
||||
void SetLastCurrencyDecay();
|
||||
}
|
29
src/NadekoBot/Services/IImageCache.cs
Normal file
29
src/NadekoBot/Services/IImageCache.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
#nullable disable
|
||||
namespace NadekoBot.Services;
|
||||
|
||||
public interface IImageCache
|
||||
{
|
||||
ImageUrls ImageUrls { get; }
|
||||
|
||||
IReadOnlyList<byte[]> Heads { get; }
|
||||
IReadOnlyList<byte[]> Tails { get; }
|
||||
|
||||
IReadOnlyList<byte[]> Dice { get; }
|
||||
|
||||
IReadOnlyList<byte[]> SlotEmojis { get; }
|
||||
IReadOnlyList<byte[]> Currency { get; }
|
||||
|
||||
byte[] SlotBackground { get; }
|
||||
|
||||
byte[] RategirlMatrix { get; }
|
||||
byte[] RategirlDot { get; }
|
||||
|
||||
byte[] XpBackground { get; }
|
||||
|
||||
byte[] Rip { get; }
|
||||
byte[] RipOverlay { get; }
|
||||
|
||||
byte[] GetCard(string key);
|
||||
|
||||
Task Reload();
|
||||
}
|
@@ -6,8 +6,8 @@ namespace NadekoBot.Services;
|
||||
|
||||
public interface ILocalDataCache
|
||||
{
|
||||
Task<IReadOnlyDictionary<string, SearchPokemon>> GetPokemonsAsync();
|
||||
Task<IReadOnlyDictionary<string, SearchPokemonAbility>> GetPokemonAbilitiesAsync();
|
||||
Task<TriviaQuestionModel[]> GetTriviaQuestionsAsync();
|
||||
Task<IReadOnlyDictionary<int, string>> GetPokemonMapAsync();
|
||||
IReadOnlyDictionary<string, SearchPokemon> Pokemons { get; }
|
||||
IReadOnlyDictionary<string, SearchPokemonAbility> PokemonAbilities { get; }
|
||||
IReadOnlyDictionary<int, string> PokemonMap { get; }
|
||||
TriviaQuestion[] TriviaQuestions { get; }
|
||||
}
|
@@ -172,10 +172,9 @@ public sealed class BotCredsProvider : IBotCredsProvider
|
||||
if (File.Exists(CREDS_FILE_NAME))
|
||||
{
|
||||
var creds = Yaml.Deserializer.Deserialize<Creds>(File.ReadAllText(CREDS_FILE_NAME));
|
||||
if (creds.Version <= 5)
|
||||
if (creds.Version <= 4)
|
||||
{
|
||||
creds.Version = 6;
|
||||
creds.BotCache = BotCacheImplemenation.Redis;
|
||||
creds.Version = 5;
|
||||
File.WriteAllText(CREDS_FILE_NAME, Yaml.Serializer.Serialize(creds));
|
||||
}
|
||||
}
|
||||
|
@@ -1,108 +0,0 @@
|
||||
using NadekoBot.Common.Pokemon;
|
||||
using NadekoBot.Modules.Games.Common.Trivia;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace NadekoBot.Services;
|
||||
|
||||
public sealed class LocalDataCache : ILocalDataCache, INService
|
||||
{
|
||||
private const string POKEMON_ABILITIES_FILE = "data/pokemon/pokemon_abilities.json";
|
||||
private const string POKEMON_LIST_FILE = "data/pokemon/pokemon_list.json";
|
||||
private const string POKEMON_MAP_PATH = "data/pokemon/name-id_map.json";
|
||||
private const string QUESTIONS_FILE = "data/trivia_questions.json";
|
||||
|
||||
private readonly IBotCache _cache;
|
||||
|
||||
private readonly JsonSerializerOptions _opts = new JsonSerializerOptions()
|
||||
{
|
||||
AllowTrailingCommas = true,
|
||||
NumberHandling = JsonNumberHandling.AllowReadingFromString,
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
public LocalDataCache(IBotCache cache)
|
||||
=> _cache = cache;
|
||||
|
||||
private async Task<T?> GetOrCreateCachedDataAsync<T>(
|
||||
TypedKey<T> key,
|
||||
string fileName)
|
||||
=> await _cache.GetOrAddAsync(key,
|
||||
async () =>
|
||||
{
|
||||
if (!File.Exists(fileName))
|
||||
{
|
||||
Log.Warning($"{fileName} is missing. Relevant data can't be loaded");
|
||||
return default;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await using var stream = File.OpenRead(fileName);
|
||||
return await JsonSerializer.DeserializeAsync<T>(stream, _opts);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex,
|
||||
"Error reading {FileName} file: {ErrorMessage}",
|
||||
fileName,
|
||||
ex.Message);
|
||||
|
||||
return default;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
private static TypedKey<IReadOnlyDictionary<string, SearchPokemon>> _pokemonListKey
|
||||
= new("pokemon:list");
|
||||
|
||||
public async Task<IReadOnlyDictionary<string, SearchPokemon>?> GetPokemonsAsync()
|
||||
=> await GetOrCreateCachedDataAsync(_pokemonListKey, POKEMON_LIST_FILE);
|
||||
|
||||
|
||||
private static TypedKey<IReadOnlyDictionary<string, SearchPokemonAbility>> _pokemonAbilitiesKey
|
||||
= new("pokemon:abilities");
|
||||
|
||||
public async Task<IReadOnlyDictionary<string, SearchPokemonAbility>?> GetPokemonAbilitiesAsync()
|
||||
=> await GetOrCreateCachedDataAsync(_pokemonAbilitiesKey, POKEMON_ABILITIES_FILE);
|
||||
|
||||
|
||||
private static TypedKey<IReadOnlyDictionary<int, string>> _pokeMapKey
|
||||
= new("pokemon:ab_map2"); // 2 because ab_map was storing arrays
|
||||
|
||||
public async Task<IReadOnlyDictionary<int, string>?> GetPokemonMapAsync()
|
||||
=> await _cache.GetOrAddAsync(_pokeMapKey,
|
||||
async () =>
|
||||
{
|
||||
var fileName = POKEMON_MAP_PATH;
|
||||
if (!File.Exists(fileName))
|
||||
{
|
||||
Log.Warning($"{fileName} is missing. Relevant data can't be loaded");
|
||||
return default;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await using var stream = File.OpenRead(fileName);
|
||||
var arr = await JsonSerializer.DeserializeAsync<PokemonNameId[]>(stream, _opts);
|
||||
|
||||
return (IReadOnlyDictionary<int, string>?)arr?.ToDictionary(x => x.Id, x => x.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex,
|
||||
"Error reading {FileName} file: {ErrorMessage}",
|
||||
fileName,
|
||||
ex.Message);
|
||||
|
||||
return default;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
private static TypedKey<TriviaQuestionModel[]> _triviaKey
|
||||
= new("trivia:questions");
|
||||
|
||||
public async Task<TriviaQuestionModel[]?> GetTriviaQuestionsAsync()
|
||||
=> await GetOrCreateCachedDataAsync(_triviaKey, QUESTIONS_FILE);
|
||||
}
|
216
src/NadekoBot/Services/Impl/RedisCache.cs
Normal file
216
src/NadekoBot/Services/Impl/RedisCache.cs
Normal file
@@ -0,0 +1,216 @@
|
||||
#nullable disable
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
using System.Net;
|
||||
|
||||
namespace NadekoBot.Services;
|
||||
|
||||
public class RedisCache : IDataCache
|
||||
{
|
||||
public ConnectionMultiplexer Redis { get; }
|
||||
|
||||
public IImageCache LocalImages { get; }
|
||||
public ILocalDataCache LocalData { get; }
|
||||
|
||||
private readonly string _redisKey;
|
||||
private readonly EndPoint _redisEndpoint;
|
||||
|
||||
private readonly object _timelyLock = new();
|
||||
|
||||
public RedisCache(
|
||||
ConnectionMultiplexer redis,
|
||||
IBotCredentials creds,
|
||||
IImageCache imageCache,
|
||||
ILocalDataCache dataCache)
|
||||
{
|
||||
Redis = redis;
|
||||
_redisEndpoint = Redis.GetEndPoints().First();
|
||||
LocalImages = imageCache;
|
||||
LocalData = dataCache;
|
||||
_redisKey = creds.RedisKey();
|
||||
}
|
||||
|
||||
// things here so far don't need the bot id
|
||||
// because it's a good thing if different bots
|
||||
// which are hosted on the same PC
|
||||
// can re-use the same image/anime data
|
||||
public async Task<(bool Success, byte[] Data)> TryGetImageDataAsync(Uri key)
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
byte[] x = await db.StringGetAsync("image_" + key);
|
||||
return (x is not null, x);
|
||||
}
|
||||
|
||||
public Task SetImageDataAsync(Uri key, byte[] data)
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
return db.StringSetAsync("image_" + key, data);
|
||||
}
|
||||
|
||||
public async Task<(bool Success, string Data)> TryGetAnimeDataAsync(string key)
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
string x = await db.StringGetAsync("anime_" + key);
|
||||
return (x is not null, x);
|
||||
}
|
||||
|
||||
public Task SetAnimeDataAsync(string key, string data)
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
return db.StringSetAsync("anime_" + key, data, TimeSpan.FromHours(3));
|
||||
}
|
||||
|
||||
public async Task<(bool Success, string Data)> TryGetNovelDataAsync(string key)
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
string x = await db.StringGetAsync("novel_" + key);
|
||||
return (x is not null, x);
|
||||
}
|
||||
|
||||
public Task SetNovelDataAsync(string key, string data)
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
return db.StringSetAsync("novel_" + key, data, TimeSpan.FromHours(3));
|
||||
}
|
||||
|
||||
public TimeSpan? AddTimelyClaim(ulong id, int period)
|
||||
{
|
||||
if (period == 0)
|
||||
return null;
|
||||
lock (_timelyLock)
|
||||
{
|
||||
var time = TimeSpan.FromHours(period);
|
||||
var db = Redis.GetDatabase();
|
||||
if ((bool?)db.StringGet($"{_redisKey}_timelyclaim_{id}") is null)
|
||||
{
|
||||
db.StringSet($"{_redisKey}_timelyclaim_{id}", true, time);
|
||||
return null;
|
||||
}
|
||||
|
||||
return db.KeyTimeToLive($"{_redisKey}_timelyclaim_{id}");
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveAllTimelyClaims()
|
||||
{
|
||||
var server = Redis.GetServer(_redisEndpoint);
|
||||
var db = Redis.GetDatabase();
|
||||
foreach (var k in server.Keys(pattern: $"{_redisKey}_timelyclaim_*"))
|
||||
db.KeyDelete(k, CommandFlags.FireAndForget);
|
||||
}
|
||||
|
||||
public bool TryAddAffinityCooldown(ulong userId, out TimeSpan? time)
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
time = db.KeyTimeToLive($"{_redisKey}_affinity_{userId}");
|
||||
if (time is null)
|
||||
{
|
||||
time = TimeSpan.FromMinutes(30);
|
||||
db.StringSet($"{_redisKey}_affinity_{userId}", true, time);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryAddDivorceCooldown(ulong userId, out TimeSpan? time)
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
time = db.KeyTimeToLive($"{_redisKey}_divorce_{userId}");
|
||||
if (time is null)
|
||||
{
|
||||
time = TimeSpan.FromHours(6);
|
||||
db.StringSet($"{_redisKey}_divorce_{userId}", true, time);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public Task SetStreamDataAsync(string url, string data)
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
return db.StringSetAsync($"{_redisKey}_stream_{url}", data, TimeSpan.FromHours(6));
|
||||
}
|
||||
|
||||
public bool TryGetStreamData(string url, out string dataStr)
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
dataStr = db.StringGet($"{_redisKey}_stream_{url}");
|
||||
|
||||
return !string.IsNullOrWhiteSpace(dataStr);
|
||||
}
|
||||
|
||||
public TimeSpan? TryAddRatelimit(ulong id, string name, int expireIn)
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
if (db.StringSet($"{_redisKey}_ratelimit_{id}_{name}",
|
||||
0, // i don't use the value
|
||||
TimeSpan.FromSeconds(expireIn),
|
||||
when: When.NotExists))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return db.KeyTimeToLive($"{_redisKey}_ratelimit_{id}_{name}");
|
||||
}
|
||||
|
||||
public bool TryGetEconomy(out string data)
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
data = db.StringGet($"{_redisKey}_economy");
|
||||
if (data is not null)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SetEconomy(string data)
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
db.StringSet($"{_redisKey}_economy", data, TimeSpan.FromMinutes(3));
|
||||
}
|
||||
|
||||
public async Task<TOut> GetOrAddCachedDataAsync<TParam, TOut>(
|
||||
string key,
|
||||
Func<TParam, Task<TOut>> factory,
|
||||
TParam param,
|
||||
TimeSpan expiry)
|
||||
where TOut : class
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
|
||||
var data = await db.StringGetAsync(key);
|
||||
if (!data.HasValue)
|
||||
{
|
||||
var obj = await factory(param);
|
||||
|
||||
if (obj is null)
|
||||
return default;
|
||||
|
||||
await db.StringSetAsync(key, JsonConvert.SerializeObject(obj), expiry);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
return (TOut)JsonConvert.DeserializeObject(data, typeof(TOut));
|
||||
}
|
||||
|
||||
public DateTime GetLastCurrencyDecay()
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
|
||||
var str = (string)db.StringGet($"{_redisKey}_last_currency_decay");
|
||||
if (string.IsNullOrEmpty(str))
|
||||
return DateTime.MinValue;
|
||||
|
||||
return JsonConvert.DeserializeObject<DateTime>(str);
|
||||
}
|
||||
|
||||
public void SetLastCurrencyDecay()
|
||||
{
|
||||
var db = Redis.GetDatabase();
|
||||
|
||||
db.StringSet($"{_redisKey}_last_currency_decay", JsonConvert.SerializeObject(DateTime.UtcNow));
|
||||
}
|
||||
}
|
@@ -1,111 +1,297 @@
|
||||
using NadekoBot.Common.Configs;
|
||||
#nullable disable
|
||||
using NadekoBot.Common.ModuleBehaviors;
|
||||
using NadekoBot.Common.Yml;
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace NadekoBot.Services;
|
||||
|
||||
public sealed class ImagesConfig : ConfigServiceBase<ImageUrls>
|
||||
public sealed class RedisImagesCache : IImageCache, IReadyExecutor
|
||||
{
|
||||
private const string PATH = "data/images.yml";
|
||||
|
||||
private static readonly TypedKey<ImageUrls> _changeKey =
|
||||
new("config.images.updated");
|
||||
|
||||
public override string Name
|
||||
=> "images";
|
||||
|
||||
public ImagesConfig(IConfigSeria serializer, IPubSub pubSub)
|
||||
: base(PATH, serializer, pubSub, _changeKey)
|
||||
public enum ImageKeys
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public interface IImageCache
|
||||
{
|
||||
Task<byte[]?> GetHeadsImageAsync();
|
||||
Task<byte[]?> GetTailsImageAsync();
|
||||
Task<byte[]?> GetCurrencyImageAsync();
|
||||
Task<byte[]?> GetXpBackgroundImageAsync();
|
||||
Task<byte[]?> GetRategirlBgAsync();
|
||||
Task<byte[]?> GetRategirlDotAsync();
|
||||
Task<byte[]?> GetDiceAsync(int num);
|
||||
Task<byte[]?> GetSlotEmojiAsync(int number);
|
||||
Task<byte[]?> GetSlotBgAsync();
|
||||
Task<byte[]?> GetRipBgAsync();
|
||||
Task<byte[]?> GetRipOverlayAsync();
|
||||
}
|
||||
|
||||
public sealed class ImageCache : IImageCache, INService
|
||||
{
|
||||
private readonly IBotCache _cache;
|
||||
private readonly ImagesConfig _ic;
|
||||
private readonly Random _rng;
|
||||
private readonly IHttpClientFactory _httpFactory;
|
||||
|
||||
public ImageCache(
|
||||
IBotCache cache,
|
||||
ImagesConfig ic,
|
||||
IHttpClientFactory httpFactory)
|
||||
{
|
||||
_cache = cache;
|
||||
_ic = ic;
|
||||
_httpFactory = httpFactory;
|
||||
_rng = new NadekoRandom();
|
||||
CoinHeads,
|
||||
CoinTails,
|
||||
Dice,
|
||||
SlotBg,
|
||||
SlotEmojis,
|
||||
Currency,
|
||||
RategirlMatrix,
|
||||
RategirlDot,
|
||||
RipOverlay,
|
||||
RipBg,
|
||||
XpBg
|
||||
}
|
||||
|
||||
private static TypedKey<byte[]> GetImageKey(Uri url)
|
||||
=> new($"image:{url}");
|
||||
private const string BASE_PATH = "data/";
|
||||
private const string CARDS_PATH = $"{BASE_PATH}images/cards";
|
||||
|
||||
private async Task<byte[]?> GetImageDataAsync(Uri url)
|
||||
=> await _cache.GetOrAddAsync(
|
||||
GetImageKey(url),
|
||||
async () =>
|
||||
private IDatabase Db
|
||||
=> _con.GetDatabase();
|
||||
|
||||
public ImageUrls ImageUrls { get; private set; }
|
||||
|
||||
public IReadOnlyList<byte[]> Heads
|
||||
=> GetByteArrayData(ImageKeys.CoinHeads);
|
||||
|
||||
public IReadOnlyList<byte[]> Tails
|
||||
=> GetByteArrayData(ImageKeys.CoinTails);
|
||||
|
||||
public IReadOnlyList<byte[]> Dice
|
||||
=> GetByteArrayData(ImageKeys.Dice);
|
||||
|
||||
public IReadOnlyList<byte[]> SlotEmojis
|
||||
=> GetByteArrayData(ImageKeys.SlotEmojis);
|
||||
|
||||
public IReadOnlyList<byte[]> Currency
|
||||
=> GetByteArrayData(ImageKeys.Currency);
|
||||
|
||||
public byte[] SlotBackground
|
||||
=> GetByteData(ImageKeys.SlotBg);
|
||||
|
||||
public byte[] RategirlMatrix
|
||||
=> GetByteData(ImageKeys.RategirlMatrix);
|
||||
|
||||
public byte[] RategirlDot
|
||||
=> GetByteData(ImageKeys.RategirlDot);
|
||||
|
||||
public byte[] XpBackground
|
||||
=> GetByteData(ImageKeys.XpBg);
|
||||
|
||||
public byte[] Rip
|
||||
=> GetByteData(ImageKeys.RipBg);
|
||||
|
||||
public byte[] RipOverlay
|
||||
=> GetByteData(ImageKeys.RipOverlay);
|
||||
|
||||
private readonly ConnectionMultiplexer _con;
|
||||
private readonly IBotCredentials _creds;
|
||||
private readonly HttpClient _http;
|
||||
private readonly string _imagesPath;
|
||||
|
||||
public RedisImagesCache(ConnectionMultiplexer con, IBotCredentials creds)
|
||||
{
|
||||
_con = con;
|
||||
_creds = creds;
|
||||
_http = new();
|
||||
_imagesPath = Path.Combine(BASE_PATH, "images.yml");
|
||||
|
||||
Migrate();
|
||||
|
||||
ImageUrls = Yaml.Deserializer.Deserialize<ImageUrls>(File.ReadAllText(_imagesPath));
|
||||
}
|
||||
|
||||
public byte[] GetCard(string key)
|
||||
// since cards are always local for now, don't cache them
|
||||
=> File.ReadAllBytes(Path.Join(CARDS_PATH, key + ".jpg"));
|
||||
|
||||
public async Task OnReadyAsync()
|
||||
{
|
||||
if (await AllKeysExist())
|
||||
return;
|
||||
|
||||
await Reload();
|
||||
}
|
||||
|
||||
private void Migrate()
|
||||
{
|
||||
// migrate to yml
|
||||
if (File.Exists(Path.Combine(BASE_PATH, "images.json")))
|
||||
{
|
||||
var oldFilePath = Path.Combine(BASE_PATH, "images.json");
|
||||
var backupFilePath = Path.Combine(BASE_PATH, "images.json.backup");
|
||||
|
||||
var oldData = JsonConvert.DeserializeObject<OldImageUrls>(File.ReadAllText(oldFilePath));
|
||||
|
||||
if (oldData is not null)
|
||||
{
|
||||
using var http = _httpFactory.CreateClient();
|
||||
var bytes = await http.GetByteArrayAsync(url);
|
||||
return bytes;
|
||||
},
|
||||
expiry: 48.Hours());
|
||||
var newData = new ImageUrls
|
||||
{
|
||||
Coins =
|
||||
new()
|
||||
{
|
||||
Heads =
|
||||
oldData.Coins.Heads.Length == 1
|
||||
&& oldData.Coins.Heads[0].ToString()
|
||||
== "https://nadeko-pictures.nyc3.digitaloceanspaces.com/other/coins/heads.png"
|
||||
? new[] { new Uri("https://cdn.nadeko.bot/coins/heads3.png") }
|
||||
: oldData.Coins.Heads,
|
||||
Tails = oldData.Coins.Tails.Length == 1
|
||||
&& oldData.Coins.Tails[0].ToString()
|
||||
== "https://nadeko-pictures.nyc3.digitaloceanspaces.com/other/coins/tails.png"
|
||||
? new[] { new Uri("https://cdn.nadeko.bot/coins/tails3.png") }
|
||||
: oldData.Coins.Tails
|
||||
},
|
||||
Dice = oldData.Dice.Map(x => x.ToNewCdn()),
|
||||
Currency = oldData.Currency.Map(x => x.ToNewCdn()),
|
||||
Rategirl =
|
||||
new()
|
||||
{
|
||||
Dot = oldData.Rategirl.Dot.ToNewCdn(),
|
||||
Matrix = oldData.Rategirl.Matrix.ToNewCdn()
|
||||
},
|
||||
Rip = new()
|
||||
{
|
||||
Bg = oldData.Rip.Bg.ToNewCdn(),
|
||||
Overlay = oldData.Rip.Overlay.ToNewCdn()
|
||||
},
|
||||
Slots = new()
|
||||
{
|
||||
Bg = new("https://cdn.nadeko.bot/slots/slots_bg.png"),
|
||||
Emojis = new[]
|
||||
{
|
||||
"https://cdn.nadeko.bot/slots/0.png", "https://cdn.nadeko.bot/slots/1.png",
|
||||
"https://cdn.nadeko.bot/slots/2.png", "https://cdn.nadeko.bot/slots/3.png",
|
||||
"https://cdn.nadeko.bot/slots/4.png", "https://cdn.nadeko.bot/slots/5.png"
|
||||
}.Map(x => new Uri(x))
|
||||
},
|
||||
Xp = new()
|
||||
{
|
||||
Bg = oldData.Xp.Bg.ToNewCdn()
|
||||
},
|
||||
Version = 2
|
||||
};
|
||||
|
||||
private async Task<byte[]?> GetRandomImageDataAsync(Uri[] urls)
|
||||
{
|
||||
if (urls.Length == 0)
|
||||
return null;
|
||||
File.Move(oldFilePath, backupFilePath, true);
|
||||
File.WriteAllText(_imagesPath, Yaml.Serializer.Serialize(newData));
|
||||
}
|
||||
}
|
||||
|
||||
var url = urls[_rng.Next(0, urls.Length)];
|
||||
// removed numbers from slots
|
||||
var localImageUrls = Yaml.Deserializer.Deserialize<ImageUrls>(File.ReadAllText(_imagesPath));
|
||||
if (localImageUrls.Version == 2)
|
||||
{
|
||||
localImageUrls.Version = 3;
|
||||
File.WriteAllText(_imagesPath, Yaml.Serializer.Serialize(localImageUrls));
|
||||
}
|
||||
|
||||
var data = await GetImageDataAsync(url);
|
||||
return data;
|
||||
if (localImageUrls.Version == 3)
|
||||
{
|
||||
localImageUrls.Version = 4;
|
||||
if (localImageUrls.Xp?.Bg.ToString() == "https://cdn.nadeko.bot/other/xp/bg.png")
|
||||
localImageUrls.Xp.Bg = new("https://cdn.nadeko.bot/other/xp/bg_k.png");
|
||||
|
||||
File.WriteAllText(_imagesPath, Yaml.Serializer.Serialize(localImageUrls));
|
||||
}
|
||||
}
|
||||
|
||||
public Task<byte[]?> GetHeadsImageAsync()
|
||||
=> GetRandomImageDataAsync(_ic.Data.Coins.Heads);
|
||||
public async Task Reload()
|
||||
{
|
||||
ImageUrls = Yaml.Deserializer.Deserialize<ImageUrls>(await File.ReadAllTextAsync(_imagesPath));
|
||||
foreach (var key in GetAllKeys())
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case ImageKeys.CoinHeads:
|
||||
await Load(key, ImageUrls.Coins.Heads);
|
||||
break;
|
||||
case ImageKeys.CoinTails:
|
||||
await Load(key, ImageUrls.Coins.Tails);
|
||||
break;
|
||||
case ImageKeys.Dice:
|
||||
await Load(key, ImageUrls.Dice);
|
||||
break;
|
||||
case ImageKeys.SlotBg:
|
||||
await Load(key, ImageUrls.Slots.Bg);
|
||||
break;
|
||||
case ImageKeys.SlotEmojis:
|
||||
await Load(key, ImageUrls.Slots.Emojis);
|
||||
break;
|
||||
case ImageKeys.Currency:
|
||||
await Load(key, ImageUrls.Currency);
|
||||
break;
|
||||
case ImageKeys.RategirlMatrix:
|
||||
await Load(key, ImageUrls.Rategirl.Matrix);
|
||||
break;
|
||||
case ImageKeys.RategirlDot:
|
||||
await Load(key, ImageUrls.Rategirl.Dot);
|
||||
break;
|
||||
case ImageKeys.RipOverlay:
|
||||
await Load(key, ImageUrls.Rip.Overlay);
|
||||
break;
|
||||
case ImageKeys.RipBg:
|
||||
await Load(key, ImageUrls.Rip.Bg);
|
||||
break;
|
||||
case ImageKeys.XpBg:
|
||||
await Load(key, ImageUrls.Xp.Bg);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Task<byte[]?> GetTailsImageAsync()
|
||||
=> GetRandomImageDataAsync(_ic.Data.Coins.Tails);
|
||||
private async Task Load(ImageKeys key, Uri uri)
|
||||
{
|
||||
var data = await GetImageData(uri);
|
||||
if (data is null)
|
||||
return;
|
||||
|
||||
public Task<byte[]?> GetCurrencyImageAsync()
|
||||
=> GetRandomImageDataAsync(_ic.Data.Currency);
|
||||
await Db.StringSetAsync(GetRedisKey(key), data);
|
||||
}
|
||||
|
||||
public Task<byte[]?> GetXpBackgroundImageAsync()
|
||||
=> GetImageDataAsync(_ic.Data.Xp.Bg);
|
||||
private async Task Load(ImageKeys key, Uri[] uris)
|
||||
{
|
||||
await Db.KeyDeleteAsync(GetRedisKey(key));
|
||||
var imageData = await uris.Select(GetImageData).WhenAll();
|
||||
var vals = imageData.Where(x => x is not null).Select(x => (RedisValue)x).ToArray();
|
||||
|
||||
public Task<byte[]?> GetRategirlBgAsync()
|
||||
=> GetImageDataAsync(_ic.Data.Rategirl.Matrix);
|
||||
await Db.ListRightPushAsync(GetRedisKey(key), vals);
|
||||
|
||||
public Task<byte[]?> GetRategirlDotAsync()
|
||||
=> GetImageDataAsync(_ic.Data.Rategirl.Dot);
|
||||
if (uris.Length != vals.Length)
|
||||
{
|
||||
Log.Information(
|
||||
"{Loaded}/{Max} URIs for the key '{ImageKey}' have been loaded.\n"
|
||||
+ "Some of the supplied URIs are either unavailable or invalid",
|
||||
vals.Length,
|
||||
uris.Length,
|
||||
key);
|
||||
}
|
||||
}
|
||||
|
||||
public Task<byte[]?> GetDiceAsync(int num)
|
||||
=> GetImageDataAsync(_ic.Data.Dice[num]);
|
||||
private async Task<byte[]> GetImageData(Uri uri)
|
||||
{
|
||||
if (uri.IsFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
var bytes = await File.ReadAllBytesAsync(uri.LocalPath);
|
||||
return bytes;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Warning(ex, "Failed reading image bytes from uri: {Uri}", uri.ToString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Task<byte[]?> GetSlotEmojiAsync(int number)
|
||||
=> GetImageDataAsync(_ic.Data.Slots.Emojis[number]);
|
||||
try
|
||||
{
|
||||
return await _http.GetByteArrayAsync(uri);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Warning(ex, "Image url you provided is not a valid image: {Uri}", uri.ToString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Task<byte[]?> GetSlotBgAsync()
|
||||
=> GetImageDataAsync(_ic.Data.Slots.Bg);
|
||||
private async Task<bool> AllKeysExist()
|
||||
{
|
||||
var tasks = await GetAllKeys().Select(x => Db.KeyExistsAsync(GetRedisKey(x))).WhenAll();
|
||||
|
||||
public Task<byte[]?> GetRipBgAsync()
|
||||
=> GetImageDataAsync(_ic.Data.Rip.Bg);
|
||||
return tasks.All(exist => exist);
|
||||
}
|
||||
|
||||
public Task<byte[]?> GetRipOverlayAsync()
|
||||
=> GetImageDataAsync(_ic.Data.Rip.Overlay);
|
||||
}
|
||||
private IEnumerable<ImageKeys> GetAllKeys()
|
||||
=> Enum.GetValues<ImageKeys>();
|
||||
|
||||
private byte[][] GetByteArrayData(ImageKeys key)
|
||||
=> Db.ListRange(GetRedisKey(key)).Map(x => (byte[])x);
|
||||
|
||||
private byte[] GetByteData(ImageKeys key)
|
||||
=> Db.StringGet(GetRedisKey(key));
|
||||
|
||||
private RedisKey GetRedisKey(ImageKeys key)
|
||||
=> _creds.RedisKey() + "_image_" + key;
|
||||
}
|
90
src/NadekoBot/Services/Impl/RedisLocalDataCache.cs
Normal file
90
src/NadekoBot/Services/Impl/RedisLocalDataCache.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
#nullable disable
|
||||
using NadekoBot.Common.Pokemon;
|
||||
using NadekoBot.Modules.Games.Common.Trivia;
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace NadekoBot.Services;
|
||||
|
||||
public class RedisLocalDataCache : ILocalDataCache
|
||||
{
|
||||
private const string POKEMON_ABILITIES_FILE = "data/pokemon/pokemon_abilities.json";
|
||||
private const string POKEMON_LIST_FILE = "data/pokemon/pokemon_list.json";
|
||||
private const string POKEMON_MAP_PATH = "data/pokemon/name-id_map.json";
|
||||
private const string QUESTIONS_FILE = "data/trivia_questions.json";
|
||||
|
||||
public IReadOnlyDictionary<string, SearchPokemon> Pokemons
|
||||
{
|
||||
get => Get<Dictionary<string, SearchPokemon>>("pokemon_list");
|
||||
private init => Set("pokemon_list", value);
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<string, SearchPokemonAbility> PokemonAbilities
|
||||
{
|
||||
get => Get<Dictionary<string, SearchPokemonAbility>>("pokemon_abilities");
|
||||
private init => Set("pokemon_abilities", value);
|
||||
}
|
||||
|
||||
public TriviaQuestion[] TriviaQuestions
|
||||
{
|
||||
get => Get<TriviaQuestion[]>("trivia_questions");
|
||||
private init => Set("trivia_questions", value);
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<int, string> PokemonMap
|
||||
{
|
||||
get => Get<Dictionary<int, string>>("pokemon_map");
|
||||
private init => Set("pokemon_map", value);
|
||||
}
|
||||
|
||||
private readonly ConnectionMultiplexer _con;
|
||||
private readonly IBotCredentials _creds;
|
||||
|
||||
public RedisLocalDataCache(ConnectionMultiplexer con, IBotCredentials creds, DiscordSocketClient client)
|
||||
{
|
||||
_con = con;
|
||||
_creds = creds;
|
||||
var shardId = client.ShardId;
|
||||
|
||||
if (shardId == 0)
|
||||
{
|
||||
if (!File.Exists(POKEMON_LIST_FILE))
|
||||
Log.Warning($"{POKEMON_LIST_FILE} is missing. Pokemon abilities not loaded");
|
||||
else
|
||||
{
|
||||
Pokemons =
|
||||
JsonConvert.DeserializeObject<Dictionary<string, SearchPokemon>>(
|
||||
File.ReadAllText(POKEMON_LIST_FILE));
|
||||
}
|
||||
|
||||
if (!File.Exists(POKEMON_ABILITIES_FILE))
|
||||
Log.Warning($"{POKEMON_ABILITIES_FILE} is missing. Pokemon abilities not loaded.");
|
||||
else
|
||||
{
|
||||
PokemonAbilities =
|
||||
JsonConvert.DeserializeObject<Dictionary<string, SearchPokemonAbility>>(
|
||||
File.ReadAllText(POKEMON_ABILITIES_FILE));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
TriviaQuestions = JsonConvert.DeserializeObject<TriviaQuestion[]>(File.ReadAllText(QUESTIONS_FILE));
|
||||
PokemonMap = JsonConvert.DeserializeObject<PokemonNameId[]>(File.ReadAllText(POKEMON_MAP_PATH))
|
||||
?.ToDictionary(x => x.Id, x => x.Name)
|
||||
?? new();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Error loading local data");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private T Get<T>(string key)
|
||||
where T : class
|
||||
=> JsonConvert.DeserializeObject<T>(_con.GetDatabase().StringGet($"{_creds.RedisKey()}_localdata_{key}"));
|
||||
|
||||
private void Set(string key, object obj)
|
||||
=> _con.GetDatabase().StringSet($"{_creds.RedisKey()}_localdata_{key}", JsonConvert.SerializeObject(obj));
|
||||
}
|
@@ -7,7 +7,7 @@ namespace NadekoBot.Services;
|
||||
|
||||
public sealed class StatsService : IStatsService, IReadyExecutor, INService
|
||||
{
|
||||
public const string BOT_VERSION = "4.2.12";
|
||||
public const string BOT_VERSION = "4.2.3";
|
||||
|
||||
public string Author
|
||||
=> "Kwoth#2452";
|
||||
|
@@ -1,13 +1,13 @@
|
||||
#nullable disable
|
||||
namespace NadekoBot.Services;
|
||||
|
||||
public class MemoryBotStringsProvider : IBotStringsProvider
|
||||
public class LocalBotStringsProvider : IBotStringsProvider
|
||||
{
|
||||
private readonly IStringsSource _source;
|
||||
private IReadOnlyDictionary<string, Dictionary<string, string>> responseStrings;
|
||||
private IReadOnlyDictionary<string, Dictionary<string, CommandStrings>> commandStrings;
|
||||
|
||||
public MemoryBotStringsProvider(IStringsSource source)
|
||||
public LocalBotStringsProvider(IStringsSource source)
|
||||
{
|
||||
_source = source;
|
||||
Reload();
|
@@ -32,7 +32,7 @@ public static class ArrayExtensions
|
||||
public static TOut[] Map<TIn, TOut>(this TIn[] arr, Func<TIn, TOut> f)
|
||||
=> Array.ConvertAll(arr, x => f(x));
|
||||
|
||||
public static TOut[] Map<TIn, TOut>(this IReadOnlyCollection<TIn> col, Func<TIn, TOut> f)
|
||||
public static IReadOnlyCollection<TOut> Map<TIn, TOut>(this IReadOnlyCollection<TIn> col, Func<TIn, TOut> f)
|
||||
{
|
||||
var toReturn = new TOut[col.Count];
|
||||
|
||||
|
@@ -37,9 +37,8 @@ public static class Extensions
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(text))
|
||||
};
|
||||
|
||||
public static ulong[] GetGuildIds(this DiscordSocketClient client)
|
||||
=> client.Guilds
|
||||
.Map(x => x.Id);
|
||||
public static List<ulong> GetGuildIds(this DiscordSocketClient client)
|
||||
=> client.Guilds.Select(x => x.Id).ToList();
|
||||
|
||||
/// <summary>
|
||||
/// Generates a string in the format HHH:mm if timespan is >= 2m.
|
||||
|
@@ -9,10 +9,10 @@ namespace NadekoBot.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddBotStringsServices(this IServiceCollection services, BotCacheImplemenation botCache)
|
||||
=> botCache == BotCacheImplemenation.Memory
|
||||
public static IServiceCollection AddBotStringsServices(this IServiceCollection services, int totalShards)
|
||||
=> totalShards <= 1
|
||||
? services.AddSingleton<IStringsSource, LocalFileStringsSource>()
|
||||
.AddSingleton<IBotStringsProvider, MemoryBotStringsProvider>()
|
||||
.AddSingleton<IBotStringsProvider, LocalBotStringsProvider>()
|
||||
.AddSingleton<IBotStrings, BotStrings>()
|
||||
: services.AddSingleton<IStringsSource, LocalFileStringsSource>()
|
||||
.AddSingleton<IBotStringsProvider, RedisBotStringsProvider>()
|
||||
@@ -23,6 +23,17 @@ public static class ServiceCollectionExtensions
|
||||
services.Scan(x => x.FromCallingAssembly()
|
||||
.AddClasses(f => f.AssignableTo(typeof(ConfigServiceBase<>)))
|
||||
.AsSelfWithInterfaces());
|
||||
|
||||
// var baseType = typeof(ConfigServiceBase<>);
|
||||
//
|
||||
// foreach (var type in Assembly.GetCallingAssembly().ExportedTypes.Where(x => x.IsSealed))
|
||||
// {
|
||||
// if (type.BaseType?.IsGenericType == true && type.BaseType.GetGenericTypeDefinition() == baseType)
|
||||
// {
|
||||
// services.AddSingleton(type);
|
||||
// services.AddSingleton(x => (IConfigService)x.GetRequiredService(type));
|
||||
// }
|
||||
// }
|
||||
|
||||
return services;
|
||||
}
|
||||
@@ -37,7 +48,7 @@ public static class ServiceCollectionExtensions
|
||||
.AddSingleton<ISoundcloudResolver, SoundcloudResolver>()
|
||||
.AddSingleton<ILocalTrackResolver, LocalTrackResolver>()
|
||||
.AddSingleton<IRadioResolver, RadioResolver>()
|
||||
.AddSingleton<ITrackCacher, TrackCacher>()
|
||||
.AddSingleton<ITrackCacher, RedisTrackCacher>()
|
||||
.AddSingleton<YtLoader>()
|
||||
.AddSingleton<IPlaceholderProvider>(svc => svc.GetRequiredService<IMusicService>());
|
||||
|
||||
@@ -54,23 +65,10 @@ public static class ServiceCollectionExtensions
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddCache(this IServiceCollection services, IBotCredentials creds)
|
||||
public static IServiceCollection AddRedis(this IServiceCollection services, string redisOptions)
|
||||
{
|
||||
if (creds.BotCache == BotCacheImplemenation.Redis)
|
||||
{
|
||||
var conf = ConfigurationOptions.Parse(creds.RedisOptions);
|
||||
services.AddSingleton(ConnectionMultiplexer.Connect(conf))
|
||||
.AddSingleton<IBotCache, RedisBotCache>()
|
||||
.AddSingleton<IPubSub, RedisPubSub>();
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddSingleton<IBotCache, MemoryBotCache>()
|
||||
.AddSingleton<IPubSub, EventPubSub>();
|
||||
|
||||
}
|
||||
|
||||
return services
|
||||
.AddBotStringsServices(creds.BotCache);
|
||||
var conf = ConfigurationOptions.Parse(redisOptions);
|
||||
services.AddSingleton(ConnectionMultiplexer.Connect(conf));
|
||||
return services;
|
||||
}
|
||||
}
|
@@ -30,9 +30,9 @@ public static class UserExtensions
|
||||
=> usr.AvatarId is null ? new(usr.GetDefaultAvatarUrl()) : new Uri(usr.GetAvatarUrl(ImageFormat.Auto, size));
|
||||
|
||||
// This method is only used for the xp card
|
||||
public static Uri RealAvatarUrl(this DiscordUser usr)
|
||||
public static Uri? RealAvatarUrl(this DiscordUser usr)
|
||||
=> usr.AvatarId is null
|
||||
? new(CDN.GetDefaultUserAvatarUrl(ushort.Parse(usr.Discriminator)))
|
||||
? null
|
||||
: new Uri(usr.AvatarId.StartsWith("a_", StringComparison.InvariantCulture)
|
||||
? $"{DiscordConfig.CDNUrl}avatars/{usr.UserId}/{usr.AvatarId}.gif"
|
||||
: $"{DiscordConfig.CDNUrl}avatars/{usr.UserId}/{usr.AvatarId}.png");
|
||||
|
@@ -1,5 +1,5 @@
|
||||
# DO NOT CHANGE
|
||||
version: 6
|
||||
version: 5
|
||||
# Bot token. Do not share with anyone ever -> https://discordapp.com/developers/applications/
|
||||
token: ''
|
||||
# List of Ids of the users who have bot owner permissions
|
||||
@@ -9,9 +9,6 @@ ownerIds: []
|
||||
usePrivilegedIntents: true
|
||||
# The number of shards that the bot will be running on.
|
||||
# Leave at 1 if you don't know what you're doing.
|
||||
#
|
||||
# note: If you are planning to have more than one shard, then you must change botCache to 'redis'.
|
||||
# Also, in that case you should be using NadekoBot.Coordinator to start the bot, and it will correctly override this value.
|
||||
totalShards: 1
|
||||
# Login to https://console.cloud.google.com, create a new project, go to APIs & Services -> Library -> YouTube Data API and enable it.
|
||||
# Then, go to APIs and Services -> Credentials and click Create credentials -> API key.
|
||||
@@ -56,12 +53,7 @@ patreon:
|
||||
botListToken: ''
|
||||
# Official cleverbot api key.
|
||||
cleverbotApiKey: ''
|
||||
# Which cache implementation should bot use.
|
||||
# 'memory' - Cache will be in memory of the bot's process itself. Only use this on bots with a single shard. When the bot is restarted the cache is reset.
|
||||
# 'redis' - Uses redis (which needs to be separately downloaded and installed). The cache will persist through bot restarts. You can configure connection string in creds.yml
|
||||
botCache: Memory
|
||||
# Redis connection string. Don't change if you don't know what you're doing.
|
||||
# Only used if botCache is set to 'redis'
|
||||
redisOptions: localhost:6379,syncTimeout=30000,responseTimeout=30000,allowAdmin=true,password=
|
||||
# Database options. Don't change if you don't know what you're doing. Leave null for default values
|
||||
db:
|
||||
@@ -107,7 +99,7 @@ twitchClientSecret:
|
||||
# args: "NadekoBot.dll -- {0}"
|
||||
# Windows default
|
||||
# cmd: NadekoBot.exe
|
||||
# args: "{0}"
|
||||
# args: {0}
|
||||
restartCommand:
|
||||
cmd:
|
||||
args:
|
||||
|
@@ -862,6 +862,8 @@ unsetmusicchannel:
|
||||
musicquality:
|
||||
- musicquality
|
||||
- mquality
|
||||
imagesreload:
|
||||
- imagesreload
|
||||
stringsreload:
|
||||
- stringsreload
|
||||
shardstats:
|
||||
@@ -1305,6 +1307,4 @@ patron:
|
||||
- patron
|
||||
patronmessage:
|
||||
- patronmessage
|
||||
- patronmsg
|
||||
eval:
|
||||
- eval
|
||||
- patronmsg
|
@@ -1,7 +1,7 @@
|
||||
# DO NOT CHANGE
|
||||
version: 2
|
||||
version: 1
|
||||
# Whether the patronage feature is enabled
|
||||
isEnabled: false
|
||||
isEnabled: true
|
||||
# List of patron only features and relevant quota data
|
||||
quotas:
|
||||
# Dictionary of feature names with their respective limits. Set to null for unlimited
|
||||
@@ -24,7 +24,7 @@ quotas:
|
||||
# Dictionary of commands with their respective quota data
|
||||
commands:
|
||||
cleverbot:
|
||||
V:
|
||||
V: null
|
||||
prune:
|
||||
X:
|
||||
PerHour: 1
|
||||
|
@@ -1496,6 +1496,10 @@ musicquality:
|
||||
- ""
|
||||
- "High"
|
||||
- "Low"
|
||||
imagesreload:
|
||||
desc: "Reloads images bot is using. Safe to use even when bot is being used heavily."
|
||||
args:
|
||||
- ""
|
||||
stringsreload:
|
||||
desc: "Reloads localized bot strings."
|
||||
args:
|
||||
@@ -2215,17 +2219,3 @@ patronmessage:
|
||||
desc: "Sends a message to all patrons of the specified tier and higher. Supports embeds."
|
||||
args:
|
||||
- "x hello"
|
||||
eval:
|
||||
desc: |-
|
||||
Execute arbitrary C# code and (optionally) return a result. Several namespaces are included by default.
|
||||
Special variables available:
|
||||
`self` - Instance of the command group executing the command (this)
|
||||
`guild` - Server in which the command is executed
|
||||
`channel` - Channel in which the command is executed
|
||||
`user` - User executing the command
|
||||
`ctx` - Discord.Net command context
|
||||
`services` - Nadeko's IServiceProvider
|
||||
args:
|
||||
- "123 / 4.5f"
|
||||
- "await ctx.OkAsync();"
|
||||
- 'await ctx.SendConfirmAsync("uwu");'
|
Reference in New Issue
Block a user