Medusa System Added

Read about the medusa system [here](https://nadekobot.readthedocs.io/en/latest/medusa/creating-a-medusa/)
This commit is contained in:
Kwoth
2022-04-16 12:44:46 +00:00
parent 3a516ab32a
commit 7cb15f5278
103 changed files with 3363 additions and 203 deletions

View File

@@ -1,4 +1,5 @@
using Humanizer.Localisation;
using Nadeko.Medusa;
using System.Globalization;
using System.Net.Http.Headers;
using System.Text.Json;
@@ -12,7 +13,7 @@ public static class Extensions
new(@"^(https?|ftp)://(?<path>[^\s/$.?#].[^\s]*)$", RegexOptions.Compiled);
public static IEmbedBuilder WithAuthor(this IEmbedBuilder eb, IUser author)
=> eb.WithAuthor(author.ToString(), author.RealAvatarUrl().ToString());
=> eb.WithAuthor(author.ToString()!, author.RealAvatarUrl().ToString());
public static Task EditAsync(this IUserMessage msg, SmartText text)
=> text switch
@@ -71,17 +72,53 @@ public static class Extensions
public static string RealSummary(
this CommandInfo cmd,
IBotStrings strings,
ulong? guildId,
IMedusaLoaderService medusae,
CultureInfo culture,
string prefix)
=> string.Format(strings.GetCommandStrings(cmd.Summary, guildId).Desc, prefix);
{
string description;
if (cmd.Remarks?.StartsWith("medusa///") ?? false)
{
// command method name is kept in Summary
// medusa///<medusa-name-here> is kept in remarks
// this way I can find the name of the medusa, and then name of the command for which
// the description should be loaded
var medusaName = cmd.Remarks.Split("///")[1];
description = medusae.GetCommandDescription(medusaName, cmd.Summary, culture);
}
else
{
description = strings.GetCommandStrings(cmd.Summary, culture).Desc;
}
return string.Format(description, prefix);
}
public static string[] RealRemarksArr(
this CommandInfo cmd,
IBotStrings strings,
ulong? guildId,
IMedusaLoaderService medusae,
CultureInfo culture,
string prefix)
=> Array.ConvertAll(strings.GetCommandStrings(cmd.Summary, guildId).Args,
{
string[] args;
if (cmd.Remarks?.StartsWith("medusa///") ?? false)
{
// command method name is kept in Summary
// medusa///<medusa-name-here> is kept in remarks
// this way I can find the name of the medusa,
// and command for which data should be loaded
var medusaName = cmd.Remarks.Split("///")[1];
args = medusae.GetCommandExampleArgs(medusaName, cmd.Summary, culture);
}
else
{
args = strings.GetCommandStrings(cmd.Summary, culture).Args;
}
return Array.ConvertAll(args,
arg => GetFullUsage(cmd.Name, arg, prefix));
}
private static string GetFullUsage(string commandName, string args, string prefix)
=> $"{prefix}{commandName} {string.Format(args, prefix)}".TrimEnd();

View File

@@ -20,16 +20,20 @@ public static class ServiceCollectionExtensions
public static IServiceCollection AddConfigServices(this IServiceCollection services)
{
var baseType = typeof(ConfigServiceBase<>);
foreach (var type in Assembly.GetCallingAssembly().ExportedTypes.Where(x => x.IsSealed))
{
if (type.BaseType?.IsGenericType == true && type.BaseType.GetGenericTypeDefinition() == baseType)
{
services.AddSingleton(type);
services.AddSingleton(x => (IConfigService)x.GetRequiredService(type));
}
}
services.Scan(x => x.FromCallingAssembly()
.AddClasses(f => f.AssignableTo(typeof(ConfigServiceBase<>)))
.AsSelfWithInterfaces());
// var baseType = typeof(ConfigServiceBase<>);
//
// foreach (var type in Assembly.GetCallingAssembly().ExportedTypes.Where(x => x.IsSealed))
// {
// if (type.BaseType?.IsGenericType == true && type.BaseType.GetGenericTypeDefinition() == baseType)
// {
// services.AddSingleton(type);
// services.AddSingleton(x => (IConfigService)x.GetRequiredService(type));
// }
// }
return services;
}