mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 01:38:27 -04:00
Restructured the project structure back to the way it was, there's no reasonable way to split the modules
This commit is contained in:
19
src/NadekoBot/_common/Abstractions/AsyncLazy.cs
Normal file
19
src/NadekoBot/_common/Abstractions/AsyncLazy.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public class AsyncLazy<T> : Lazy<Task<T>>
|
||||
{
|
||||
public AsyncLazy(Func<T> valueFactory)
|
||||
: base(() => Task.Run(valueFactory))
|
||||
{
|
||||
}
|
||||
|
||||
public AsyncLazy(Func<Task<T>> taskFactory)
|
||||
: base(() => Task.Run(taskFactory))
|
||||
{
|
||||
}
|
||||
|
||||
public TaskAwaiter<T> GetAwaiter()
|
||||
=> Value.GetAwaiter();
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
using OneOf;
|
||||
using OneOf.Types;
|
||||
|
||||
namespace Nadeko.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;
|
||||
}
|
||||
}
|
47
src/NadekoBot/_common/Abstractions/Cache/IBotCache.cs
Normal file
47
src/NadekoBot/_common/Abstractions/Cache/IBotCache.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using OneOf;
|
||||
using OneOf.Types;
|
||||
|
||||
namespace Nadeko.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);
|
||||
}
|
71
src/NadekoBot/_common/Abstractions/Cache/MemoryBotCache.cs
Normal file
71
src/NadekoBot/_common/Abstractions/Cache/MemoryBotCache.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using OneOf;
|
||||
using OneOf.Types;
|
||||
|
||||
// ReSharper disable InconsistentlySynchronizedField
|
||||
|
||||
namespace Nadeko.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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,88 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace System.Collections.Generic;
|
||||
|
||||
[DebuggerDisplay("{_backingStore.Count}")]
|
||||
public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T> where T : notnull
|
||||
{
|
||||
private readonly ConcurrentDictionary<T, bool> _backingStore;
|
||||
|
||||
public ConcurrentHashSet()
|
||||
=> _backingStore = new();
|
||||
|
||||
public ConcurrentHashSet(IEnumerable<T> values, IEqualityComparer<T>? comparer = null)
|
||||
=> _backingStore = new(values.Select(x => new KeyValuePair<T, bool>(x, true)), comparer);
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
=> _backingStore.Keys.GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
=> GetEnumerator();
|
||||
|
||||
/// <summary>
|
||||
/// Adds the specified item to the <see cref="ConcurrentHashSet{T}" />.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to add.</param>
|
||||
/// <returns>
|
||||
/// true if the items was added to the <see cref="ConcurrentHashSet{T}" />
|
||||
/// successfully; false if it already exists.
|
||||
/// </returns>
|
||||
/// <exception cref="T:System.OverflowException">
|
||||
/// The <see cref="ConcurrentHashSet{T}" />
|
||||
/// contains too many items.
|
||||
/// </exception>
|
||||
public bool Add(T item)
|
||||
=> _backingStore.TryAdd(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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (arrayIndex >= len)
|
||||
throw new IndexOutOfRangeException(nameof(arrayIndex));
|
||||
|
||||
array[arrayIndex++] = k;
|
||||
}
|
||||
}
|
||||
|
||||
bool ICollection<T>.Remove(T item)
|
||||
=> TryRemove(item);
|
||||
|
||||
public bool TryRemove(T item)
|
||||
=> _backingStore.TryRemove(item, out _);
|
||||
|
||||
public void RemoveWhere(Func<T, bool> predicate)
|
||||
{
|
||||
foreach (var elem in this.Where(predicate))
|
||||
TryRemove(elem);
|
||||
}
|
||||
|
||||
public int Count
|
||||
=> _backingStore.Count;
|
||||
|
||||
public bool IsReadOnly
|
||||
=> false;
|
||||
}
|
@@ -0,0 +1,148 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public interface IIndexed
|
||||
{
|
||||
int Index { get; set; }
|
||||
}
|
||||
|
||||
public class IndexedCollection<T> : IList<T>
|
||||
where T : class, IIndexed
|
||||
{
|
||||
public List<T> Source { get; }
|
||||
|
||||
public int Count
|
||||
=> Source.Count;
|
||||
|
||||
public bool IsReadOnly
|
||||
=> false;
|
||||
|
||||
public virtual T this[int index]
|
||||
{
|
||||
get => Source[index];
|
||||
set
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
value.Index = index;
|
||||
Source[index] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private readonly object _locker = new();
|
||||
|
||||
public IndexedCollection()
|
||||
=> Source = new();
|
||||
|
||||
public IndexedCollection(IEnumerable<T> source)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
Source = source.OrderBy(x => x.Index).ToList();
|
||||
UpdateIndexes();
|
||||
}
|
||||
}
|
||||
|
||||
public int IndexOf(T item)
|
||||
=> item?.Index ?? -1;
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
=> Source.GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
=> Source.GetEnumerator();
|
||||
|
||||
public void Add(T item)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(item);
|
||||
|
||||
lock (_locker)
|
||||
{
|
||||
item.Index = Source.Count;
|
||||
Source.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Clear()
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
Source.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public bool Contains(T item)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
return Source.Contains(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
Source.CopyTo(array, arrayIndex);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool Remove(T item)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
if (Source.Remove(item))
|
||||
{
|
||||
for (var i = 0; i < Source.Count; i++)
|
||||
{
|
||||
if (Source[i].Index != i)
|
||||
Source[i].Index = i;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public virtual void Insert(int index, T item)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
Source.Insert(index, item);
|
||||
for (var i = index; i < Source.Count; i++)
|
||||
Source[i].Index = i;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void RemoveAt(int index)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
Source.RemoveAt(index);
|
||||
for (var i = index; i < Source.Count; i++)
|
||||
Source[i].Index = i;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateIndexes()
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
for (var i = 0; i < Source.Count; i++)
|
||||
{
|
||||
if (Source[i].Index != i)
|
||||
Source[i].Index = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static implicit operator List<T>(IndexedCollection<T> x)
|
||||
=> x.Source;
|
||||
|
||||
public List<T> ToList()
|
||||
=> Source.ToList();
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Nadeko.Common;
|
||||
|
||||
// made for expressions because they almost never get added
|
||||
// and they get looped through constantly
|
||||
public static class ArrayExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a new array from the old array + new element at the end
|
||||
/// </summary>
|
||||
/// <param name="input">Input array</param>
|
||||
/// <param name="added">Item to add to the end of the output array</param>
|
||||
/// <typeparam name="T">Type of the array</typeparam>
|
||||
/// <returns>A new array with the new element at the end</returns>
|
||||
public static T[] With<T>(this T[] input, T added)
|
||||
{
|
||||
var newExprs = new T[input.Length + 1];
|
||||
Array.Copy(input, 0, newExprs, 0, input.Length);
|
||||
newExprs[input.Length] = added;
|
||||
return newExprs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new array by applying the specified function to every element in the input array
|
||||
/// </summary>
|
||||
/// <param name="arr">Array to modify</param>
|
||||
/// <param name="f">Function to apply</param>
|
||||
/// <typeparam name="TIn">Orignal type of the elements in the array</typeparam>
|
||||
/// <typeparam name="TOut">Output type of the elements of the array</typeparam>
|
||||
/// <returns>New array with updated elements</returns>
|
||||
public static TOut[] Map<TIn, TOut>(this TIn[] arr, Func<TIn, TOut> f)
|
||||
=> Array.ConvertAll(arr, x => f(x));
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new array by applying the specified function to every element in the input array
|
||||
/// </summary>
|
||||
/// <param name="col">Array to modify</param>
|
||||
/// <param name="f">Function to apply</param>
|
||||
/// <typeparam name="TIn">Orignal type of the elements in the array</typeparam>
|
||||
/// <typeparam name="TOut">Output type of the elements of the array</typeparam>
|
||||
/// <returns>New array with updated elements</returns>
|
||||
public static TOut[] Map<TIn, TOut>(this IReadOnlyCollection<TIn> col, Func<TIn, TOut> f)
|
||||
{
|
||||
var toReturn = new TOut[col.Count];
|
||||
|
||||
var i = 0;
|
||||
foreach (var item in col)
|
||||
toReturn[i++] = f(item);
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
public static T? RandomOrDefault<T>(this T[] data)
|
||||
{
|
||||
if (data.Length == 0)
|
||||
return default;
|
||||
|
||||
var index = RandomNumberGenerator.GetInt32(0, data.Length);
|
||||
return data[index];
|
||||
}
|
||||
}
|
@@ -0,0 +1,97 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public static class EnumerableExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Concatenates the members of a collection, using the specified separator between each member.
|
||||
/// </summary>
|
||||
/// <param name="data">Collection to join</param>
|
||||
/// <param name="separator">
|
||||
/// The character to use as a separator. separator is included in the returned string only if
|
||||
/// values has more than one element.
|
||||
/// </param>
|
||||
/// <param name="func">Optional transformation to apply to each element before concatenation.</param>
|
||||
/// <typeparam name="T">The type of the members of values.</typeparam>
|
||||
/// <returns>
|
||||
/// A string that consists of the members of values delimited by the separator character. -or- Empty if values has
|
||||
/// no elements.
|
||||
/// </returns>
|
||||
public static string Join<T>(this IEnumerable<T> data, char separator, Func<T, string>? func = null)
|
||||
=> string.Join(separator, data.Select(func ?? (x => x?.ToString() ?? string.Empty)));
|
||||
|
||||
/// <summary>
|
||||
/// Concatenates the members of a collection, using the specified separator between each member.
|
||||
/// </summary>
|
||||
/// <param name="data">Collection to join</param>
|
||||
/// <param name="separator">
|
||||
/// The string to use as a separator.separator is included in the returned string only if values
|
||||
/// has more than one element.
|
||||
/// </param>
|
||||
/// <param name="func">Optional transformation to apply to each element before concatenation.</param>
|
||||
/// <typeparam name="T">The type of the members of values.</typeparam>
|
||||
/// <returns>
|
||||
/// A string that consists of the members of values delimited by the separator character. -or- Empty if values has
|
||||
/// no elements.
|
||||
/// </returns>
|
||||
public static string Join<T>(this IEnumerable<T> data, string separator, Func<T, string>? func = null)
|
||||
=> string.Join(separator, data.Select(func ?? (x => x?.ToString() ?? string.Empty)));
|
||||
|
||||
/// <summary>
|
||||
/// Randomize element order by performing the Fisher-Yates shuffle
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Item type</typeparam>
|
||||
/// <param name="items">Items to shuffle</param>
|
||||
public static IReadOnlyList<T> Shuffle<T>(this IEnumerable<T> items)
|
||||
{
|
||||
var list = items.ToArray();
|
||||
var n = list.Length;
|
||||
while (n-- > 1)
|
||||
{
|
||||
var k = RandomNumberGenerator.GetInt32(n);
|
||||
(list[k], list[n]) = (list[n], list[k]);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConcurrentDictionary{TKey,TValue}" /> class
|
||||
/// that contains elements copied from the specified <see cref="IEnumerable{T}" />
|
||||
/// has the default concurrency level, has the default initial capacity,
|
||||
/// and uses the default comparer for the key type.
|
||||
/// </summary>
|
||||
/// <param name="dict">
|
||||
/// The <see cref="IEnumerable{T}" /> whose elements are copied to the new
|
||||
/// <see cref="ConcurrentDictionary{TKey,TValue}" />.
|
||||
/// </param>
|
||||
/// <returns>A new instance of the <see cref="ConcurrentDictionary{TKey,TValue}" /> class</returns>
|
||||
public static ConcurrentDictionary<TKey, TValue> ToConcurrent<TKey, TValue>(
|
||||
this IEnumerable<KeyValuePair<TKey, TValue>> dict)
|
||||
where TKey : notnull
|
||||
=> new(dict);
|
||||
|
||||
public static IndexedCollection<T> ToIndexed<T>(this IEnumerable<T> enumerable)
|
||||
where T : class, IIndexed
|
||||
=> new(enumerable);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a task that will complete when all of the <see cref="Task{TResult}" /> objects in an enumerable
|
||||
/// collection have completed
|
||||
/// </summary>
|
||||
/// <param name="tasks">The tasks to wait on for completion.</param>
|
||||
/// <typeparam name="TResult">The type of the completed task.</typeparam>
|
||||
/// <returns>A task that represents the completion of all of the supplied tasks.</returns>
|
||||
public static Task<TResult[]> WhenAll<TResult>(this IEnumerable<Task<TResult>> tasks)
|
||||
=> Task.WhenAll(tasks);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a task that will complete when all of the <see cref="Task" /> objects in an enumerable
|
||||
/// collection have completed
|
||||
/// </summary>
|
||||
/// <param name="tasks">The tasks to wait on for completion.</param>
|
||||
/// <returns>A task that represents the completion of all of the supplied tasks.</returns>
|
||||
public static Task WhenAll(this IEnumerable<Task> tasks)
|
||||
=> Task.WhenAll(tasks);
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
public static long ToTimestamp(this in DateTime value)
|
||||
=> (value.Ticks - 621355968000000000) / 10000000;
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public static class HttpClientExtensions
|
||||
{
|
||||
public static HttpClient AddFakeHeaders(this HttpClient http)
|
||||
{
|
||||
AddFakeHeaders(http.DefaultRequestHeaders);
|
||||
return http;
|
||||
}
|
||||
|
||||
public static void AddFakeHeaders(this HttpHeaders dict)
|
||||
{
|
||||
dict.Clear();
|
||||
dict.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
|
||||
dict.Add("User-Agent",
|
||||
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1");
|
||||
}
|
||||
|
||||
public static bool IsImage(this HttpResponseMessage msg)
|
||||
=> IsImage(msg, out _);
|
||||
|
||||
public static bool IsImage(this HttpResponseMessage msg, out string? mimeType)
|
||||
{
|
||||
mimeType = msg.Content.Headers.ContentType?.MediaType;
|
||||
if (mimeType is "image/png" or "image/jpeg" or "image/gif")
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static long GetContentLength(this HttpResponseMessage msg)
|
||||
=> msg.Content.Headers.ContentLength ?? long.MaxValue;
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
using OneOf.Types;
|
||||
using OneOf;
|
||||
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public static class OneOfExtensions
|
||||
{
|
||||
public static bool TryGetValue<T>(this OneOf<T, None> oneOf, out T value)
|
||||
=> oneOf.TryPickT0(out value, out _);
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public delegate TOut PipeFunc<TIn, out TOut>(in TIn a);
|
||||
public delegate TOut PipeFunc<TIn1, TIn2, out TOut>(in TIn1 a, in TIn2 b);
|
||||
|
||||
public static class PipeExtensions
|
||||
{
|
||||
public static TOut Pipe<TIn, TOut>(this TIn a, Func<TIn, TOut> fn)
|
||||
=> fn(a);
|
||||
|
||||
public static TOut Pipe<TIn, TOut>(this TIn a, PipeFunc<TIn, TOut> fn)
|
||||
=> fn(a);
|
||||
|
||||
public static TOut Pipe<TIn1, TIn2, TOut>(this (TIn1, TIn2) a, PipeFunc<TIn1, TIn2, TOut> fn)
|
||||
=> fn(a.Item1, a.Item2);
|
||||
|
||||
public static (TIn, TExtra) With<TIn, TExtra>(this TIn a, TExtra b)
|
||||
=> (a, b);
|
||||
|
||||
public static async Task<TOut> Pipe<TIn, TOut>(this Task<TIn> a, Func<TIn, TOut> fn)
|
||||
=> fn(await a);
|
||||
}
|
@@ -0,0 +1,139 @@
|
||||
using NadekoBot.Common.Yml;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Humanizer;
|
||||
using Nadeko.Common;
|
||||
|
||||
namespace NadekoBot.Extensions;
|
||||
|
||||
public static class StringExtensions
|
||||
{
|
||||
private static readonly HashSet<char> _lettersAndDigits = new(Enumerable.Range(48, 10)
|
||||
.Concat(Enumerable.Range(65, 26))
|
||||
.Concat(Enumerable.Range(97, 26))
|
||||
.Select(x => (char)x));
|
||||
|
||||
private static readonly Regex _filterRegex = new(@"discord(?:\.gg|\.io|\.me|\.li|(?:app)?\.com\/invite)\/(\w+)",
|
||||
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
private static readonly Regex _codePointRegex =
|
||||
new(@"(\\U(?<code>[a-zA-Z0-9]{8})|\\u(?<code>[a-zA-Z0-9]{4})|\\x(?<code>[a-zA-Z0-9]{2}))",
|
||||
RegexOptions.Compiled);
|
||||
|
||||
public static string PadBoth(this string str, int length)
|
||||
{
|
||||
var spaces = length - str.Length;
|
||||
var padLeft = (spaces / 2) + str.Length;
|
||||
return str.PadLeft(padLeft, ' ').PadRight(length, ' ');
|
||||
}
|
||||
|
||||
public static string StripHtml(this string input)
|
||||
=> Regex.Replace(input, "<.*?>", string.Empty);
|
||||
|
||||
public static string? TrimTo(this string? str, int maxLength, bool hideDots = false)
|
||||
=> hideDots ? str?.Truncate(maxLength, string.Empty) : str?.Truncate(maxLength);
|
||||
|
||||
public static string ToTitleCase(this string str)
|
||||
{
|
||||
var tokens = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
|
||||
for (var i = 0; i < tokens.Length; i++)
|
||||
{
|
||||
var token = tokens[i];
|
||||
tokens[i] = token[..1].ToUpperInvariant() + token[1..];
|
||||
}
|
||||
|
||||
return tokens.Join(" ").Replace(" Of ", " of ").Replace(" The ", " the ");
|
||||
}
|
||||
|
||||
//http://www.dotnetperls.com/levenshtein
|
||||
public static int LevenshteinDistance(this string s, string t)
|
||||
{
|
||||
var n = s.Length;
|
||||
var m = t.Length;
|
||||
var d = new int[n + 1, m + 1];
|
||||
|
||||
// Step 1
|
||||
if (n == 0)
|
||||
return m;
|
||||
|
||||
if (m == 0)
|
||||
return n;
|
||||
|
||||
// Step 2
|
||||
for (var i = 0; i <= n; d[i, 0] = i++)
|
||||
{
|
||||
}
|
||||
|
||||
for (var j = 0; j <= m; d[0, j] = j++)
|
||||
{
|
||||
}
|
||||
|
||||
// Step 3
|
||||
for (var i = 1; i <= n; i++)
|
||||
//Step 4
|
||||
for (var j = 1; j <= m; j++)
|
||||
{
|
||||
// Step 5
|
||||
var cost = t[j - 1] == s[i - 1] ? 0 : 1;
|
||||
|
||||
// Step 6
|
||||
d[i, j] = Math.Min(Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost);
|
||||
}
|
||||
|
||||
// Step 7
|
||||
return d[n, m];
|
||||
}
|
||||
|
||||
public static async Task<Stream> ToStream(this string str)
|
||||
{
|
||||
var ms = new MemoryStream();
|
||||
var sw = new StreamWriter(ms);
|
||||
await sw.WriteAsync(str);
|
||||
await sw.FlushAsync();
|
||||
ms.Position = 0;
|
||||
return ms;
|
||||
}
|
||||
|
||||
public static bool IsDiscordInvite(this string str)
|
||||
=> _filterRegex.IsMatch(str);
|
||||
|
||||
public static string Unmention(this string str)
|
||||
=> str.Replace("@", "ම", StringComparison.InvariantCulture);
|
||||
|
||||
public static string SanitizeMentions(this string str, bool sanitizeRoleMentions = false)
|
||||
{
|
||||
str = str.Replace("@everyone", "@everyοne", StringComparison.InvariantCultureIgnoreCase)
|
||||
.Replace("@here", "@һere", StringComparison.InvariantCultureIgnoreCase);
|
||||
if (sanitizeRoleMentions)
|
||||
str = str.SanitizeRoleMentions();
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public static string SanitizeRoleMentions(this string str)
|
||||
=> str.Replace("<@&", "<ම&", StringComparison.InvariantCultureIgnoreCase);
|
||||
|
||||
public static string SanitizeAllMentions(this string str)
|
||||
=> str.SanitizeMentions().SanitizeRoleMentions();
|
||||
|
||||
public static string ToBase64(this string plainText)
|
||||
{
|
||||
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
|
||||
return Convert.ToBase64String(plainTextBytes);
|
||||
}
|
||||
|
||||
public static string GetInitials(this string txt, string glue = "")
|
||||
=> txt.Split(' ').Select(x => x.FirstOrDefault()).Join(glue);
|
||||
|
||||
public static bool IsAlphaNumeric(this string txt)
|
||||
=> txt.All(c => _lettersAndDigits.Contains(c));
|
||||
|
||||
public static string UnescapeUnicodeCodePoints(this string input)
|
||||
=> _codePointRegex.Replace(input,
|
||||
me =>
|
||||
{
|
||||
var str = me.Groups["code"].Value;
|
||||
var newString = YamlHelper.UnescapeUnicodeCodePoint(str);
|
||||
return newString;
|
||||
});
|
||||
}
|
36
src/NadekoBot/_common/Abstractions/Helpers/LogSetup.cs
Normal file
36
src/NadekoBot/_common/Abstractions/Helpers/LogSetup.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Serilog.Events;
|
||||
using Serilog.Sinks.SystemConsole.Themes;
|
||||
using System.Text;
|
||||
using Serilog;
|
||||
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public static class LogSetup
|
||||
{
|
||||
public static void SetupLogger(object source)
|
||||
{
|
||||
Log.Logger = new LoggerConfiguration().MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
||||
.MinimumLevel.Override("System", LogEventLevel.Information)
|
||||
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Console(LogEventLevel.Information,
|
||||
theme: GetTheme(),
|
||||
outputTemplate:
|
||||
"[{Timestamp:HH:mm:ss} {Level:u3}] | #{LogSource} | {Message:lj}{NewLine}{Exception}")
|
||||
.Enrich.WithProperty("LogSource", source)
|
||||
.CreateLogger();
|
||||
|
||||
Console.OutputEncoding = Encoding.UTF8;
|
||||
}
|
||||
|
||||
private static ConsoleTheme GetTheme()
|
||||
{
|
||||
if (Environment.OSVersion.Platform == PlatformID.Unix)
|
||||
return AnsiConsoleTheme.Code;
|
||||
#if DEBUG
|
||||
return AnsiConsoleTheme.Code;
|
||||
#else
|
||||
return ConsoleTheme.None;
|
||||
#endif
|
||||
}
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public static class StandardConversions
|
||||
{
|
||||
public static double CelsiusToFahrenheit(double cel)
|
||||
=> (cel * 1.8f) + 32;
|
||||
}
|
100
src/NadekoBot/_common/Abstractions/Kwum.cs
Normal file
100
src/NadekoBot/_common/Abstractions/Kwum.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Nadeko.Common;
|
||||
|
||||
// needs proper invalid input check (character array input out of range)
|
||||
// needs negative number support
|
||||
// ReSharper disable once InconsistentNaming
|
||||
#pragma warning disable CS8981
|
||||
public readonly struct kwum : IEquatable<kwum>
|
||||
#pragma warning restore CS8981
|
||||
{
|
||||
private const string VALID_CHARACTERS = "23456789abcdefghijkmnpqrstuvwxyz";
|
||||
private readonly int _value;
|
||||
|
||||
public kwum(int num)
|
||||
=> _value = num;
|
||||
|
||||
public kwum(in char c)
|
||||
{
|
||||
if (!IsValidChar(c))
|
||||
throw new ArgumentException("Character needs to be a valid kwum character.", nameof(c));
|
||||
|
||||
_value = InternalCharToValue(c);
|
||||
}
|
||||
|
||||
public kwum(in ReadOnlySpan<char> input)
|
||||
{
|
||||
_value = 0;
|
||||
for (var index = 0; index < input.Length; index++)
|
||||
{
|
||||
var c = input[index];
|
||||
if (!IsValidChar(c))
|
||||
throw new ArgumentException("All characters need to be a valid kwum characters.", nameof(input));
|
||||
|
||||
_value += VALID_CHARACTERS.IndexOf(c) * (int)Math.Pow(VALID_CHARACTERS.Length, input.Length - index - 1);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int InternalCharToValue(in char c)
|
||||
=> VALID_CHARACTERS.IndexOf(c);
|
||||
|
||||
public static bool TryParse(in ReadOnlySpan<char> input, out kwum value)
|
||||
{
|
||||
value = default;
|
||||
foreach (var c in input)
|
||||
{
|
||||
if (!IsValidChar(c))
|
||||
return false;
|
||||
}
|
||||
|
||||
value = new(input);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static kwum operator +(kwum left, kwum right)
|
||||
=> new(left._value + right._value);
|
||||
|
||||
public static bool operator ==(kwum left, kwum right)
|
||||
=> left._value == right._value;
|
||||
|
||||
public static bool operator !=(kwum left, kwum right)
|
||||
=> !(left == right);
|
||||
|
||||
public static implicit operator long(kwum kwum)
|
||||
=> kwum._value;
|
||||
|
||||
public static implicit operator int(kwum kwum)
|
||||
=> kwum._value;
|
||||
|
||||
public static implicit operator kwum(int num)
|
||||
=> new(num);
|
||||
|
||||
public static bool IsValidChar(char c)
|
||||
=> VALID_CHARACTERS.Contains(c);
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var count = VALID_CHARACTERS.Length;
|
||||
var localValue = _value;
|
||||
var arrSize = (int)Math.Log(localValue, count) + 1;
|
||||
Span<char> chars = new char[arrSize];
|
||||
while (localValue > 0)
|
||||
{
|
||||
localValue = Math.DivRem(localValue, count, out var rem);
|
||||
chars[--arrSize] = VALID_CHARACTERS[rem];
|
||||
}
|
||||
|
||||
return new(chars);
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
=> obj is kwum kw && kw == this;
|
||||
|
||||
public bool Equals(kwum other)
|
||||
=> other == this;
|
||||
|
||||
public override int GetHashCode()
|
||||
=> _value.GetHashCode();
|
||||
}
|
67
src/NadekoBot/_common/Abstractions/NadekoRandom.cs
Normal file
67
src/NadekoBot/_common/Abstractions/NadekoRandom.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
#nullable disable
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public sealed class NadekoRandom : Random
|
||||
{
|
||||
private readonly RandomNumberGenerator _rng;
|
||||
|
||||
public NadekoRandom()
|
||||
=> _rng = RandomNumberGenerator.Create();
|
||||
|
||||
public override int Next()
|
||||
{
|
||||
var bytes = new byte[sizeof(int)];
|
||||
_rng.GetBytes(bytes);
|
||||
return Math.Abs(BitConverter.ToInt32(bytes, 0));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random integer between 0 (inclusive) and
|
||||
/// a specified exclusive upper bound using a cryptographically strong random number generator.
|
||||
/// </summary>
|
||||
/// <param name="maxValue">Exclusive max value</param>
|
||||
/// <returns>A random number</returns>
|
||||
public override int Next(int maxValue)
|
||||
=> RandomNumberGenerator.GetInt32(maxValue);
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random integer between a specified inclusive lower bound and a
|
||||
/// specified exclusive upper bound using a cryptographically strong random number generator.
|
||||
/// </summary>
|
||||
/// <param name="minValue">Inclusive min value</param>
|
||||
/// <param name="maxValue">Exclusive max value</param>
|
||||
/// <returns>A random number</returns>
|
||||
public override int Next(int minValue, int maxValue)
|
||||
=> RandomNumberGenerator.GetInt32(minValue, maxValue);
|
||||
|
||||
public long NextLong(long minValue, long maxValue)
|
||||
{
|
||||
if (minValue > maxValue)
|
||||
throw new ArgumentOutOfRangeException(nameof(maxValue));
|
||||
if (minValue == maxValue)
|
||||
return minValue;
|
||||
var bytes = new byte[sizeof(long)];
|
||||
_rng.GetBytes(bytes);
|
||||
var sign = Math.Sign(BitConverter.ToInt64(bytes, 0));
|
||||
return (sign * BitConverter.ToInt64(bytes, 0) % (maxValue - minValue)) + minValue;
|
||||
}
|
||||
|
||||
public override void NextBytes(byte[] buffer)
|
||||
=> _rng.GetBytes(buffer);
|
||||
|
||||
protected override double Sample()
|
||||
{
|
||||
var bytes = new byte[sizeof(double)];
|
||||
_rng.GetBytes(bytes);
|
||||
return Math.Abs((BitConverter.ToDouble(bytes, 0) / double.MaxValue) + 1);
|
||||
}
|
||||
|
||||
public override double NextDouble()
|
||||
{
|
||||
var bytes = new byte[sizeof(double)];
|
||||
_rng.GetBytes(bytes);
|
||||
return BitConverter.ToDouble(bytes, 0);
|
||||
}
|
||||
}
|
80
src/NadekoBot/_common/Abstractions/PubSub/EventPubSub.cs
Normal file
80
src/NadekoBot/_common/Abstractions/PubSub/EventPubSub.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public class EventPubSub : IPubSub
|
||||
{
|
||||
private readonly Dictionary<string, Dictionary<Delegate, List<Func<object, ValueTask>>>> _actions = new();
|
||||
private readonly object _locker = new();
|
||||
|
||||
public Task Sub<TData>(in TypedKey<TData> key, Func<TData, ValueTask> action)
|
||||
where TData : notnull
|
||||
{
|
||||
Func<object, ValueTask> localAction = obj => action((TData)obj);
|
||||
lock (_locker)
|
||||
{
|
||||
if (!_actions.TryGetValue(key.Key, out var keyActions))
|
||||
{
|
||||
keyActions = new();
|
||||
_actions[key.Key] = keyActions;
|
||||
}
|
||||
|
||||
if (!keyActions.TryGetValue(action, out var sameActions))
|
||||
{
|
||||
sameActions = new();
|
||||
keyActions[action] = sameActions;
|
||||
}
|
||||
|
||||
sameActions.Add(localAction);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public Task Pub<TData>(in TypedKey<TData> key, TData data)
|
||||
where TData : notnull
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
if (_actions.TryGetValue(key.Key, out var actions))
|
||||
// if this class ever gets used, this needs to be properly implemented
|
||||
// 1. ignore all valuetasks which are completed
|
||||
// 2. run all other tasks in parallel
|
||||
return actions.SelectMany(kvp => kvp.Value).Select(action => action(data).AsTask()).WhenAll();
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public Task Unsub<TData>(in TypedKey<TData> key, Func<TData, ValueTask> action)
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
// get subscriptions for this action
|
||||
if (_actions.TryGetValue(key.Key, out var actions))
|
||||
// get subscriptions which have the same action hash code
|
||||
// note: having this as a list allows for multiple subscriptions of
|
||||
// the same insance's/static method
|
||||
{
|
||||
if (actions.TryGetValue(action, out var sameActions))
|
||||
{
|
||||
// remove last subscription
|
||||
sameActions.RemoveAt(sameActions.Count - 1);
|
||||
|
||||
// if the last subscription was the only subscription
|
||||
// we can safely remove this action's dictionary entry
|
||||
if (sameActions.Count == 0)
|
||||
{
|
||||
actions.Remove(action);
|
||||
|
||||
// if our dictionary has no more elements after
|
||||
// removing the entry
|
||||
// it's safe to remove it from the key's subscriptions
|
||||
if (actions.Count == 0)
|
||||
_actions.Remove(key.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
10
src/NadekoBot/_common/Abstractions/PubSub/IPubSub.cs
Normal file
10
src/NadekoBot/_common/Abstractions/PubSub/IPubSub.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public interface IPubSub
|
||||
{
|
||||
public Task Pub<TData>(in TypedKey<TData> key, TData data)
|
||||
where TData : notnull;
|
||||
|
||||
public Task Sub<TData>(in TypedKey<TData> key, Func<TData, ValueTask> action)
|
||||
where TData : notnull;
|
||||
}
|
7
src/NadekoBot/_common/Abstractions/PubSub/ISeria.cs
Normal file
7
src/NadekoBot/_common/Abstractions/PubSub/ISeria.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public interface ISeria
|
||||
{
|
||||
byte[] Serialize<T>(T data);
|
||||
T? Deserialize<T>(byte[]? data);
|
||||
}
|
63
src/NadekoBot/_common/Abstractions/QueueRunner.cs
Normal file
63
src/NadekoBot/_common/Abstractions/QueueRunner.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System.Threading.Channels;
|
||||
using Serilog;
|
||||
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public sealed class QueueRunner
|
||||
{
|
||||
private readonly Channel<Func<Task>> _channel;
|
||||
private readonly int _delayMs;
|
||||
|
||||
public QueueRunner(int delayMs = 0, int maxCapacity = -1)
|
||||
{
|
||||
if (delayMs < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(delayMs));
|
||||
|
||||
_delayMs = delayMs;
|
||||
_channel = maxCapacity switch
|
||||
{
|
||||
0 or < -1 => throw new ArgumentOutOfRangeException(nameof(maxCapacity)),
|
||||
-1 => Channel.CreateUnbounded<Func<Task>>(new UnboundedChannelOptions()
|
||||
{
|
||||
SingleReader = true,
|
||||
SingleWriter = false,
|
||||
AllowSynchronousContinuations = true,
|
||||
}),
|
||||
_ => Channel.CreateBounded<Func<Task>>(new BoundedChannelOptions(maxCapacity)
|
||||
{
|
||||
Capacity = maxCapacity,
|
||||
FullMode = BoundedChannelFullMode.DropOldest,
|
||||
SingleReader = true,
|
||||
SingleWriter = false,
|
||||
AllowSynchronousContinuations = true
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
public async Task RunAsync(CancellationToken cancel = default)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var func = await _channel.Reader.ReadAsync(cancel);
|
||||
|
||||
try
|
||||
{
|
||||
await func();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Warning(ex, "Exception executing a staggered func: {ErrorMessage}", ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (_delayMs != 0)
|
||||
{
|
||||
await Task.Delay(_delayMs, cancel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ValueTask EnqueueAsync(Func<Task> action)
|
||||
=> _channel.Writer.WriteAsync(action);
|
||||
}
|
30
src/NadekoBot/_common/Abstractions/TypedKey.cs
Normal file
30
src/NadekoBot/_common/Abstractions/TypedKey.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public readonly struct TypedKey<TData>
|
||||
{
|
||||
public string Key { get; }
|
||||
|
||||
public TypedKey(in string key)
|
||||
=> Key = key;
|
||||
|
||||
public static implicit operator TypedKey<TData>(in string input)
|
||||
=> new(input);
|
||||
|
||||
public static implicit operator string(in TypedKey<TData> input)
|
||||
=> input.Key;
|
||||
|
||||
public static bool operator ==(in TypedKey<TData> left, in TypedKey<TData> right)
|
||||
=> left.Key == right.Key;
|
||||
|
||||
public static bool operator !=(in TypedKey<TData> left, in TypedKey<TData> right)
|
||||
=> !(left == right);
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
=> obj is TypedKey<TData> o && o == this;
|
||||
|
||||
public override int GetHashCode()
|
||||
=> Key?.GetHashCode() ?? 0;
|
||||
|
||||
public override string ToString()
|
||||
=> Key;
|
||||
}
|
48
src/NadekoBot/_common/Abstractions/YamlHelper.cs
Normal file
48
src/NadekoBot/_common/Abstractions/YamlHelper.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
#nullable disable
|
||||
namespace NadekoBot.Common.Yml;
|
||||
|
||||
public static class YamlHelper
|
||||
{
|
||||
// https://github.com/aaubry/YamlDotNet/blob/0f4cc205e8b2dd8ef6589d96de32bf608a687c6f/YamlDotNet/Core/Scanner.cs#L1687
|
||||
/// <summary>
|
||||
/// This is modified code from yamldotnet's repo which handles parsing unicode code points
|
||||
/// it is needed as yamldotnet doesn't support unescaped unicode characters
|
||||
/// </summary>
|
||||
/// <param name="point">Unicode code point</param>
|
||||
/// <returns>Actual character</returns>
|
||||
public static string UnescapeUnicodeCodePoint(this string point)
|
||||
{
|
||||
var character = 0;
|
||||
|
||||
// Scan the character value.
|
||||
|
||||
foreach (var c in point)
|
||||
{
|
||||
if (!IsHex(c))
|
||||
return point;
|
||||
|
||||
character = (character << 4) + AsHex(c);
|
||||
}
|
||||
|
||||
// Check the value and write the character.
|
||||
|
||||
if (character is (>= 0xD800 and <= 0xDFFF) or > 0x10FFFF)
|
||||
return point;
|
||||
|
||||
return char.ConvertFromUtf32(character);
|
||||
}
|
||||
|
||||
public static bool IsHex(char c)
|
||||
=> c is (>= '0' and <= '9') or (>= 'A' and <= 'F') or (>= 'a' and <= 'f');
|
||||
|
||||
public static int AsHex(char c)
|
||||
{
|
||||
if (c <= '9')
|
||||
return c - '0';
|
||||
|
||||
if (c <= 'F')
|
||||
return c - 'A' + 10;
|
||||
|
||||
return c - 'a' + 10;
|
||||
}
|
||||
}
|
78
src/NadekoBot/_common/Abstractions/creds/IBotCredentials.cs
Normal file
78
src/NadekoBot/_common/Abstractions/creds/IBotCredentials.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
#nullable disable
|
||||
namespace NadekoBot;
|
||||
|
||||
public interface IBotCredentials
|
||||
{
|
||||
string Token { get; }
|
||||
string GoogleApiKey { get; }
|
||||
ICollection<ulong> OwnerIds { get; set; }
|
||||
bool UsePrivilegedIntents { get; }
|
||||
string RapidApiKey { get; }
|
||||
|
||||
Creds.DbOptions Db { get; }
|
||||
string OsuApiKey { get; }
|
||||
int TotalShards { get; }
|
||||
Creds.PatreonSettings Patreon { get; }
|
||||
string CleverbotApiKey { get; }
|
||||
string Gpt3ApiKey { get; }
|
||||
RestartConfig RestartCommand { get; }
|
||||
Creds.VotesSettings Votes { get; }
|
||||
string BotListToken { get; }
|
||||
string RedisOptions { get; }
|
||||
string LocationIqApiKey { get; }
|
||||
string TimezoneDbApiKey { get; }
|
||||
string CoinmarketcapApiKey { get; }
|
||||
string TrovoClientId { get; }
|
||||
string CoordinatorUrl { get; set; }
|
||||
string TwitchClientId { get; set; }
|
||||
string TwitchClientSecret { get; set; }
|
||||
GoogleApiConfig Google { get; set; }
|
||||
BotCacheImplemenation BotCache { get; set; }
|
||||
}
|
||||
|
||||
public interface IVotesSettings
|
||||
{
|
||||
string TopggServiceUrl { get; set; }
|
||||
string TopggKey { get; set; }
|
||||
string DiscordsServiceUrl { get; set; }
|
||||
string DiscordsKey { get; set; }
|
||||
}
|
||||
|
||||
public interface IPatreonSettings
|
||||
{
|
||||
public string ClientId { get; set; }
|
||||
public string AccessToken { get; set; }
|
||||
public string RefreshToken { get; set; }
|
||||
public string ClientSecret { get; set; }
|
||||
public string CampaignId { get; set; }
|
||||
}
|
||||
|
||||
public interface IRestartConfig
|
||||
{
|
||||
string Cmd { get; set; }
|
||||
string Args { get; set; }
|
||||
}
|
||||
|
||||
public class RestartConfig : IRestartConfig
|
||||
{
|
||||
public string Cmd { get; set; }
|
||||
public string Args { get; set; }
|
||||
}
|
||||
|
||||
public enum BotCacheImplemenation
|
||||
{
|
||||
Memory,
|
||||
Redis
|
||||
}
|
||||
|
||||
public interface IDbOptions
|
||||
{
|
||||
string Type { get; set; }
|
||||
string ConnectionString { get; set; }
|
||||
}
|
||||
|
||||
public interface IGoogleApiConfig
|
||||
{
|
||||
string SearchId { get; init; }
|
||||
string ImageSearchId { get; init; }
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
namespace NadekoBot;
|
||||
|
||||
public interface IBotCredsProvider
|
||||
{
|
||||
public void Reload();
|
||||
public IBotCredentials GetCreds();
|
||||
public void ModifyCredsFile(Action<IBotCredentials> func);
|
||||
}
|
13
src/NadekoBot/_common/Abstractions/strings/CommandStrings.cs
Normal file
13
src/NadekoBot/_common/Abstractions/strings/CommandStrings.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
#nullable disable
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace NadekoBot.Services;
|
||||
|
||||
public sealed class CommandStrings
|
||||
{
|
||||
[YamlMember(Alias = "desc")]
|
||||
public string Desc { get; set; }
|
||||
|
||||
[YamlMember(Alias = "args")]
|
||||
public string[] Args { get; set; }
|
||||
}
|
16
src/NadekoBot/_common/Abstractions/strings/IBotStrings.cs
Normal file
16
src/NadekoBot/_common/Abstractions/strings/IBotStrings.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
#nullable disable
|
||||
using System.Globalization;
|
||||
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Defines methods to retrieve and reload bot strings
|
||||
/// </summary>
|
||||
public interface IBotStrings
|
||||
{
|
||||
string GetText(string key, ulong? guildId = null, params object[] data);
|
||||
string GetText(string key, CultureInfo locale, params object[] data);
|
||||
void Reload();
|
||||
CommandStrings GetCommandStrings(string commandName, ulong? guildId = null);
|
||||
CommandStrings GetCommandStrings(string commandName, CultureInfo cultureInfo);
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
#nullable disable
|
||||
using System.Globalization;
|
||||
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public static class BotStringsExtensions
|
||||
{
|
||||
// this one is for pipe fun, see PipeExtensions.cs
|
||||
public static string GetText(this IBotStrings strings, in LocStr str, in ulong guildId)
|
||||
=> strings.GetText(str.Key, guildId, str.Params);
|
||||
|
||||
public static string GetText(this IBotStrings strings, in LocStr str, ulong? guildId = null)
|
||||
=> strings.GetText(str.Key, guildId, str.Params);
|
||||
|
||||
public static string GetText(this IBotStrings strings, in LocStr str, CultureInfo culture)
|
||||
=> strings.GetText(str.Key, culture, str.Params);
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
#nullable disable
|
||||
namespace NadekoBot.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Implemented by classes which provide localized strings in their own ways
|
||||
/// </summary>
|
||||
public interface IBotStringsProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets localized string
|
||||
/// </summary>
|
||||
/// <param name="localeName">Language name</param>
|
||||
/// <param name="key">String key</param>
|
||||
/// <returns>Localized string</returns>
|
||||
string GetText(string localeName, string key);
|
||||
|
||||
/// <summary>
|
||||
/// Reloads string cache
|
||||
/// </summary>
|
||||
void Reload();
|
||||
|
||||
/// <summary>
|
||||
/// Gets command arg examples and description
|
||||
/// </summary>
|
||||
/// <param name="localeName">Language name</param>
|
||||
/// <param name="commandName">Command name</param>
|
||||
CommandStrings GetCommandStrings(string localeName, string commandName);
|
||||
}
|
17
src/NadekoBot/_common/Abstractions/strings/IStringsSource.cs
Normal file
17
src/NadekoBot/_common/Abstractions/strings/IStringsSource.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
#nullable disable
|
||||
|
||||
namespace NadekoBot.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Basic interface used for classes implementing strings loading mechanism
|
||||
/// </summary>
|
||||
public interface IStringsSource
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets all response strings
|
||||
/// </summary>
|
||||
/// <returns>Dictionary(localename, Dictionary(key, response))</returns>
|
||||
Dictionary<string, Dictionary<string, string>> GetResponseStrings();
|
||||
|
||||
Dictionary<string, Dictionary<string, CommandStrings>> GetCommandStrings();
|
||||
}
|
13
src/NadekoBot/_common/Abstractions/strings/LocStr.cs
Normal file
13
src/NadekoBot/_common/Abstractions/strings/LocStr.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace NadekoBot;
|
||||
|
||||
public readonly struct LocStr
|
||||
{
|
||||
public readonly string Key;
|
||||
public readonly object[] Params;
|
||||
|
||||
public LocStr(string key, params object[] data)
|
||||
{
|
||||
Key = key;
|
||||
Params = data;
|
||||
}
|
||||
}
|
12
src/NadekoBot/_common/Attributes/AliasesAttribute.cs
Normal file
12
src/NadekoBot/_common/Attributes/AliasesAttribute.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace NadekoBot.Common.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class AliasesAttribute : AliasAttribute
|
||||
{
|
||||
public AliasesAttribute([CallerMemberName] string memberName = "")
|
||||
: base(CommandNameLoadHelper.GetAliasesFor(memberName))
|
||||
{
|
||||
}
|
||||
}
|
18
src/NadekoBot/_common/Attributes/CmdAttribute.cs
Normal file
18
src/NadekoBot/_common/Attributes/CmdAttribute.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace NadekoBot.Common.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class CmdAttribute : CommandAttribute
|
||||
{
|
||||
public string MethodName { get; }
|
||||
|
||||
public CmdAttribute([CallerMemberName] string memberName = "")
|
||||
: base(CommandNameLoadHelper.GetCommandNameFor(memberName))
|
||||
{
|
||||
MethodName = memberName.ToLowerInvariant();
|
||||
Aliases = CommandNameLoadHelper.GetAliasesFor(memberName);
|
||||
Remarks = memberName.ToLowerInvariant();
|
||||
Summary = memberName.ToLowerInvariant();
|
||||
}
|
||||
}
|
11
src/NadekoBot/_common/Attributes/DIIgnoreAttribute.cs
Normal file
11
src/NadekoBot/_common/Attributes/DIIgnoreAttribute.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
#nullable disable
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Classed marked with this attribute will not be added to the service provider
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class DIIgnoreAttribute : Attribute
|
||||
{
|
||||
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
namespace NadekoBot.Common.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class NadekoOptionsAttribute<TOption> : Attribute
|
||||
where TOption: INadekoCommandOptions
|
||||
{
|
||||
}
|
21
src/NadekoBot/_common/Attributes/NoPublicBotAttribute.cs
Normal file
21
src/NadekoBot/_common/Attributes/NoPublicBotAttribute.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
#nullable disable
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
|
||||
[SuppressMessage("Style", "IDE0022:Use expression body for methods")]
|
||||
public sealed class NoPublicBotAttribute : PreconditionAttribute
|
||||
{
|
||||
public override Task<PreconditionResult> CheckPermissionsAsync(
|
||||
ICommandContext context,
|
||||
CommandInfo command,
|
||||
IServiceProvider services)
|
||||
{
|
||||
#if GLOBAL_NADEKO
|
||||
return Task.FromResult(PreconditionResult.FromError("Not available on the public bot. To learn how to selfhost a private bot, click [here](https://nadekobot.readthedocs.io/en/latest/)."));
|
||||
#else
|
||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||
#endif
|
||||
}
|
||||
}
|
21
src/NadekoBot/_common/Attributes/OnlyPublicBotAttribute.cs
Normal file
21
src/NadekoBot/_common/Attributes/OnlyPublicBotAttribute.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
#nullable disable
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
|
||||
[SuppressMessage("Style", "IDE0022:Use expression body for methods")]
|
||||
public sealed class OnlyPublicBotAttribute : PreconditionAttribute
|
||||
{
|
||||
public override Task<PreconditionResult> CheckPermissionsAsync(
|
||||
ICommandContext context,
|
||||
CommandInfo command,
|
||||
IServiceProvider services)
|
||||
{
|
||||
#if GLOBAL_NADEKO || DEBUG
|
||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||
#else
|
||||
return Task.FromResult(PreconditionResult.FromError("Only available on the public bot."));
|
||||
#endif
|
||||
}
|
||||
}
|
19
src/NadekoBot/_common/Attributes/OwnerOnlyAttribute.cs
Normal file
19
src/NadekoBot/_common/Attributes/OwnerOnlyAttribute.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace NadekoBot.Common.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
|
||||
public sealed class OwnerOnlyAttribute : PreconditionAttribute
|
||||
{
|
||||
public override Task<PreconditionResult> CheckPermissionsAsync(
|
||||
ICommandContext context,
|
||||
CommandInfo command,
|
||||
IServiceProvider services)
|
||||
{
|
||||
var creds = services.GetRequiredService<IBotCredsProvider>().GetCreds();
|
||||
|
||||
return Task.FromResult(creds.IsOwner(context.User) || context.Client.CurrentUser.Id == context.User.Id
|
||||
? PreconditionResult.FromSuccess()
|
||||
: PreconditionResult.FromError("Not owner"));
|
||||
}
|
||||
}
|
38
src/NadekoBot/_common/Attributes/RatelimitAttribute.cs
Normal file
38
src/NadekoBot/_common/Attributes/RatelimitAttribute.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace NadekoBot.Common.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class RatelimitAttribute : PreconditionAttribute
|
||||
{
|
||||
public int Seconds { get; }
|
||||
|
||||
public RatelimitAttribute(int seconds)
|
||||
{
|
||||
if (seconds <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(seconds));
|
||||
|
||||
Seconds = seconds;
|
||||
}
|
||||
|
||||
public override async Task<PreconditionResult> CheckPermissionsAsync(
|
||||
ICommandContext context,
|
||||
CommandInfo command,
|
||||
IServiceProvider services)
|
||||
{
|
||||
if (Seconds == 0)
|
||||
return PreconditionResult.FromSuccess();
|
||||
|
||||
var cache = services.GetRequiredService<IBotCache>();
|
||||
var rem = await cache.GetRatelimitAsync(
|
||||
new($"precondition:{context.User.Id}:{command.Name}"),
|
||||
Seconds.Seconds());
|
||||
|
||||
if (rem is null)
|
||||
return PreconditionResult.FromSuccess();
|
||||
|
||||
var msgContent = $"You can use this command again in {rem.Value.TotalSeconds:F1}s.";
|
||||
|
||||
return PreconditionResult.FromError(msgContent);
|
||||
}
|
||||
}
|
29
src/NadekoBot/_common/Attributes/UserPermAttribute.cs
Normal file
29
src/NadekoBot/_common/Attributes/UserPermAttribute.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Discord;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
|
||||
public class UserPermAttribute : RequireUserPermissionAttribute
|
||||
{
|
||||
public UserPermAttribute(GuildPerm permission)
|
||||
: base(permission)
|
||||
{
|
||||
}
|
||||
|
||||
public UserPermAttribute(ChannelPerm permission)
|
||||
: base(permission)
|
||||
{
|
||||
}
|
||||
|
||||
public override Task<PreconditionResult> CheckPermissionsAsync(
|
||||
ICommandContext context,
|
||||
CommandInfo command,
|
||||
IServiceProvider services)
|
||||
{
|
||||
var permService = services.GetRequiredService<IDiscordPermOverrideService>();
|
||||
if (permService.TryGetOverrides(context.Guild?.Id ?? 0, command.Name.ToUpperInvariant(), out _))
|
||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||
|
||||
return base.CheckPermissionsAsync(context, command, services);
|
||||
}
|
||||
}
|
30
src/NadekoBot/_common/BotCommandTypeReader.cs
Normal file
30
src/NadekoBot/_common/BotCommandTypeReader.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
#nullable disable
|
||||
namespace NadekoBot.Common.TypeReaders;
|
||||
|
||||
public sealed class CommandTypeReader : NadekoTypeReader<CommandInfo>
|
||||
{
|
||||
private readonly CommandService _cmds;
|
||||
private readonly ICommandHandler _handler;
|
||||
|
||||
public CommandTypeReader(ICommandHandler handler, CommandService cmds)
|
||||
{
|
||||
_handler = handler;
|
||||
_cmds = cmds;
|
||||
}
|
||||
|
||||
public override ValueTask<TypeReaderResult<CommandInfo>> ReadAsync(ICommandContext ctx, string input)
|
||||
{
|
||||
input = input.ToUpperInvariant();
|
||||
var prefix = _handler.GetPrefix(ctx.Guild);
|
||||
if (!input.StartsWith(prefix.ToUpperInvariant(), StringComparison.InvariantCulture))
|
||||
return new(TypeReaderResult.FromError<CommandInfo>(CommandError.ParseFailed, "No such command found."));
|
||||
|
||||
input = input[prefix.Length..];
|
||||
|
||||
var cmd = _cmds.Commands.FirstOrDefault(c => c.Aliases.Select(a => a.ToUpperInvariant()).Contains(input));
|
||||
if (cmd is null)
|
||||
return new(TypeReaderResult.FromError<CommandInfo>(CommandError.ParseFailed, "No such command found."));
|
||||
|
||||
return new(TypeReaderResult.FromSuccess(cmd));
|
||||
}
|
||||
}
|
25
src/NadekoBot/_common/CleanupModuleBase.cs
Normal file
25
src/NadekoBot/_common/CleanupModuleBase.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
#nullable disable
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public abstract class CleanupModuleBase : NadekoModule
|
||||
{
|
||||
protected async Task ConfirmActionInternalAsync(string name, Func<Task> action)
|
||||
{
|
||||
try
|
||||
{
|
||||
var embed = _eb.Create()
|
||||
.WithTitle(GetText(strs.sql_confirm_exec))
|
||||
.WithDescription(name);
|
||||
|
||||
if (!await PromptUserConfirmAsync(embed))
|
||||
return;
|
||||
|
||||
await action();
|
||||
await ctx.OkAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await SendErrorAsync(ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
10
src/NadekoBot/_common/CleverBotResponseStr.cs
Normal file
10
src/NadekoBot/_common/CleverBotResponseStr.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
#nullable disable
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace NadekoBot.Modules.Permissions;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, Size = 1)]
|
||||
public readonly struct CleverBotResponseStr
|
||||
{
|
||||
public const string CLEVERBOT_RESPONSE = "cleverbot:response";
|
||||
}
|
31
src/NadekoBot/_common/CommandNameLoadHelper.cs
Normal file
31
src/NadekoBot/_common/CommandNameLoadHelper.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace NadekoBot.Common.Attributes;
|
||||
|
||||
public static class CommandNameLoadHelper
|
||||
{
|
||||
private static readonly IDeserializer _deserializer = new Deserializer();
|
||||
|
||||
private static readonly Lazy<Dictionary<string, string[]>> _lazyCommandAliases
|
||||
= new(() => LoadAliases());
|
||||
|
||||
public static Dictionary<string, string[]> LoadAliases(string aliasesFilePath = "data/aliases.yml")
|
||||
{
|
||||
var text = File.ReadAllText(aliasesFilePath);
|
||||
return _deserializer.Deserialize<Dictionary<string, string[]>>(text);
|
||||
}
|
||||
|
||||
public static string[] GetAliasesFor(string methodName)
|
||||
=> _lazyCommandAliases.Value.TryGetValue(methodName.ToLowerInvariant(), out var aliases) && aliases.Length > 1
|
||||
? aliases.Skip(1).ToArray()
|
||||
: Array.Empty<string>();
|
||||
|
||||
public static string GetCommandNameFor(string methodName)
|
||||
{
|
||||
methodName = methodName.ToLowerInvariant();
|
||||
var toReturn = _lazyCommandAliases.Value.TryGetValue(methodName, out var aliases) && aliases.Length > 0
|
||||
? aliases[0]
|
||||
: methodName;
|
||||
return toReturn;
|
||||
}
|
||||
}
|
203
src/NadekoBot/_common/Configs/BotConfig.cs
Normal file
203
src/NadekoBot/_common/Configs/BotConfig.cs
Normal file
@@ -0,0 +1,203 @@
|
||||
#nullable disable
|
||||
|
||||
using Cloneable;
|
||||
using NadekoBot.Common.Yml;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using System.Globalization;
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace NadekoBot.Common.Configs;
|
||||
|
||||
[Cloneable]
|
||||
public sealed partial class BotConfig : ICloneable<BotConfig>
|
||||
{
|
||||
[Comment("""DO NOT CHANGE""")]
|
||||
public int Version { get; set; } = 5;
|
||||
|
||||
[Comment("""
|
||||
Most commands, when executed, have a small colored line
|
||||
next to the response. The color depends whether the command
|
||||
is completed, errored or in progress (pending)
|
||||
Color settings below are for the color of those lines.
|
||||
To get color's hex, you can go here https://htmlcolorcodes.com/
|
||||
and copy the hex code fo your selected color (marked as #)
|
||||
""")]
|
||||
public ColorConfig Color { get; set; }
|
||||
|
||||
[Comment("Default bot language. It has to be in the list of supported languages (.langli)")]
|
||||
public CultureInfo DefaultLocale { get; set; }
|
||||
|
||||
[Comment("""
|
||||
Style in which executed commands will show up in the console.
|
||||
Allowed values: Simple, Normal, None
|
||||
""")]
|
||||
public ConsoleOutputType ConsoleOutputType { get; set; }
|
||||
|
||||
[Comment("""Whether the bot will check for new releases every hour""")]
|
||||
public bool CheckForUpdates { get; set; } = true;
|
||||
|
||||
[Comment("""Do you want any messages sent by users in Bot's DM to be forwarded to the owner(s)?""")]
|
||||
public bool ForwardMessages { get; set; }
|
||||
|
||||
[Comment("""
|
||||
Do you want the message to be forwarded only to the first owner specified in the list of owners (in creds.yml),
|
||||
or all owners? (this might cause the bot to lag if there's a lot of owners specified)
|
||||
""")]
|
||||
public bool ForwardToAllOwners { get; set; }
|
||||
|
||||
[Comment("""
|
||||
Any messages sent by users in Bot's DM to be forwarded to the specified channel.
|
||||
This option will only work when ForwardToAllOwners is set to false
|
||||
""")]
|
||||
public ulong? ForwardToChannel { get; set; }
|
||||
|
||||
[Comment("""
|
||||
When a user DMs the bot with a message which is not a command
|
||||
they will receive this message. Leave empty for no response. The string which will be sent whenever someone DMs the bot.
|
||||
Supports embeds. How it looks: https://puu.sh/B0BLV.png
|
||||
""")]
|
||||
[YamlMember(ScalarStyle = ScalarStyle.Literal)]
|
||||
public string DmHelpText { get; set; }
|
||||
|
||||
[Comment("""
|
||||
Only users who send a DM to the bot containing one of the specified words will get a DmHelpText response.
|
||||
Case insensitive.
|
||||
Leave empty to reply with DmHelpText to every DM.
|
||||
""")]
|
||||
public List<string> DmHelpTextKeywords { get; set; }
|
||||
|
||||
[Comment("""This is the response for the .h command""")]
|
||||
[YamlMember(ScalarStyle = ScalarStyle.Literal)]
|
||||
public string HelpText { get; set; }
|
||||
|
||||
[Comment("""List of modules and commands completely blocked on the bot""")]
|
||||
public BlockedConfig Blocked { get; set; }
|
||||
|
||||
[Comment("""Which string will be used to recognize the commands""")]
|
||||
public string Prefix { get; set; }
|
||||
|
||||
[Comment("""
|
||||
Toggles whether your bot will group greet/bye messages into a single message every 5 seconds.
|
||||
1st user who joins will get greeted immediately
|
||||
If more users join within the next 5 seconds, they will be greeted in groups of 5.
|
||||
This will cause %user.mention% and other placeholders to be replaced with multiple users.
|
||||
Keep in mind this might break some of your embeds - for example if you have %user.avatar% in the thumbnail,
|
||||
it will become invalid, as it will resolve to a list of avatars of grouped users.
|
||||
note: This setting is primarily used if you're afraid of raids, or you're running medium/large bots where some
|
||||
servers might get hundreds of people join at once. This is used to prevent the bot from getting ratelimited,
|
||||
and (slightly) reduce the greet spam in those servers.
|
||||
""")]
|
||||
public bool GroupGreets { get; set; }
|
||||
|
||||
[Comment("""
|
||||
Whether the bot will rotate through all specified statuses.
|
||||
This setting can be changed via .ropl command.
|
||||
See RotatingStatuses submodule in Administration.
|
||||
""")]
|
||||
public bool RotateStatuses { get; set; }
|
||||
|
||||
public BotConfig()
|
||||
{
|
||||
var color = new ColorConfig();
|
||||
Color = color;
|
||||
DefaultLocale = new("en-US");
|
||||
ConsoleOutputType = ConsoleOutputType.Normal;
|
||||
ForwardMessages = false;
|
||||
ForwardToAllOwners = false;
|
||||
DmHelpText = """{"description": "Type `%prefix%h` for help."}""";
|
||||
HelpText = """
|
||||
{
|
||||
"title": "To invite me to your server, use this link",
|
||||
"description": "https://discordapp.com/oauth2/authorize?client_id={0}&scope=bot&permissions=66186303",
|
||||
"color": 53380,
|
||||
"thumbnail": "https://i.imgur.com/nKYyqMK.png",
|
||||
"fields": [
|
||||
{
|
||||
"name": "Useful help commands",
|
||||
"value": "`%bot.prefix%modules` Lists all bot modules.
|
||||
`%prefix%h CommandName` Shows some help about a specific command.
|
||||
`%prefix%commands ModuleName` Lists all commands in a module.",
|
||||
"inline": false
|
||||
},
|
||||
{
|
||||
"name": "List of all Commands",
|
||||
"value": "https://nadeko.bot/commands",
|
||||
"inline": false
|
||||
},
|
||||
{
|
||||
"name": "Nadeko Support Server",
|
||||
"value": "https://discord.nadeko.bot/ ",
|
||||
"inline": true
|
||||
}
|
||||
]
|
||||
}
|
||||
""";
|
||||
var blocked = new BlockedConfig();
|
||||
Blocked = blocked;
|
||||
Prefix = ".";
|
||||
RotateStatuses = false;
|
||||
GroupGreets = false;
|
||||
DmHelpTextKeywords = new()
|
||||
{
|
||||
"help",
|
||||
"commands",
|
||||
"cmds",
|
||||
"module",
|
||||
"can you do"
|
||||
};
|
||||
}
|
||||
|
||||
// [Comment(@"Whether the prefix will be a suffix, or prefix.
|
||||
// For example, if your prefix is ! you will run a command called 'cash' by typing either
|
||||
// '!cash @Someone' if your prefixIsSuffix: false or
|
||||
// 'cash @Someone!' if your prefixIsSuffix: true")]
|
||||
// public bool PrefixIsSuffix { get; set; }
|
||||
|
||||
// public string Prefixed(string text) => PrefixIsSuffix
|
||||
// ? text + Prefix
|
||||
// : Prefix + text;
|
||||
|
||||
public string Prefixed(string text)
|
||||
=> Prefix + text;
|
||||
}
|
||||
|
||||
[Cloneable]
|
||||
public sealed partial class BlockedConfig
|
||||
{
|
||||
public HashSet<string> Commands { get; set; }
|
||||
public HashSet<string> Modules { get; set; }
|
||||
|
||||
public BlockedConfig()
|
||||
{
|
||||
Modules = new();
|
||||
Commands = new();
|
||||
}
|
||||
}
|
||||
|
||||
[Cloneable]
|
||||
public partial class ColorConfig
|
||||
{
|
||||
[Comment("""Color used for embed responses when command successfully executes""")]
|
||||
public Rgba32 Ok { get; set; }
|
||||
|
||||
[Comment("""Color used for embed responses when command has an error""")]
|
||||
public Rgba32 Error { get; set; }
|
||||
|
||||
[Comment("""Color used for embed responses while command is doing work or is in progress""")]
|
||||
public Rgba32 Pending { get; set; }
|
||||
|
||||
public ColorConfig()
|
||||
{
|
||||
Ok = Rgba32.ParseHex("00e584");
|
||||
Error = Rgba32.ParseHex("ee281f");
|
||||
Pending = Rgba32.ParseHex("faa61a");
|
||||
}
|
||||
}
|
||||
|
||||
public enum ConsoleOutputType
|
||||
{
|
||||
Normal = 0,
|
||||
Simple = 1,
|
||||
None = 2
|
||||
}
|
18
src/NadekoBot/_common/Configs/IConfigSeria.cs
Normal file
18
src/NadekoBot/_common/Configs/IConfigSeria.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
namespace NadekoBot.Common.Configs;
|
||||
|
||||
/// <summary>
|
||||
/// Base interface for available config serializers
|
||||
/// </summary>
|
||||
public interface IConfigSeria
|
||||
{
|
||||
/// <summary>
|
||||
/// Serialize the object to string
|
||||
/// </summary>
|
||||
public string Serialize<T>(T obj)
|
||||
where T : notnull;
|
||||
|
||||
/// <summary>
|
||||
/// Deserialize string data into an object of the specified type
|
||||
/// </summary>
|
||||
public T Deserialize<T>(string data);
|
||||
}
|
275
src/NadekoBot/_common/Creds.cs
Normal file
275
src/NadekoBot/_common/Creds.cs
Normal file
@@ -0,0 +1,275 @@
|
||||
#nullable disable
|
||||
using NadekoBot.Common.Yml;
|
||||
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public sealed class Creds : IBotCredentials
|
||||
{
|
||||
[Comment("""DO NOT CHANGE""")]
|
||||
public int Version { get; set; }
|
||||
|
||||
[Comment("""Bot token. Do not share with anyone ever -> https://discordapp.com/developers/applications/""")]
|
||||
public string Token { get; set; }
|
||||
|
||||
[Comment("""
|
||||
List of Ids of the users who have bot owner permissions
|
||||
**DO NOT ADD PEOPLE YOU DON'T TRUST**
|
||||
""")]
|
||||
public ICollection<ulong> OwnerIds { get; set; }
|
||||
|
||||
[Comment("Keep this on 'true' unless you're sure your bot shouldn't use privileged intents or you're waiting to be accepted")]
|
||||
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.
|
||||
""")]
|
||||
public int TotalShards { get; set; }
|
||||
|
||||
[Comment(
|
||||
"""
|
||||
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.
|
||||
Used only for Youtube Data Api (at the moment).
|
||||
""")]
|
||||
public string GoogleApiKey { get; set; }
|
||||
|
||||
[Comment(
|
||||
"""
|
||||
Create a new custom search here https://programmablesearchengine.google.com/cse/create/new
|
||||
Enable SafeSearch
|
||||
Remove all Sites to Search
|
||||
Enable Search the entire web
|
||||
Copy the 'Search Engine ID' to the SearchId field
|
||||
|
||||
Do all steps again but enable image search for the ImageSearchId
|
||||
""")]
|
||||
public GoogleApiConfig Google { get; set; }
|
||||
|
||||
[Comment("""Settings for voting system for discordbots. Meant for use on global Nadeko.""")]
|
||||
public VotesSettings Votes { get; set; }
|
||||
|
||||
[Comment("""
|
||||
Patreon auto reward system settings.
|
||||
go to https://www.patreon.com/portal -> my clients -> create client
|
||||
""")]
|
||||
public PatreonSettings Patreon { get; set; }
|
||||
|
||||
[Comment("""Api key for sending stats to DiscordBotList.""")]
|
||||
public string BotListToken { get; set; }
|
||||
|
||||
[Comment("""Official cleverbot api key.""")]
|
||||
public string CleverbotApiKey { get; set; }
|
||||
|
||||
[Comment(@"Official GPT-3 api key.")]
|
||||
public string Gpt3ApiKey { 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'
|
||||
""")]
|
||||
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""")]
|
||||
public DbOptions Db { get; set; }
|
||||
|
||||
[Comment("""
|
||||
Address and port of the coordinator endpoint. Leave empty for default.
|
||||
Change only if you've changed the coordinator address or port.
|
||||
""")]
|
||||
public string CoordinatorUrl { get; set; }
|
||||
|
||||
[Comment(
|
||||
"""Api key obtained on https://rapidapi.com (go to MyApps -> Add New App -> Enter Name -> Application key)""")]
|
||||
public string RapidApiKey { get; set; }
|
||||
|
||||
[Comment("""
|
||||
https://locationiq.com api key (register and you will receive the token in the email).
|
||||
Used only for .time command.
|
||||
""")]
|
||||
public string LocationIqApiKey { get; set; }
|
||||
|
||||
[Comment("""
|
||||
https://timezonedb.com api key (register and you will receive the token in the email).
|
||||
Used only for .time command
|
||||
""")]
|
||||
public string TimezoneDbApiKey { get; set; }
|
||||
|
||||
[Comment("""
|
||||
https://pro.coinmarketcap.com/account/ api key. There is a free plan for personal use.
|
||||
Used for cryptocurrency related commands.
|
||||
""")]
|
||||
public string CoinmarketcapApiKey { get; set; }
|
||||
|
||||
// [Comment(@"https://polygon.io/dashboard/api-keys api key. Free plan allows for 5 queries per minute.
|
||||
// Used for stocks related commands.")]
|
||||
// public string PolygonIoApiKey { get; set; }
|
||||
|
||||
[Comment("""Api key used for Osu related commands. Obtain this key at https://osu.ppy.sh/p/api""")]
|
||||
public string OsuApiKey { get; set; }
|
||||
|
||||
[Comment("""
|
||||
Optional Trovo client id.
|
||||
You should use this if Trovo stream notifications stopped working or you're getting ratelimit errors.
|
||||
""")]
|
||||
public string TrovoClientId { get; set; }
|
||||
|
||||
[Comment("""Obtain by creating an application at https://dev.twitch.tv/console/apps""")]
|
||||
public string TwitchClientId { get; set; }
|
||||
|
||||
[Comment("""Obtain by creating an application at https://dev.twitch.tv/console/apps""")]
|
||||
public string TwitchClientSecret { get; set; }
|
||||
|
||||
[Comment("""
|
||||
Command and args which will be used to restart the bot.
|
||||
Only used if bot is executed directly (NOT through the coordinator)
|
||||
placeholders:
|
||||
{0} -> shard id
|
||||
{1} -> total shards
|
||||
Linux default
|
||||
cmd: dotnet
|
||||
args: "NadekoBot.dll -- {0}"
|
||||
Windows default
|
||||
cmd: NadekoBot.exe
|
||||
args: "{0}"
|
||||
""")]
|
||||
public RestartConfig RestartCommand { get; set; }
|
||||
|
||||
public Creds()
|
||||
{
|
||||
Version = 7;
|
||||
Token = string.Empty;
|
||||
UsePrivilegedIntents = true;
|
||||
OwnerIds = new List<ulong>();
|
||||
TotalShards = 1;
|
||||
GoogleApiKey = string.Empty;
|
||||
Votes = new VotesSettings(string.Empty, string.Empty, string.Empty, string.Empty);
|
||||
Patreon = new PatreonSettings(string.Empty, string.Empty, string.Empty, string.Empty);
|
||||
BotListToken = string.Empty;
|
||||
CleverbotApiKey = string.Empty;
|
||||
Gpt3ApiKey = string.Empty;
|
||||
BotCache = BotCacheImplemenation.Memory;
|
||||
RedisOptions = "localhost:6379,syncTimeout=30000,responseTimeout=30000,allowAdmin=true,password=";
|
||||
Db = new DbOptions()
|
||||
{
|
||||
Type = "sqlite",
|
||||
ConnectionString = "Data Source=data/NadekoBot.db"
|
||||
};
|
||||
|
||||
CoordinatorUrl = "http://localhost:3442";
|
||||
|
||||
RestartCommand = new RestartConfig();
|
||||
Google = new GoogleApiConfig();
|
||||
}
|
||||
|
||||
public class DbOptions
|
||||
: IDbOptions
|
||||
{
|
||||
[Comment("""
|
||||
Database type. "sqlite", "mysql" and "postgresql" are supported.
|
||||
Default is "sqlite"
|
||||
""")]
|
||||
public string Type { get; set; }
|
||||
|
||||
[Comment("""
|
||||
Database connection string.
|
||||
You MUST change this if you're not using "sqlite" type.
|
||||
Default is "Data Source=data/NadekoBot.db"
|
||||
Example for mysql: "Server=localhost;Port=3306;Uid=root;Pwd=my_super_secret_mysql_password;Database=nadeko"
|
||||
Example for postgresql: "Server=localhost;Port=5432;User Id=postgres;Password=my_super_secret_postgres_password;Database=nadeko;"
|
||||
""")]
|
||||
public string ConnectionString { get; set; }
|
||||
}
|
||||
|
||||
public sealed record PatreonSettings : IPatreonSettings
|
||||
{
|
||||
public string ClientId { get; set; }
|
||||
public string AccessToken { get; set; }
|
||||
public string RefreshToken { get; set; }
|
||||
public string ClientSecret { get; set; }
|
||||
|
||||
[Comment(
|
||||
"""Campaign ID of your patreon page. Go to your patreon page (make sure you're logged in) and type "prompt('Campaign ID', window.patreon.bootstrap.creator.data.id);" in the console. (ctrl + shift + i)""")]
|
||||
public string CampaignId { get; set; }
|
||||
|
||||
public PatreonSettings(
|
||||
string accessToken,
|
||||
string refreshToken,
|
||||
string clientSecret,
|
||||
string campaignId)
|
||||
{
|
||||
AccessToken = accessToken;
|
||||
RefreshToken = refreshToken;
|
||||
ClientSecret = clientSecret;
|
||||
CampaignId = campaignId;
|
||||
}
|
||||
|
||||
public PatreonSettings()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public sealed record VotesSettings : IVotesSettings
|
||||
{
|
||||
[Comment("""
|
||||
top.gg votes service url
|
||||
This is the url of your instance of the NadekoBot.Votes api
|
||||
Example: https://votes.my.cool.bot.com
|
||||
""")]
|
||||
public string TopggServiceUrl { get; set; }
|
||||
|
||||
[Comment("""
|
||||
Authorization header value sent to the TopGG service url with each request
|
||||
This should be equivalent to the TopggKey in your NadekoBot.Votes api appsettings.json file
|
||||
""")]
|
||||
public string TopggKey { get; set; }
|
||||
|
||||
[Comment("""
|
||||
discords.com votes service url
|
||||
This is the url of your instance of the NadekoBot.Votes api
|
||||
Example: https://votes.my.cool.bot.com
|
||||
""")]
|
||||
public string DiscordsServiceUrl { get; set; }
|
||||
|
||||
[Comment("""
|
||||
Authorization header value sent to the Discords service url with each request
|
||||
This should be equivalent to the DiscordsKey in your NadekoBot.Votes api appsettings.json file
|
||||
""")]
|
||||
public string DiscordsKey { get; set; }
|
||||
|
||||
public VotesSettings()
|
||||
{
|
||||
}
|
||||
|
||||
public VotesSettings(
|
||||
string topggServiceUrl,
|
||||
string topggKey,
|
||||
string discordsServiceUrl,
|
||||
string discordsKey)
|
||||
{
|
||||
TopggServiceUrl = topggServiceUrl;
|
||||
TopggKey = topggKey;
|
||||
DiscordsServiceUrl = discordsServiceUrl;
|
||||
DiscordsKey = discordsKey;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class GoogleApiConfig : IGoogleApiConfig
|
||||
{
|
||||
public string SearchId { get; init; }
|
||||
public string ImageSearchId { get; init; }
|
||||
}
|
||||
|
||||
|
||||
|
6
src/NadekoBot/_common/Currency/CurrencyType.cs
Normal file
6
src/NadekoBot/_common/Currency/CurrencyType.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace NadekoBot.Services.Currency;
|
||||
|
||||
public enum CurrencyType
|
||||
{
|
||||
Default
|
||||
}
|
10
src/NadekoBot/_common/Currency/IBankService.cs
Normal file
10
src/NadekoBot/_common/Currency/IBankService.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace NadekoBot.Modules.Gambling.Bank;
|
||||
|
||||
public interface IBankService
|
||||
{
|
||||
Task<bool> DepositAsync(ulong userId, long amount);
|
||||
Task<bool> WithdrawAsync(ulong userId, long amount);
|
||||
Task<long> GetBalanceAsync(ulong userId);
|
||||
Task<bool> AwardAsync(ulong userId, long amount);
|
||||
Task<bool> TakeAsync(ulong userId, long amount);
|
||||
}
|
43
src/NadekoBot/_common/Currency/ICurrencyService.cs
Normal file
43
src/NadekoBot/_common/Currency/ICurrencyService.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using NadekoBot.Db.Models;
|
||||
using NadekoBot.Services.Currency;
|
||||
|
||||
namespace NadekoBot.Services;
|
||||
|
||||
public interface ICurrencyService
|
||||
{
|
||||
Task<IWallet> GetWalletAsync(ulong userId, CurrencyType type = CurrencyType.Default);
|
||||
|
||||
Task AddBulkAsync(
|
||||
IReadOnlyCollection<ulong> userIds,
|
||||
long amount,
|
||||
TxData? txData,
|
||||
CurrencyType type = CurrencyType.Default);
|
||||
|
||||
Task RemoveBulkAsync(
|
||||
IReadOnlyCollection<ulong> userIds,
|
||||
long amount,
|
||||
TxData? txData,
|
||||
CurrencyType type = CurrencyType.Default);
|
||||
|
||||
Task AddAsync(
|
||||
ulong userId,
|
||||
long amount,
|
||||
TxData? txData);
|
||||
|
||||
Task AddAsync(
|
||||
IUser user,
|
||||
long amount,
|
||||
TxData? txData);
|
||||
|
||||
Task<bool> RemoveAsync(
|
||||
ulong userId,
|
||||
long amount,
|
||||
TxData? txData);
|
||||
|
||||
Task<bool> RemoveAsync(
|
||||
IUser user,
|
||||
long amount,
|
||||
TxData? txData);
|
||||
|
||||
Task<IReadOnlyList<DiscordUser>> GetTopRichest(ulong ignoreId, int page = 0, int perPage = 9);
|
||||
}
|
9
src/NadekoBot/_common/Currency/ITxTracker.cs
Normal file
9
src/NadekoBot/_common/Currency/ITxTracker.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using NadekoBot.Services.Currency;
|
||||
|
||||
namespace NadekoBot.Services;
|
||||
|
||||
public interface ITxTracker
|
||||
{
|
||||
Task TrackAdd(long amount, TxData? txData);
|
||||
Task TrackRemove(long amount, TxData? txData);
|
||||
}
|
40
src/NadekoBot/_common/Currency/IWallet.cs
Normal file
40
src/NadekoBot/_common/Currency/IWallet.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
namespace NadekoBot.Services.Currency;
|
||||
|
||||
public interface IWallet
|
||||
{
|
||||
public ulong UserId { get; }
|
||||
|
||||
public Task<long> GetBalance();
|
||||
public Task<bool> Take(long amount, TxData? txData);
|
||||
public Task Add(long amount, TxData? txData);
|
||||
|
||||
public async Task<bool> Transfer(
|
||||
long amount,
|
||||
IWallet to,
|
||||
TxData? txData)
|
||||
{
|
||||
if (amount <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(amount), "Amount must be greater than 0.");
|
||||
|
||||
if (txData is not null)
|
||||
txData = txData with
|
||||
{
|
||||
OtherId = to.UserId
|
||||
};
|
||||
|
||||
var succ = await Take(amount, txData);
|
||||
|
||||
if (!succ)
|
||||
return false;
|
||||
|
||||
if (txData is not null)
|
||||
txData = txData with
|
||||
{
|
||||
OtherId = UserId
|
||||
};
|
||||
|
||||
await to.Add(amount, txData);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
7
src/NadekoBot/_common/Currency/TxData.cs
Normal file
7
src/NadekoBot/_common/Currency/TxData.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace NadekoBot.Services.Currency;
|
||||
|
||||
public record class TxData(
|
||||
string Type,
|
||||
string Extra,
|
||||
string? Note = "",
|
||||
ulong? OtherId = null);
|
18
src/NadekoBot/_common/DbService.cs
Normal file
18
src/NadekoBot/_common/DbService.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
#nullable disable
|
||||
using LinqToDB.Common;
|
||||
using LinqToDB.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
|
||||
namespace NadekoBot.Services;
|
||||
|
||||
public abstract class DbService
|
||||
{
|
||||
/// <summary>
|
||||
/// Call this to apply all migrations
|
||||
/// </summary>
|
||||
public abstract Task SetupAsync();
|
||||
|
||||
public abstract DbContext CreateRawDbContext(string dbType, string connString);
|
||||
public abstract DbContext GetDbContext();
|
||||
}
|
309
src/NadekoBot/_common/Deck/Deck.cs
Normal file
309
src/NadekoBot/_common/Deck/Deck.cs
Normal file
@@ -0,0 +1,309 @@
|
||||
#nullable disable
|
||||
namespace Nadeko.Econ;
|
||||
|
||||
public class Deck
|
||||
{
|
||||
public enum CardSuit
|
||||
{
|
||||
Spades = 1,
|
||||
Hearts = 2,
|
||||
Diamonds = 3,
|
||||
Clubs = 4
|
||||
}
|
||||
|
||||
private static readonly Dictionary<int, string> _cardNames = new()
|
||||
{
|
||||
{ 1, "Ace" },
|
||||
{ 2, "Two" },
|
||||
{ 3, "Three" },
|
||||
{ 4, "Four" },
|
||||
{ 5, "Five" },
|
||||
{ 6, "Six" },
|
||||
{ 7, "Seven" },
|
||||
{ 8, "Eight" },
|
||||
{ 9, "Nine" },
|
||||
{ 10, "Ten" },
|
||||
{ 11, "Jack" },
|
||||
{ 12, "Queen" },
|
||||
{ 13, "King" }
|
||||
};
|
||||
|
||||
private static Dictionary<string, Func<List<Card>, bool>> handValues;
|
||||
|
||||
public List<Card> CardPool { get; set; }
|
||||
private readonly Random _r = new NadekoRandom();
|
||||
|
||||
static Deck()
|
||||
=> InitHandValues();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the BlackJackGame, this allows you to create multiple games running at one time.
|
||||
/// </summary>
|
||||
public Deck()
|
||||
=> RefillPool();
|
||||
|
||||
/// <summary>
|
||||
/// Restart the game of blackjack. It will only refill the pool for now. Probably wont be used, unless you want to have
|
||||
/// only 1 bjg running at one time,
|
||||
/// then you will restart the same game every time.
|
||||
/// </summary>
|
||||
public void Restart()
|
||||
=> RefillPool();
|
||||
|
||||
/// <summary>
|
||||
/// Removes all cards from the pool and refills the pool with all of the possible cards. NOTE: I think this is too
|
||||
/// expensive.
|
||||
/// We should probably make it so it copies another premade list with all the cards, or something.
|
||||
/// </summary>
|
||||
protected virtual void RefillPool()
|
||||
{
|
||||
CardPool = new(52);
|
||||
//foreach suit
|
||||
for (var j = 1; j < 14; j++)
|
||||
// and number
|
||||
for (var i = 1; i < 5; i++)
|
||||
//generate a card of that suit and number and add it to the pool
|
||||
|
||||
// the pool will go from ace of spades,hears,diamonds,clubs all the way to the king of spades. hearts, ...
|
||||
CardPool.Add(new((CardSuit)i, j));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Take a card from the pool, you either take it from the top if the deck is shuffled, or from a random place if the
|
||||
/// deck is in the default order.
|
||||
/// </summary>
|
||||
/// <returns>A card from the pool</returns>
|
||||
public Card Draw()
|
||||
{
|
||||
if (CardPool.Count == 0)
|
||||
Restart();
|
||||
//you can either do this if your deck is not shuffled
|
||||
|
||||
var num = _r.Next(0, CardPool.Count);
|
||||
var c = CardPool[num];
|
||||
CardPool.RemoveAt(num);
|
||||
return c;
|
||||
|
||||
// if you want to shuffle when you fill, then take the first one
|
||||
/*
|
||||
Card c = cardPool[0];
|
||||
cardPool.RemoveAt(0);
|
||||
return c;
|
||||
*/
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shuffles the deck. Use this if you want to take cards from the top of the deck, instead of randomly. See DrawACard
|
||||
/// method.
|
||||
/// </summary>
|
||||
private void Shuffle()
|
||||
{
|
||||
if (CardPool.Count <= 1)
|
||||
return;
|
||||
var orderedPool = CardPool.Shuffle();
|
||||
CardPool ??= orderedPool.ToList();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
=> string.Concat(CardPool.Select(c => c.ToString())) + Environment.NewLine;
|
||||
|
||||
private static void InitHandValues()
|
||||
{
|
||||
bool HasPair(List<Card> cards)
|
||||
{
|
||||
return cards.GroupBy(card => card.Number).Count(group => group.Count() == 2) == 1;
|
||||
}
|
||||
|
||||
bool IsPair(List<Card> cards)
|
||||
{
|
||||
return cards.GroupBy(card => card.Number).Count(group => group.Count() == 3) == 0 && HasPair(cards);
|
||||
}
|
||||
|
||||
bool IsTwoPair(List<Card> cards)
|
||||
{
|
||||
return cards.GroupBy(card => card.Number).Count(group => group.Count() == 2) == 2;
|
||||
}
|
||||
|
||||
bool IsStraight(List<Card> cards)
|
||||
{
|
||||
if (cards.GroupBy(card => card.Number).Count() != cards.Count())
|
||||
return false;
|
||||
var toReturn = cards.Max(card => card.Number) - cards.Min(card => card.Number) == 4;
|
||||
if (toReturn || cards.All(c => c.Number != 1))
|
||||
return toReturn;
|
||||
|
||||
var newCards = cards.Select(c => c.Number == 1 ? new(c.Suit, 14) : c).ToArray();
|
||||
return newCards.Max(card => card.Number) - newCards.Min(card => card.Number) == 4;
|
||||
}
|
||||
|
||||
bool HasThreeOfKind(List<Card> cards)
|
||||
{
|
||||
return cards.GroupBy(card => card.Number).Any(group => group.Count() == 3);
|
||||
}
|
||||
|
||||
bool IsThreeOfKind(List<Card> cards)
|
||||
{
|
||||
return HasThreeOfKind(cards) && !HasPair(cards);
|
||||
}
|
||||
|
||||
bool IsFlush(List<Card> cards)
|
||||
{
|
||||
return cards.GroupBy(card => card.Suit).Count() == 1;
|
||||
}
|
||||
|
||||
bool IsFourOfKind(List<Card> cards)
|
||||
{
|
||||
return cards.GroupBy(card => card.Number).Any(group => group.Count() == 4);
|
||||
}
|
||||
|
||||
bool IsFullHouse(List<Card> cards)
|
||||
{
|
||||
return HasPair(cards) && HasThreeOfKind(cards);
|
||||
}
|
||||
|
||||
bool HasStraightFlush(List<Card> cards)
|
||||
{
|
||||
return IsFlush(cards) && IsStraight(cards);
|
||||
}
|
||||
|
||||
bool IsRoyalFlush(List<Card> cards)
|
||||
{
|
||||
return cards.Min(card => card.Number) == 1
|
||||
&& cards.Max(card => card.Number) == 13
|
||||
&& HasStraightFlush(cards);
|
||||
}
|
||||
|
||||
bool IsStraightFlush(List<Card> cards)
|
||||
{
|
||||
return HasStraightFlush(cards) && !IsRoyalFlush(cards);
|
||||
}
|
||||
|
||||
handValues = new()
|
||||
{
|
||||
{ "Royal Flush", IsRoyalFlush },
|
||||
{ "Straight Flush", IsStraightFlush },
|
||||
{ "Four Of A Kind", IsFourOfKind },
|
||||
{ "Full House", IsFullHouse },
|
||||
{ "Flush", IsFlush },
|
||||
{ "Straight", IsStraight },
|
||||
{ "Three Of A Kind", IsThreeOfKind },
|
||||
{ "Two Pairs", IsTwoPair },
|
||||
{ "A Pair", IsPair }
|
||||
};
|
||||
}
|
||||
|
||||
public static string GetHandValue(List<Card> cards)
|
||||
{
|
||||
if (handValues is null)
|
||||
InitHandValues();
|
||||
|
||||
foreach (var kvp in handValues.Where(x => x.Value(cards)))
|
||||
return kvp.Key;
|
||||
return "High card " + (cards.FirstOrDefault(c => c.Number == 1)?.GetValueText() ?? cards.Max().GetValueText());
|
||||
}
|
||||
|
||||
public class Card : IComparable
|
||||
{
|
||||
private static readonly IReadOnlyDictionary<CardSuit, string> _suitToSuitChar = new Dictionary<CardSuit, string>
|
||||
{
|
||||
{ CardSuit.Diamonds, "♦" },
|
||||
{ CardSuit.Clubs, "♣" },
|
||||
{ CardSuit.Spades, "♠" },
|
||||
{ CardSuit.Hearts, "♥" }
|
||||
};
|
||||
|
||||
private static readonly IReadOnlyDictionary<string, CardSuit> _suitCharToSuit = new Dictionary<string, CardSuit>
|
||||
{
|
||||
{ "♦", CardSuit.Diamonds },
|
||||
{ "d", CardSuit.Diamonds },
|
||||
{ "♣", CardSuit.Clubs },
|
||||
{ "c", CardSuit.Clubs },
|
||||
{ "♠", CardSuit.Spades },
|
||||
{ "s", CardSuit.Spades },
|
||||
{ "♥", CardSuit.Hearts },
|
||||
{ "h", CardSuit.Hearts }
|
||||
};
|
||||
|
||||
private static readonly IReadOnlyDictionary<char, int> _numberCharToNumber = new Dictionary<char, int>
|
||||
{
|
||||
{ 'a', 1 },
|
||||
{ '2', 2 },
|
||||
{ '3', 3 },
|
||||
{ '4', 4 },
|
||||
{ '5', 5 },
|
||||
{ '6', 6 },
|
||||
{ '7', 7 },
|
||||
{ '8', 8 },
|
||||
{ '9', 9 },
|
||||
{ 't', 10 },
|
||||
{ 'j', 11 },
|
||||
{ 'q', 12 },
|
||||
{ 'k', 13 }
|
||||
};
|
||||
|
||||
public CardSuit Suit { get; }
|
||||
public int Number { get; }
|
||||
|
||||
public string FullName
|
||||
{
|
||||
get
|
||||
{
|
||||
var str = string.Empty;
|
||||
|
||||
if (Number is <= 10 and > 1)
|
||||
str += "_" + Number;
|
||||
else
|
||||
str += GetValueText().ToLowerInvariant();
|
||||
return str + "_of_" + Suit.ToString().ToLowerInvariant();
|
||||
}
|
||||
}
|
||||
|
||||
private readonly string[] _regIndicators =
|
||||
{
|
||||
"🇦", ":two:", ":three:", ":four:", ":five:", ":six:", ":seven:", ":eight:", ":nine:", ":keycap_ten:",
|
||||
"🇯", "🇶", "🇰"
|
||||
};
|
||||
|
||||
public Card(CardSuit s, int cardNum)
|
||||
{
|
||||
Suit = s;
|
||||
Number = cardNum;
|
||||
}
|
||||
|
||||
public string GetValueText()
|
||||
=> _cardNames[Number];
|
||||
|
||||
public override string ToString()
|
||||
=> _cardNames[Number] + " Of " + Suit;
|
||||
|
||||
public int CompareTo(object obj)
|
||||
{
|
||||
if (obj is not Card card)
|
||||
return 0;
|
||||
return Number - card.Number;
|
||||
}
|
||||
|
||||
public static Card Parse(string input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
throw new ArgumentNullException(nameof(input));
|
||||
|
||||
if (input.Length != 2
|
||||
|| !_numberCharToNumber.TryGetValue(input[0], out var n)
|
||||
|| !_suitCharToSuit.TryGetValue(input[1].ToString(), out var s))
|
||||
throw new ArgumentException("Invalid input", nameof(input));
|
||||
|
||||
return new(s, n);
|
||||
}
|
||||
|
||||
public string GetEmojiString()
|
||||
{
|
||||
var str = string.Empty;
|
||||
|
||||
str += _regIndicators[Number - 1];
|
||||
str += _suitToSuitChar[Suit];
|
||||
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
5
src/NadekoBot/_common/Deck/NewCard.cs
Normal file
5
src/NadekoBot/_common/Deck/NewCard.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace Nadeko.Econ;
|
||||
|
||||
public abstract record class NewCard<TSuit, TValue>(TSuit Suit, TValue Value)
|
||||
where TSuit : struct, Enum
|
||||
where TValue : struct, Enum;
|
54
src/NadekoBot/_common/Deck/NewDeck.cs
Normal file
54
src/NadekoBot/_common/Deck/NewDeck.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
namespace Nadeko.Econ;
|
||||
|
||||
public abstract class NewDeck<TCard, TSuit, TValue>
|
||||
where TCard: NewCard<TSuit, TValue>
|
||||
where TSuit : struct, Enum
|
||||
where TValue : struct, Enum
|
||||
{
|
||||
protected static readonly TSuit[] _suits = Enum.GetValues<TSuit>();
|
||||
protected static readonly TValue[] _values = Enum.GetValues<TValue>();
|
||||
|
||||
public virtual int CurrentCount
|
||||
=> _cards.Count;
|
||||
|
||||
public virtual int TotalCount { get; }
|
||||
|
||||
protected readonly LinkedList<TCard> _cards = new();
|
||||
public NewDeck()
|
||||
{
|
||||
TotalCount = _suits.Length * _values.Length;
|
||||
}
|
||||
|
||||
public virtual TCard? Draw()
|
||||
{
|
||||
var first = _cards.First;
|
||||
if (first is not null)
|
||||
{
|
||||
_cards.RemoveFirst();
|
||||
return first.Value;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual TCard? Peek(int x = 0)
|
||||
{
|
||||
var card = _cards.First;
|
||||
for (var i = 0; i < x; i++)
|
||||
{
|
||||
card = card?.Next;
|
||||
}
|
||||
|
||||
return card?.Value;
|
||||
}
|
||||
|
||||
public virtual void Shuffle()
|
||||
{
|
||||
var cards = _cards.ToList();
|
||||
var newCards = cards.Shuffle();
|
||||
|
||||
_cards.Clear();
|
||||
foreach (var card in newCards)
|
||||
_cards.AddFirst(card);
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
namespace Nadeko.Econ;
|
||||
|
||||
public class MultipleRegularDeck : NewDeck<RegularCard, RegularSuit, RegularValue>
|
||||
{
|
||||
private int Decks { get; }
|
||||
|
||||
public override int TotalCount { get; }
|
||||
|
||||
public MultipleRegularDeck(int decks = 1)
|
||||
{
|
||||
if (decks < 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(decks), "Has to be more than 0");
|
||||
|
||||
Decks = decks;
|
||||
TotalCount = base.TotalCount * decks;
|
||||
|
||||
for (var i = 0; i < Decks; i++)
|
||||
{
|
||||
foreach (var suit in _suits)
|
||||
{
|
||||
foreach (var val in _values)
|
||||
{
|
||||
_cards.AddLast((RegularCard)Activator.CreateInstance(typeof(RegularCard), suit, val)!);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
4
src/NadekoBot/_common/Deck/Regular/RegularCard.cs
Normal file
4
src/NadekoBot/_common/Deck/Regular/RegularCard.cs
Normal file
@@ -0,0 +1,4 @@
|
||||
namespace Nadeko.Econ;
|
||||
|
||||
public sealed record class RegularCard(RegularSuit Suit, RegularValue Value)
|
||||
: NewCard<RegularSuit, RegularValue>(Suit, Value);
|
15
src/NadekoBot/_common/Deck/Regular/RegularDeck.cs
Normal file
15
src/NadekoBot/_common/Deck/Regular/RegularDeck.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace Nadeko.Econ;
|
||||
|
||||
public sealed class RegularDeck : NewDeck<RegularCard, RegularSuit, RegularValue>
|
||||
{
|
||||
public RegularDeck()
|
||||
{
|
||||
foreach (var suit in _suits)
|
||||
{
|
||||
foreach (var val in _values)
|
||||
{
|
||||
_cards.AddLast((RegularCard)Activator.CreateInstance(typeof(RegularCard), suit, val)!);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
56
src/NadekoBot/_common/Deck/Regular/RegularDeckExtensions.cs
Normal file
56
src/NadekoBot/_common/Deck/Regular/RegularDeckExtensions.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
namespace Nadeko.Econ;
|
||||
|
||||
public static class RegularDeckExtensions
|
||||
{
|
||||
public static string GetEmoji(this RegularSuit suit)
|
||||
=> suit switch
|
||||
{
|
||||
RegularSuit.Hearts => "♥️",
|
||||
RegularSuit.Spades => "♠️",
|
||||
RegularSuit.Diamonds => "♦️",
|
||||
_ => "♣️",
|
||||
};
|
||||
|
||||
public static string GetEmoji(this RegularValue value)
|
||||
=> value switch
|
||||
{
|
||||
RegularValue.Ace => "🇦",
|
||||
RegularValue.Two => "2️⃣",
|
||||
RegularValue.Three => "3️⃣",
|
||||
RegularValue.Four => "4️⃣",
|
||||
RegularValue.Five => "5️⃣",
|
||||
RegularValue.Six => "6️⃣",
|
||||
RegularValue.Seven => "7️⃣",
|
||||
RegularValue.Eight => "8️⃣",
|
||||
RegularValue.Nine => "9️⃣",
|
||||
RegularValue.Ten => "🔟",
|
||||
RegularValue.Jack => "🇯",
|
||||
RegularValue.Queen => "🇶",
|
||||
_ => "🇰",
|
||||
};
|
||||
|
||||
public static string GetEmoji(this RegularCard card)
|
||||
=> $"{card.Value.GetEmoji()} {card.Suit.GetEmoji()}";
|
||||
|
||||
public static string GetName(this RegularValue value)
|
||||
=> value.ToString();
|
||||
|
||||
public static string GetName(this RegularSuit suit)
|
||||
=> suit.ToString();
|
||||
|
||||
public static string GetName(this RegularCard card)
|
||||
=> $"{card.Value.ToString()} of {card.Suit.GetName()}";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
9
src/NadekoBot/_common/Deck/Regular/RegularSuit.cs
Normal file
9
src/NadekoBot/_common/Deck/Regular/RegularSuit.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Nadeko.Econ;
|
||||
|
||||
public enum RegularSuit
|
||||
{
|
||||
Hearts,
|
||||
Diamonds,
|
||||
Clubs,
|
||||
Spades
|
||||
}
|
18
src/NadekoBot/_common/Deck/Regular/RegularValue.cs
Normal file
18
src/NadekoBot/_common/Deck/Regular/RegularValue.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
namespace Nadeko.Econ;
|
||||
|
||||
public enum RegularValue
|
||||
{
|
||||
Ace = 1,
|
||||
Two = 2,
|
||||
Three = 3,
|
||||
Four = 4,
|
||||
Five = 5,
|
||||
Six = 6,
|
||||
Seven = 7,
|
||||
Eight = 8,
|
||||
Nine = 9,
|
||||
Ten = 10,
|
||||
Jack = 12,
|
||||
Queen = 13,
|
||||
King = 14,
|
||||
}
|
154
src/NadekoBot/_common/DoAsUserMessage.cs
Normal file
154
src/NadekoBot/_common/DoAsUserMessage.cs
Normal file
@@ -0,0 +1,154 @@
|
||||
using MessageType = Discord.MessageType;
|
||||
|
||||
namespace NadekoBot.Modules.Administration;
|
||||
|
||||
public sealed class DoAsUserMessage : IUserMessage
|
||||
{
|
||||
private readonly string _message;
|
||||
private IUserMessage _msg;
|
||||
private readonly IUser _user;
|
||||
|
||||
public DoAsUserMessage(SocketUserMessage msg, IUser user, string message)
|
||||
{
|
||||
_msg = msg;
|
||||
_user = user;
|
||||
_message = message;
|
||||
}
|
||||
|
||||
public ulong Id => _msg.Id;
|
||||
|
||||
public DateTimeOffset CreatedAt => _msg.CreatedAt;
|
||||
|
||||
public Task DeleteAsync(RequestOptions? options = null)
|
||||
{
|
||||
return _msg.DeleteAsync(options);
|
||||
}
|
||||
|
||||
public Task AddReactionAsync(IEmote emote, RequestOptions? options = null)
|
||||
{
|
||||
return _msg.AddReactionAsync(emote, options);
|
||||
}
|
||||
|
||||
public Task RemoveReactionAsync(IEmote emote, IUser user, RequestOptions? options = null)
|
||||
{
|
||||
return _msg.RemoveReactionAsync(emote, user, options);
|
||||
}
|
||||
|
||||
public Task RemoveReactionAsync(IEmote emote, ulong userId, RequestOptions? options = null)
|
||||
{
|
||||
return _msg.RemoveReactionAsync(emote, userId, options);
|
||||
}
|
||||
|
||||
public Task RemoveAllReactionsAsync(RequestOptions? options = null)
|
||||
{
|
||||
return _msg.RemoveAllReactionsAsync(options);
|
||||
}
|
||||
|
||||
public Task RemoveAllReactionsForEmoteAsync(IEmote emote, RequestOptions? options = null)
|
||||
{
|
||||
return _msg.RemoveAllReactionsForEmoteAsync(emote, options);
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<IReadOnlyCollection<IUser>> GetReactionUsersAsync(
|
||||
IEmote emoji,
|
||||
int limit,
|
||||
RequestOptions? options = null,
|
||||
ReactionType type = ReactionType.Normal)
|
||||
=> _msg.GetReactionUsersAsync(emoji, limit, options, type);
|
||||
|
||||
public IAsyncEnumerable<IReadOnlyCollection<IUser>> GetReactionUsersAsync(IEmote emoji, int limit,
|
||||
RequestOptions? options = null)
|
||||
{
|
||||
return _msg.GetReactionUsersAsync(emoji, limit, options);
|
||||
}
|
||||
|
||||
public MessageType Type => _msg.Type;
|
||||
|
||||
public MessageSource Source => _msg.Source;
|
||||
|
||||
public bool IsTTS => _msg.IsTTS;
|
||||
|
||||
public bool IsPinned => _msg.IsPinned;
|
||||
|
||||
public bool IsSuppressed => _msg.IsSuppressed;
|
||||
|
||||
public bool MentionedEveryone => _msg.MentionedEveryone;
|
||||
|
||||
public string Content => _message;
|
||||
|
||||
public string CleanContent => _msg.CleanContent;
|
||||
|
||||
public DateTimeOffset Timestamp => _msg.Timestamp;
|
||||
|
||||
public DateTimeOffset? EditedTimestamp => _msg.EditedTimestamp;
|
||||
|
||||
public IMessageChannel Channel => _msg.Channel;
|
||||
|
||||
public IUser Author => _user;
|
||||
|
||||
public IThreadChannel Thread => _msg.Thread;
|
||||
|
||||
public IReadOnlyCollection<IAttachment> Attachments => _msg.Attachments;
|
||||
|
||||
public IReadOnlyCollection<IEmbed> Embeds => _msg.Embeds;
|
||||
|
||||
public IReadOnlyCollection<ITag> Tags => _msg.Tags;
|
||||
|
||||
public IReadOnlyCollection<ulong> MentionedChannelIds => _msg.MentionedChannelIds;
|
||||
|
||||
public IReadOnlyCollection<ulong> MentionedRoleIds => _msg.MentionedRoleIds;
|
||||
|
||||
public IReadOnlyCollection<ulong> MentionedUserIds => _msg.MentionedUserIds;
|
||||
|
||||
public MessageActivity Activity => _msg.Activity;
|
||||
|
||||
public MessageApplication Application => _msg.Application;
|
||||
|
||||
public MessageReference Reference => _msg.Reference;
|
||||
|
||||
public IReadOnlyDictionary<IEmote, ReactionMetadata> Reactions => _msg.Reactions;
|
||||
|
||||
public IReadOnlyCollection<IMessageComponent> Components => _msg.Components;
|
||||
|
||||
public IReadOnlyCollection<IStickerItem> Stickers => _msg.Stickers;
|
||||
|
||||
public MessageFlags? Flags => _msg.Flags;
|
||||
|
||||
[Obsolete("Obsolete in favor of InteractionMetadata")]
|
||||
public IMessageInteraction Interaction => _msg.Interaction;
|
||||
public MessageRoleSubscriptionData RoleSubscriptionData => _msg.RoleSubscriptionData;
|
||||
|
||||
public Task ModifyAsync(Action<MessageProperties> func, RequestOptions? options = null)
|
||||
{
|
||||
return _msg.ModifyAsync(func, options);
|
||||
}
|
||||
|
||||
public Task PinAsync(RequestOptions? options = null)
|
||||
{
|
||||
return _msg.PinAsync(options);
|
||||
}
|
||||
|
||||
public Task UnpinAsync(RequestOptions? options = null)
|
||||
{
|
||||
return _msg.UnpinAsync(options);
|
||||
}
|
||||
|
||||
public Task CrosspostAsync(RequestOptions? options = null)
|
||||
{
|
||||
return _msg.CrosspostAsync(options);
|
||||
}
|
||||
|
||||
public string Resolve(TagHandling userHandling = TagHandling.Name, TagHandling channelHandling = TagHandling.Name,
|
||||
TagHandling roleHandling = TagHandling.Name,
|
||||
TagHandling everyoneHandling = TagHandling.Ignore, TagHandling emojiHandling = TagHandling.Name)
|
||||
{
|
||||
return _msg.Resolve(userHandling, channelHandling, roleHandling, everyoneHandling, emojiHandling);
|
||||
}
|
||||
|
||||
public MessageResolvedData ResolvedData => _msg.ResolvedData;
|
||||
|
||||
public IUserMessage ReferencedMessage => _msg.ReferencedMessage;
|
||||
|
||||
public IMessageInteractionMetadata InteractionMetadata
|
||||
=> _msg.InteractionMetadata;
|
||||
}
|
11
src/NadekoBot/_common/Extensions/DbExtensions.cs
Normal file
11
src/NadekoBot/_common/Extensions/DbExtensions.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NadekoBot.Db;
|
||||
using NadekoBot.Db.Models;
|
||||
|
||||
namespace NadekoBot.Extensions;
|
||||
|
||||
public static class DbExtensions
|
||||
{
|
||||
public static DiscordUser GetOrCreateUser(this DbContext ctx, IUser original, Func<IQueryable<DiscordUser>, IQueryable<DiscordUser>>? includes = null)
|
||||
=> ctx.GetOrCreateUser(original.Id, original.Username, original.Discriminator, original.AvatarId, includes);
|
||||
}
|
97
src/NadekoBot/_common/Extensions/ImagesharpExtensions.cs
Normal file
97
src/NadekoBot/_common/Extensions/ImagesharpExtensions.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Drawing;
|
||||
using SixLabors.ImageSharp.Drawing.Processing;
|
||||
using SixLabors.ImageSharp.Formats;
|
||||
using SixLabors.ImageSharp.Formats.Png;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using Color = Discord.Color;
|
||||
|
||||
namespace NadekoBot.Extensions;
|
||||
|
||||
public static class ImagesharpExtensions
|
||||
{
|
||||
// https://github.com/SixLabors/Samples/blob/master/ImageSharp/AvatarWithRoundedCorner/Program.cs
|
||||
public static IImageProcessingContext ApplyRoundedCorners(this IImageProcessingContext ctx, float cornerRadius)
|
||||
{
|
||||
var size = ctx.GetCurrentSize();
|
||||
var corners = BuildCorners(size.Width, size.Height, cornerRadius);
|
||||
|
||||
ctx.SetGraphicsOptions(new GraphicsOptions
|
||||
{
|
||||
Antialias = true,
|
||||
// enforces that any part of this shape that has color is punched out of the background
|
||||
AlphaCompositionMode = PixelAlphaCompositionMode.DestOut
|
||||
});
|
||||
|
||||
foreach (var c in corners)
|
||||
ctx = ctx.Fill(SixLabors.ImageSharp.Color.Red, c);
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
private static IPathCollection BuildCorners(int imageWidth, int imageHeight, float cornerRadius)
|
||||
{
|
||||
// first create a square
|
||||
var rect = new RectangularPolygon(-0.5f, -0.5f, cornerRadius, cornerRadius);
|
||||
|
||||
// then cut out of the square a circle so we are left with a corner
|
||||
var cornerTopLeft = rect.Clip(new EllipsePolygon(cornerRadius - 0.5f, cornerRadius - 0.5f, cornerRadius));
|
||||
|
||||
// corner is now a corner shape positions top left
|
||||
//lets make 3 more positioned correctly, we can do that by translating the original around the center of the image
|
||||
|
||||
var rightPos = imageWidth - cornerTopLeft.Bounds.Width + 1;
|
||||
var bottomPos = imageHeight - cornerTopLeft.Bounds.Height + 1;
|
||||
|
||||
// move it across the width of the image - the width of the shape
|
||||
var cornerTopRight = cornerTopLeft.RotateDegree(90).Translate(rightPos, 0);
|
||||
var cornerBottomLeft = cornerTopLeft.RotateDegree(-90).Translate(0, bottomPos);
|
||||
var cornerBottomRight = cornerTopLeft.RotateDegree(180).Translate(rightPos, bottomPos);
|
||||
|
||||
return new PathCollection(cornerTopLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight);
|
||||
}
|
||||
|
||||
public static Color ToDiscordColor(this Rgba32 color)
|
||||
=> new(color.R, color.G, color.B);
|
||||
|
||||
public static MemoryStream ToStream(this Image<Rgba32> img, IImageFormat? format = null)
|
||||
{
|
||||
var imageStream = new MemoryStream();
|
||||
if (format?.Name == "GIF")
|
||||
img.SaveAsGif(imageStream);
|
||||
else
|
||||
{
|
||||
img.SaveAsPng(imageStream,
|
||||
new()
|
||||
{
|
||||
ColorType = PngColorType.RgbWithAlpha,
|
||||
CompressionLevel = PngCompressionLevel.DefaultCompression
|
||||
});
|
||||
}
|
||||
|
||||
imageStream.Position = 0;
|
||||
return imageStream;
|
||||
}
|
||||
|
||||
public static async Task<MemoryStream> ToStreamAsync(this Image<Rgba32> img, IImageFormat? format = null)
|
||||
{
|
||||
var imageStream = new MemoryStream();
|
||||
if (format?.Name == "GIF")
|
||||
{
|
||||
await img.SaveAsGifAsync(imageStream);
|
||||
}
|
||||
else
|
||||
{
|
||||
await img.SaveAsPngAsync(imageStream,
|
||||
new PngEncoder()
|
||||
{
|
||||
ColorType = PngColorType.RgbWithAlpha,
|
||||
CompressionLevel = PngCompressionLevel.DefaultCompression
|
||||
});
|
||||
}
|
||||
|
||||
imageStream.Position = 0;
|
||||
return imageStream;
|
||||
}
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
namespace Nadeko.Econ.Gambling.Betdraw;
|
||||
|
||||
public enum BetdrawColorGuess
|
||||
{
|
||||
Red,
|
||||
Black
|
||||
}
|
84
src/NadekoBot/_common/Gambling/Betdraw/BetdrawGame.cs
Normal file
84
src/NadekoBot/_common/Gambling/Betdraw/BetdrawGame.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
namespace Nadeko.Econ.Gambling.Betdraw;
|
||||
|
||||
public sealed class BetdrawGame
|
||||
{
|
||||
private static readonly NadekoRandom _rng = new();
|
||||
private readonly RegularDeck _deck;
|
||||
|
||||
private const decimal SINGLE_GUESS_MULTI = 2.075M;
|
||||
private const decimal DOUBLE_GUESS_MULTI = 4.15M;
|
||||
|
||||
public BetdrawGame()
|
||||
{
|
||||
_deck = new RegularDeck();
|
||||
}
|
||||
|
||||
public BetdrawResult Draw(BetdrawValueGuess? val, BetdrawColorGuess? col, decimal amount)
|
||||
{
|
||||
if (val is null && col is null)
|
||||
throw new ArgumentNullException(nameof(val));
|
||||
|
||||
var card = _deck.Peek(_rng.Next(0, 52))!;
|
||||
|
||||
var realVal = (int)card.Value < 7
|
||||
? BetdrawValueGuess.Low
|
||||
: BetdrawValueGuess.High;
|
||||
|
||||
var realCol = card.Suit is RegularSuit.Diamonds or RegularSuit.Hearts
|
||||
? BetdrawColorGuess.Red
|
||||
: BetdrawColorGuess.Black;
|
||||
|
||||
// if card is 7, autoloss
|
||||
if (card.Value == RegularValue.Seven)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Won = 0M,
|
||||
Multiplier = 0M,
|
||||
ResultType = BetdrawResultType.Lose,
|
||||
Card = card,
|
||||
};
|
||||
}
|
||||
|
||||
byte win = 0;
|
||||
if (val is BetdrawValueGuess valGuess)
|
||||
{
|
||||
if (realVal != valGuess)
|
||||
return new()
|
||||
{
|
||||
Won = 0M,
|
||||
Multiplier = 0M,
|
||||
ResultType = BetdrawResultType.Lose,
|
||||
Card = card
|
||||
};
|
||||
|
||||
++win;
|
||||
}
|
||||
|
||||
if (col is BetdrawColorGuess colGuess)
|
||||
{
|
||||
if (realCol != colGuess)
|
||||
return new()
|
||||
{
|
||||
Won = 0M,
|
||||
Multiplier = 0M,
|
||||
ResultType = BetdrawResultType.Lose,
|
||||
Card = card
|
||||
};
|
||||
|
||||
++win;
|
||||
}
|
||||
|
||||
var multi = win == 1
|
||||
? SINGLE_GUESS_MULTI
|
||||
: DOUBLE_GUESS_MULTI;
|
||||
|
||||
return new()
|
||||
{
|
||||
Won = amount * multi,
|
||||
Multiplier = multi,
|
||||
ResultType = BetdrawResultType.Win,
|
||||
Card = card
|
||||
};
|
||||
}
|
||||
}
|
9
src/NadekoBot/_common/Gambling/Betdraw/BetdrawResult.cs
Normal file
9
src/NadekoBot/_common/Gambling/Betdraw/BetdrawResult.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Nadeko.Econ.Gambling.Betdraw;
|
||||
|
||||
public readonly struct BetdrawResult
|
||||
{
|
||||
public decimal Won { get; init; }
|
||||
public decimal Multiplier { get; init; }
|
||||
public BetdrawResultType ResultType { get; init; }
|
||||
public RegularCard Card { get; init; }
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
namespace Nadeko.Econ.Gambling.Betdraw;
|
||||
|
||||
public enum BetdrawResultType
|
||||
{
|
||||
Win,
|
||||
Lose
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
namespace Nadeko.Econ.Gambling.Betdraw;
|
||||
|
||||
public enum BetdrawValueGuess
|
||||
{
|
||||
High,
|
||||
Low,
|
||||
}
|
33
src/NadekoBot/_common/Gambling/Betflip/BetflipGame.cs
Normal file
33
src/NadekoBot/_common/Gambling/Betflip/BetflipGame.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
namespace Nadeko.Econ.Gambling;
|
||||
|
||||
public sealed class BetflipGame
|
||||
{
|
||||
private readonly decimal _winMulti;
|
||||
private static readonly NadekoRandom _rng = new NadekoRandom();
|
||||
|
||||
public BetflipGame(decimal winMulti)
|
||||
{
|
||||
_winMulti = winMulti;
|
||||
}
|
||||
|
||||
public BetflipResult Flip(byte guess, decimal amount)
|
||||
{
|
||||
var side = (byte)_rng.Next(0, 2);
|
||||
if (side == guess)
|
||||
{
|
||||
return new BetflipResult()
|
||||
{
|
||||
Side = side,
|
||||
Won = amount * _winMulti,
|
||||
Multiplier = _winMulti
|
||||
};
|
||||
}
|
||||
|
||||
return new BetflipResult()
|
||||
{
|
||||
Side = side,
|
||||
Won = 0,
|
||||
Multiplier = 0,
|
||||
};
|
||||
}
|
||||
}
|
8
src/NadekoBot/_common/Gambling/Betflip/BetflipResult.cs
Normal file
8
src/NadekoBot/_common/Gambling/Betflip/BetflipResult.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Nadeko.Econ.Gambling;
|
||||
|
||||
public readonly struct BetflipResult
|
||||
{
|
||||
public decimal Won { get; init; }
|
||||
public byte Side { get; init; }
|
||||
public decimal Multiplier { get; init; }
|
||||
}
|
42
src/NadekoBot/_common/Gambling/Betroll/BetrollGame.cs
Normal file
42
src/NadekoBot/_common/Gambling/Betroll/BetrollGame.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
namespace Nadeko.Econ.Gambling;
|
||||
|
||||
public sealed class BetrollGame
|
||||
{
|
||||
private readonly (int WhenAbove, decimal MultiplyBy)[] _thresholdPairs;
|
||||
private readonly NadekoRandom _rng;
|
||||
|
||||
public BetrollGame(IReadOnlyList<(int WhenAbove, decimal MultiplyBy)> pairs)
|
||||
{
|
||||
_thresholdPairs = pairs.OrderByDescending(x => x.WhenAbove).ToArray();
|
||||
_rng = new();
|
||||
}
|
||||
|
||||
public BetrollResult Roll(decimal amount = 0)
|
||||
{
|
||||
var roll = _rng.Next(1, 101);
|
||||
|
||||
for (var i = 0; i < _thresholdPairs.Length; i++)
|
||||
{
|
||||
ref var pair = ref _thresholdPairs[i];
|
||||
|
||||
if (pair.WhenAbove < roll)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Multiplier = pair.MultiplyBy,
|
||||
Roll = roll,
|
||||
Threshold = pair.WhenAbove,
|
||||
Won = amount * pair.MultiplyBy
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Multiplier = 0,
|
||||
Roll = roll,
|
||||
Threshold = -1,
|
||||
Won = 0,
|
||||
};
|
||||
}
|
||||
}
|
9
src/NadekoBot/_common/Gambling/Betroll/BetrollResult.cs
Normal file
9
src/NadekoBot/_common/Gambling/Betroll/BetrollResult.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Nadeko.Econ.Gambling;
|
||||
|
||||
public readonly struct BetrollResult
|
||||
{
|
||||
public int Roll { get; init; }
|
||||
public decimal Multiplier { get; init; }
|
||||
public decimal Threshold { get; init; }
|
||||
public decimal Won { get; init; }
|
||||
}
|
75
src/NadekoBot/_common/Gambling/Rps/RpsGame.cs
Normal file
75
src/NadekoBot/_common/Gambling/Rps/RpsGame.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
namespace Nadeko.Econ.Gambling.Rps;
|
||||
|
||||
public sealed class RpsGame
|
||||
{
|
||||
private static readonly NadekoRandom _rng = new NadekoRandom();
|
||||
|
||||
const decimal WIN_MULTI = 1.95m;
|
||||
const decimal DRAW_MULTI = 1m;
|
||||
const decimal LOSE_MULTI = 0m;
|
||||
|
||||
public RpsGame()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public RpsResult Play(RpsPick pick, decimal amount)
|
||||
{
|
||||
var compPick = (RpsPick)_rng.Next(0, 3);
|
||||
if (compPick == pick)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Won = amount * DRAW_MULTI,
|
||||
Multiplier = DRAW_MULTI,
|
||||
ComputerPick = compPick,
|
||||
Result = RpsResultType.Draw,
|
||||
};
|
||||
}
|
||||
|
||||
if ((compPick == RpsPick.Paper && pick == RpsPick.Rock)
|
||||
|| (compPick == RpsPick.Rock && pick == RpsPick.Scissors)
|
||||
|| (compPick == RpsPick.Scissors && pick == RpsPick.Paper))
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Won = amount * LOSE_MULTI,
|
||||
Multiplier = LOSE_MULTI,
|
||||
Result = RpsResultType.Lose,
|
||||
ComputerPick = compPick,
|
||||
};
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Won = amount * WIN_MULTI,
|
||||
Multiplier = WIN_MULTI,
|
||||
Result = RpsResultType.Win,
|
||||
ComputerPick = compPick,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public enum RpsPick : byte
|
||||
{
|
||||
Rock = 0,
|
||||
Paper = 1,
|
||||
Scissors = 2,
|
||||
}
|
||||
|
||||
public enum RpsResultType : byte
|
||||
{
|
||||
Win,
|
||||
Draw,
|
||||
Lose
|
||||
}
|
||||
|
||||
|
||||
|
||||
public readonly struct RpsResult
|
||||
{
|
||||
public decimal Won { get; init; }
|
||||
public decimal Multiplier { get; init; }
|
||||
public RpsResultType Result { get; init; }
|
||||
public RpsPick ComputerPick { get; init; }
|
||||
}
|
116
src/NadekoBot/_common/Gambling/Slot/SlotGame.cs
Normal file
116
src/NadekoBot/_common/Gambling/Slot/SlotGame.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
namespace Nadeko.Econ.Gambling;
|
||||
|
||||
//here is a payout chart
|
||||
//https://lh6.googleusercontent.com/-i1hjAJy_kN4/UswKxmhrbPI/AAAAAAAAB1U/82wq_4ZZc-Y/DE6B0895-6FC1-48BE-AC4F-14D1B91AB75B.jpg
|
||||
//thanks to judge for helping me with this
|
||||
public class SlotGame
|
||||
{
|
||||
private static readonly NadekoRandom _rng = new NadekoRandom();
|
||||
|
||||
public SlotResult Spin(decimal bet)
|
||||
{
|
||||
var rolls = new[]
|
||||
{
|
||||
(byte)_rng.Next(0, 6),
|
||||
(byte)_rng.Next(0, 6),
|
||||
(byte)_rng.Next(0, 6)
|
||||
};
|
||||
|
||||
ref var a = ref rolls[0];
|
||||
ref var b = ref rolls[1];
|
||||
ref var c = ref rolls[2];
|
||||
|
||||
var multi = 0;
|
||||
var winType = SlotWinType.None;
|
||||
if (a == b && b == c)
|
||||
{
|
||||
if (a == 5)
|
||||
{
|
||||
winType = SlotWinType.TrippleJoker;
|
||||
multi = 30;
|
||||
}
|
||||
else
|
||||
{
|
||||
winType = SlotWinType.TrippleNormal;
|
||||
multi = 10;
|
||||
}
|
||||
}
|
||||
else if (a == 5 && (b == 5 || c == 5)
|
||||
|| (b == 5 && c == 5))
|
||||
{
|
||||
winType = SlotWinType.DoubleJoker;
|
||||
multi = 4;
|
||||
}
|
||||
else if (a == 5 || b == 5 || c == 5)
|
||||
{
|
||||
winType = SlotWinType.SingleJoker;
|
||||
multi = 1;
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Won = bet * multi,
|
||||
WinType = winType,
|
||||
Multiplier = multi,
|
||||
Rolls = rolls,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public enum SlotWinType : byte
|
||||
{
|
||||
None,
|
||||
SingleJoker,
|
||||
DoubleJoker,
|
||||
TrippleNormal,
|
||||
TrippleJoker,
|
||||
}
|
||||
|
||||
/*
|
||||
var rolls = new[]
|
||||
{
|
||||
_rng.Next(default(byte), 6),
|
||||
_rng.Next(default(byte), 6),
|
||||
_rng.Next(default(byte), 6)
|
||||
};
|
||||
|
||||
var multi = 0;
|
||||
var winType = SlotWinType.None;
|
||||
|
||||
ref var a = ref rolls[0];
|
||||
ref var b = ref rolls[1];
|
||||
ref var c = ref rolls[2];
|
||||
if (a == b && b == c)
|
||||
{
|
||||
if (a == 5)
|
||||
{
|
||||
winType = SlotWinType.TrippleJoker;
|
||||
multi = 30;
|
||||
}
|
||||
else
|
||||
{
|
||||
winType = SlotWinType.TrippleNormal;
|
||||
multi = 10;
|
||||
}
|
||||
}
|
||||
else if (a == 5 && (b == 5 || c == 5)
|
||||
|| (b == 5 && c == 5))
|
||||
{
|
||||
winType = SlotWinType.DoubleJoker;
|
||||
multi = 4;
|
||||
}
|
||||
else if (rolls.Any(x => x == 5))
|
||||
{
|
||||
winType = SlotWinType.SingleJoker;
|
||||
multi = 1;
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Won = bet * multi,
|
||||
WinType = winType,
|
||||
Multiplier = multi,
|
||||
Rolls = rolls,
|
||||
};
|
||||
}
|
||||
*/
|
9
src/NadekoBot/_common/Gambling/Slot/SlotResult.cs
Normal file
9
src/NadekoBot/_common/Gambling/Slot/SlotResult.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Nadeko.Econ.Gambling;
|
||||
|
||||
public readonly struct SlotResult
|
||||
{
|
||||
public decimal Multiplier { get; init; }
|
||||
public byte[] Rolls { get; init; }
|
||||
public decimal Won { get; init; }
|
||||
public SlotWinType WinType { get; init; }
|
||||
}
|
9
src/NadekoBot/_common/Gambling/Wof/LuLaResult.cs
Normal file
9
src/NadekoBot/_common/Gambling/Wof/LuLaResult.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Nadeko.Econ.Gambling;
|
||||
|
||||
public readonly struct LuLaResult
|
||||
{
|
||||
public int Index { get; init; }
|
||||
public decimal Multiplier { get; init; }
|
||||
public decimal Won { get; init; }
|
||||
public IReadOnlyList<decimal> Multipliers { get; init; }
|
||||
}
|
34
src/NadekoBot/_common/Gambling/Wof/WofGame.cs
Normal file
34
src/NadekoBot/_common/Gambling/Wof/WofGame.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
namespace Nadeko.Econ.Gambling;
|
||||
|
||||
public sealed class LulaGame
|
||||
{
|
||||
private static readonly IReadOnlyList<decimal> DEFAULT_MULTIPLIERS = new[] { 1.7M, 1.5M, 0.2M, 0.1M, 0.3M, 0.5M, 1.2M, 2.4M };
|
||||
|
||||
private readonly IReadOnlyList<decimal> _multipliers;
|
||||
private static readonly NadekoRandom _rng = new();
|
||||
|
||||
public LulaGame(IReadOnlyList<decimal> multipliers)
|
||||
{
|
||||
_multipliers = multipliers;
|
||||
}
|
||||
|
||||
public LulaGame() : this(DEFAULT_MULTIPLIERS)
|
||||
{
|
||||
}
|
||||
|
||||
public LuLaResult Spin(long bet)
|
||||
{
|
||||
var result = _rng.Next(0, _multipliers.Count);
|
||||
|
||||
var multi = _multipliers[result];
|
||||
var amount = bet * multi;
|
||||
|
||||
return new()
|
||||
{
|
||||
Index = result,
|
||||
Multiplier = multi,
|
||||
Won = amount,
|
||||
Multipliers = _multipliers.ToArray(),
|
||||
};
|
||||
}
|
||||
}
|
12
src/NadekoBot/_common/IBot.cs
Normal file
12
src/NadekoBot/_common/IBot.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
#nullable disable
|
||||
using Nadeko.Bot.Db.Models;
|
||||
|
||||
namespace NadekoBot;
|
||||
|
||||
public interface IBot
|
||||
{
|
||||
IReadOnlyList<ulong> GetCurrentGuildIds();
|
||||
event Func<GuildConfig, Task> JoinedGuild;
|
||||
IReadOnlyCollection<GuildConfig> AllGuildConfigs { get; }
|
||||
bool IsReady { get; }
|
||||
}
|
8
src/NadekoBot/_common/ICloneable.cs
Normal file
8
src/NadekoBot/_common/ICloneable.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
#nullable disable
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public interface ICloneable<T>
|
||||
where T : new()
|
||||
{
|
||||
public T Clone();
|
||||
}
|
29
src/NadekoBot/_common/ICurrencyProvider.cs
Normal file
29
src/NadekoBot/_common/ICurrencyProvider.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Globalization;
|
||||
using System.Numerics;
|
||||
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public interface ICurrencyProvider
|
||||
{
|
||||
string GetCurrencySign();
|
||||
}
|
||||
|
||||
public static class CurrencyHelper
|
||||
{
|
||||
public static string N<T>(T cur, IFormatProvider format)
|
||||
where T : INumber<T>
|
||||
=> cur.ToString("C0", format);
|
||||
|
||||
public static string N<T>(T cur, CultureInfo culture, string currencySign)
|
||||
where T : INumber<T>
|
||||
=> N(cur, GetCurrencyFormat(culture, currencySign));
|
||||
|
||||
private static IFormatProvider GetCurrencyFormat(CultureInfo culture, string currencySign)
|
||||
{
|
||||
var flowersCurrencyCulture = (CultureInfo)culture.Clone();
|
||||
flowersCurrencyCulture.NumberFormat.CurrencySymbol = currencySign;
|
||||
flowersCurrencyCulture.NumberFormat.CurrencyNegativePattern = 5;
|
||||
|
||||
return flowersCurrencyCulture;
|
||||
}
|
||||
}
|
7
src/NadekoBot/_common/IDiscordPermOverrideService.cs
Normal file
7
src/NadekoBot/_common/IDiscordPermOverrideService.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
#nullable disable
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public interface IDiscordPermOverrideService
|
||||
{
|
||||
bool TryGetOverrides(ulong guildId, string commandName, out Nadeko.Bot.Db.GuildPerm? perm);
|
||||
}
|
35
src/NadekoBot/_common/ILogCommandService.cs
Normal file
35
src/NadekoBot/_common/ILogCommandService.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Nadeko.Bot.Db.Models;
|
||||
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public interface ILogCommandService
|
||||
{
|
||||
void AddDeleteIgnore(ulong xId);
|
||||
Task LogServer(ulong guildId, ulong channelId, bool actionValue);
|
||||
bool LogIgnore(ulong guildId, ulong itemId, IgnoredItemType itemType);
|
||||
LogSetting? GetGuildLogSettings(ulong guildId);
|
||||
bool Log(ulong guildId, ulong? channelId, LogType type);
|
||||
}
|
||||
|
||||
public enum LogType
|
||||
{
|
||||
Other,
|
||||
MessageUpdated,
|
||||
MessageDeleted,
|
||||
UserJoined,
|
||||
UserLeft,
|
||||
UserBanned,
|
||||
UserUnbanned,
|
||||
UserUpdated,
|
||||
ChannelCreated,
|
||||
ChannelDestroyed,
|
||||
ChannelUpdated,
|
||||
UserPresence,
|
||||
VoicePresence,
|
||||
VoicePresenceTts,
|
||||
UserMuted,
|
||||
UserWarned,
|
||||
|
||||
ThreadDeleted,
|
||||
ThreadCreated
|
||||
}
|
7
src/NadekoBot/_common/INadekoCommandOptions.cs
Normal file
7
src/NadekoBot/_common/INadekoCommandOptions.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
#nullable disable
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public interface INadekoCommandOptions
|
||||
{
|
||||
void NormalizeOptions();
|
||||
}
|
39
src/NadekoBot/_common/IPermissionChecker.cs
Normal file
39
src/NadekoBot/_common/IPermissionChecker.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using Nadeko.Bot.Db.Models;
|
||||
using OneOf;
|
||||
using OneOf.Types;
|
||||
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public interface IPermissionChecker
|
||||
{
|
||||
Task<PermCheckResult> CheckPermsAsync(IGuild guild,
|
||||
IMessageChannel channel,
|
||||
IUser author,
|
||||
string module,
|
||||
string? cmd);
|
||||
}
|
||||
|
||||
[GenerateOneOf]
|
||||
public partial class PermCheckResult
|
||||
: OneOfBase<PermAllowed, PermCooldown, PermGlobalBlock, PermDisallowed>
|
||||
{
|
||||
public bool IsAllowed
|
||||
=> IsT0;
|
||||
|
||||
public bool IsCooldown
|
||||
=> IsT1;
|
||||
|
||||
public bool IsGlobalBlock
|
||||
=> IsT2;
|
||||
|
||||
public bool IsDisallowed
|
||||
=> IsT3;
|
||||
}
|
||||
|
||||
public readonly record struct PermAllowed;
|
||||
|
||||
public readonly record struct PermCooldown;
|
||||
|
||||
public readonly record struct PermGlobalBlock;
|
||||
|
||||
public readonly record struct PermDisallowed(int PermIndex, string PermText, bool IsVerbose);
|
7
src/NadekoBot/_common/IPlaceholderProvider.cs
Normal file
7
src/NadekoBot/_common/IPlaceholderProvider.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
#nullable disable
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public interface IPlaceholderProvider
|
||||
{
|
||||
public IEnumerable<(string Name, Func<string> Func)> GetPlaceholders();
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
namespace NadekoBot;
|
||||
|
||||
public interface INadekoInteractionService
|
||||
{
|
||||
public NadekoInteraction Create<T>(
|
||||
ulong userId,
|
||||
SimpleInteraction<T> inter);
|
||||
}
|
82
src/NadekoBot/_common/Interaction/NadekoInteraction.cs
Normal file
82
src/NadekoBot/_common/Interaction/NadekoInteraction.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
namespace NadekoBot;
|
||||
|
||||
public sealed class NadekoInteraction
|
||||
{
|
||||
private readonly ulong _authorId;
|
||||
private readonly ButtonBuilder _button;
|
||||
private readonly Func<SocketMessageComponent, Task> _onClick;
|
||||
private readonly bool _onlyAuthor;
|
||||
public DiscordSocketClient Client { get; }
|
||||
|
||||
private readonly TaskCompletionSource<bool> _interactionCompletedSource;
|
||||
|
||||
private IUserMessage message = null!;
|
||||
|
||||
public NadekoInteraction(DiscordSocketClient client,
|
||||
ulong authorId,
|
||||
ButtonBuilder button,
|
||||
Func<SocketMessageComponent, Task> onClick,
|
||||
bool onlyAuthor)
|
||||
{
|
||||
_authorId = authorId;
|
||||
_button = button;
|
||||
_onClick = onClick;
|
||||
_onlyAuthor = onlyAuthor;
|
||||
_interactionCompletedSource = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
Client = client;
|
||||
}
|
||||
|
||||
public async Task RunAsync(IUserMessage msg)
|
||||
{
|
||||
message = msg;
|
||||
|
||||
Client.InteractionCreated += OnInteraction;
|
||||
await Task.WhenAny(Task.Delay(15_000), _interactionCompletedSource.Task);
|
||||
Client.InteractionCreated -= OnInteraction;
|
||||
|
||||
await msg.ModifyAsync(m => m.Components = new ComponentBuilder().Build());
|
||||
}
|
||||
|
||||
private Task OnInteraction(SocketInteraction arg)
|
||||
{
|
||||
if (arg is not SocketMessageComponent smc)
|
||||
return Task.CompletedTask;
|
||||
|
||||
if (smc.Message.Id != message.Id)
|
||||
return Task.CompletedTask;
|
||||
|
||||
if (_onlyAuthor && smc.User.Id != _authorId)
|
||||
return Task.CompletedTask;
|
||||
|
||||
if (smc.Data.CustomId != _button.CustomId)
|
||||
return Task.CompletedTask;
|
||||
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
await ExecuteOnActionAsync(smc);
|
||||
|
||||
// this should only be a thing on single-response buttons
|
||||
_interactionCompletedSource.TrySetResult(true);
|
||||
|
||||
if (!smc.HasResponded)
|
||||
{
|
||||
await smc.DeferAsync();
|
||||
}
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
public MessageComponent CreateComponent()
|
||||
{
|
||||
var comp = new ComponentBuilder()
|
||||
.WithButton(_button);
|
||||
|
||||
return comp.Build();
|
||||
}
|
||||
|
||||
public Task ExecuteOnActionAsync(SocketMessageComponent smc)
|
||||
=> _onClick(smc);
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
namespace NadekoBot;
|
||||
|
||||
/// <summary>
|
||||
/// Represents essential interacation data
|
||||
/// </summary>
|
||||
/// <param name="Emote">Emote which will show on a button</param>
|
||||
/// <param name="CustomId">Custom interaction id</param>
|
||||
public record NadekoInteractionData(IEmote Emote, string CustomId, string? Text = null);
|
@@ -0,0 +1,20 @@
|
||||
namespace NadekoBot;
|
||||
|
||||
public class NadekoInteractionService : INadekoInteractionService, INService
|
||||
{
|
||||
private readonly DiscordSocketClient _client;
|
||||
|
||||
public NadekoInteractionService(DiscordSocketClient client)
|
||||
{
|
||||
_client = client;
|
||||
}
|
||||
|
||||
public NadekoInteraction Create<T>(
|
||||
ulong userId,
|
||||
SimpleInteraction<T> inter)
|
||||
=> new NadekoInteraction(_client,
|
||||
userId,
|
||||
inter.Button,
|
||||
inter.TriggerAsync,
|
||||
onlyAuthor: true);
|
||||
}
|
20
src/NadekoBot/_common/Interaction/SimpleInteraction.cs
Normal file
20
src/NadekoBot/_common/Interaction/SimpleInteraction.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace NadekoBot;
|
||||
|
||||
public class SimpleInteraction<T>
|
||||
{
|
||||
public ButtonBuilder Button { get; }
|
||||
private readonly Func<SocketMessageComponent, T, Task> _onClick;
|
||||
private readonly T? _state;
|
||||
|
||||
public SimpleInteraction(ButtonBuilder button, Func<SocketMessageComponent, T?, Task> onClick, T? state = default)
|
||||
{
|
||||
Button = button;
|
||||
_onClick = onClick;
|
||||
_state = state;
|
||||
}
|
||||
|
||||
public async Task TriggerAsync(SocketMessageComponent smc)
|
||||
{
|
||||
await _onClick(smc, _state!);
|
||||
}
|
||||
}
|
24
src/NadekoBot/_common/Medusa/IMedusaLoaderService.cs
Normal file
24
src/NadekoBot/_common/Medusa/IMedusaLoaderService.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace Nadeko.Common.Medusa;
|
||||
|
||||
public interface IMedusaLoaderService
|
||||
{
|
||||
Task<MedusaLoadResult> LoadMedusaAsync(string medusaName);
|
||||
Task<MedusaUnloadResult> UnloadMedusaAsync(string medusaName);
|
||||
string GetCommandDescription(string medusaName, string commandName, CultureInfo culture);
|
||||
string[] GetCommandExampleArgs(string medusaName, string commandName, CultureInfo culture);
|
||||
Task ReloadStrings();
|
||||
IReadOnlyCollection<string> GetAllMedusae();
|
||||
IReadOnlyCollection<MedusaStats> GetLoadedMedusae(CultureInfo? cultureInfo = null);
|
||||
}
|
||||
|
||||
public sealed record MedusaStats(string Name,
|
||||
string? Description,
|
||||
IReadOnlyCollection<SnekStats> Sneks);
|
||||
|
||||
public sealed record SnekStats(string Name,
|
||||
string? Prefix,
|
||||
IReadOnlyCollection<SnekCommandStats> Commands);
|
||||
|
||||
public sealed record SnekCommandStats(string Name);
|
10
src/NadekoBot/_common/Medusa/MedusaLoadResult.cs
Normal file
10
src/NadekoBot/_common/Medusa/MedusaLoadResult.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Nadeko.Common.Medusa;
|
||||
|
||||
public enum MedusaLoadResult
|
||||
{
|
||||
Success,
|
||||
NotFound,
|
||||
AlreadyLoaded,
|
||||
Empty,
|
||||
UnknownError,
|
||||
}
|
9
src/NadekoBot/_common/Medusa/MedusaUnloadResult.cs
Normal file
9
src/NadekoBot/_common/Medusa/MedusaUnloadResult.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Nadeko.Common.Medusa;
|
||||
|
||||
public enum MedusaUnloadResult
|
||||
{
|
||||
Success,
|
||||
NotLoaded,
|
||||
PossiblyUnable,
|
||||
NotFound,
|
||||
}
|
8
src/NadekoBot/_common/MessageType.cs
Normal file
8
src/NadekoBot/_common/MessageType.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace NadekoBot.Common;
|
||||
|
||||
public enum MsgType
|
||||
{
|
||||
Ok,
|
||||
Pending,
|
||||
Error
|
||||
}
|
6
src/NadekoBot/_common/ModuleBehaviors/IBehavior.cs
Normal file
6
src/NadekoBot/_common/ModuleBehaviors/IBehavior.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace NadekoBot.Common.ModuleBehaviors;
|
||||
|
||||
public interface IBehavior
|
||||
{
|
||||
public virtual string Name => this.GetType().Name;
|
||||
}
|
19
src/NadekoBot/_common/ModuleBehaviors/IExecNoCommand.cs
Normal file
19
src/NadekoBot/_common/ModuleBehaviors/IExecNoCommand.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace NadekoBot.Common.ModuleBehaviors;
|
||||
|
||||
/// <summary>
|
||||
/// Executed if no command was found for this message
|
||||
/// </summary>
|
||||
public interface IExecNoCommand : IBehavior
|
||||
{
|
||||
/// <summary>
|
||||
/// Executed at the end of the lifecycle if no command was found
|
||||
/// <see cref="IExecOnMessage"/> →
|
||||
/// <see cref="IInputTransformer"/> →
|
||||
/// <see cref="IExecPreCommand"/> →
|
||||
/// [<see cref="IExecPostCommand"/> | *<see cref="IExecNoCommand"/>*]
|
||||
/// </summary>
|
||||
/// <param name="guild"></param>
|
||||
/// <param name="msg"></param>
|
||||
/// <returns>A task representing completion</returns>
|
||||
Task ExecOnNoCommandAsync(IGuild guild, IUserMessage msg);
|
||||
}
|
21
src/NadekoBot/_common/ModuleBehaviors/IExecOnMessage.cs
Normal file
21
src/NadekoBot/_common/ModuleBehaviors/IExecOnMessage.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
namespace NadekoBot.Common.ModuleBehaviors;
|
||||
|
||||
/// <summary>
|
||||
/// Implemented by modules to handle non-bot messages received
|
||||
/// </summary>
|
||||
public interface IExecOnMessage : IBehavior
|
||||
{
|
||||
int Priority { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Ran after a non-bot message was received
|
||||
/// *<see cref="IExecOnMessage"/>* →
|
||||
/// <see cref="IInputTransformer"/> →
|
||||
/// <see cref="IExecPreCommand"/> →
|
||||
/// [<see cref="IExecPostCommand"/> | <see cref="IExecNoCommand"/>]
|
||||
/// </summary>
|
||||
/// <param name="guild">Guild where the message was sent</param>
|
||||
/// <param name="msg">The message that was received</param>
|
||||
/// <returns>Whether further processing of this message should be blocked</returns>
|
||||
Task<bool> ExecOnMessageAsync(IGuild guild, IUserMessage msg);
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user