mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-12 10:18:27 -04:00
Medusa System Added
Read about the medusa system [here](https://nadekobot.readthedocs.io/en/latest/medusa/creating-a-medusa/)
This commit is contained in:
76
src/NadekoBot/Common/Medusa/Adapters/BehaviorAdapter.cs
Normal file
76
src/NadekoBot/Common/Medusa/Adapters/BehaviorAdapter.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
#nullable enable
|
||||
|
||||
[DontAddToIocContainer]
|
||||
public sealed class BehaviorAdapter : ICustomBehavior
|
||||
{
|
||||
private readonly WeakReference<Snek> _snekWr;
|
||||
private readonly IMedusaStrings _strings;
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly string _name;
|
||||
|
||||
// unused
|
||||
public int Priority
|
||||
=> 0;
|
||||
|
||||
public BehaviorAdapter(WeakReference<Snek> snekWr, IMedusaStrings strings, IServiceProvider services)
|
||||
{
|
||||
_snekWr = snekWr;
|
||||
_strings = strings;
|
||||
_services = services;
|
||||
|
||||
_name = snekWr.TryGetTarget(out var snek)
|
||||
? $"snek/{snek.GetType().Name}"
|
||||
: "unknown";
|
||||
}
|
||||
|
||||
public async Task<bool> ExecPreCommandAsync(ICommandContext context, string moduleName, CommandInfo command)
|
||||
{
|
||||
if (!_snekWr.TryGetTarget(out var snek))
|
||||
return false;
|
||||
|
||||
return await snek.ExecPreCommandAsync(ContextAdapterFactory.CreateNew(context, _strings, _services),
|
||||
moduleName,
|
||||
command.Name);
|
||||
}
|
||||
|
||||
public async Task<bool> ExecOnMessageAsync(IGuild? guild, IUserMessage msg)
|
||||
{
|
||||
if (!_snekWr.TryGetTarget(out var snek))
|
||||
return false;
|
||||
|
||||
return await snek.ExecOnMessageAsync(guild, msg);
|
||||
}
|
||||
|
||||
public async Task<string?> TransformInput(
|
||||
IGuild guild,
|
||||
IMessageChannel channel,
|
||||
IUser user,
|
||||
string input)
|
||||
{
|
||||
if (!_snekWr.TryGetTarget(out var snek))
|
||||
return null;
|
||||
|
||||
return await snek.ExecInputTransformAsync(guild, channel, user, input);
|
||||
}
|
||||
|
||||
public async Task ExecOnNoCommandAsync(IGuild? guild, IUserMessage msg)
|
||||
{
|
||||
if (!_snekWr.TryGetTarget(out var snek))
|
||||
return;
|
||||
|
||||
await snek.ExecOnNoCommandAsync(guild, msg);
|
||||
}
|
||||
|
||||
public async ValueTask ExecPostCommandAsync(ICommandContext context, string moduleName, string commandName)
|
||||
{
|
||||
if (!_snekWr.TryGetTarget(out var snek))
|
||||
return;
|
||||
|
||||
await snek.ExecPostCommandAsync(ContextAdapterFactory.CreateNew(context, _strings, _services),
|
||||
moduleName,
|
||||
commandName);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
=> _name;
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
internal class ContextAdapterFactory
|
||||
{
|
||||
public static AnyContext CreateNew(ICommandContext context, IMedusaStrings strings, IServiceProvider services)
|
||||
=> context.Guild is null
|
||||
? new DmContextAdapter(context, strings, services)
|
||||
: new GuildContextAdapter(context, strings, services);
|
||||
}
|
48
src/NadekoBot/Common/Medusa/Adapters/DmContextAdapter.cs
Normal file
48
src/NadekoBot/Common/Medusa/Adapters/DmContextAdapter.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
public sealed class DmContextAdapter : DmContext
|
||||
{
|
||||
public override IMedusaStrings Strings { get; }
|
||||
public override IDMChannel Channel { get; }
|
||||
public override IUserMessage Message { get; }
|
||||
public override IUser User
|
||||
=> Message.Author;
|
||||
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly Lazy<IEmbedBuilderService> _ebs;
|
||||
private readonly Lazy<IBotStrings> _botStrings;
|
||||
private readonly Lazy<ILocalization> _localization;
|
||||
|
||||
public DmContextAdapter(ICommandContext ctx, IMedusaStrings strings, IServiceProvider services)
|
||||
{
|
||||
if (ctx is not { Channel: IDMChannel ch })
|
||||
{
|
||||
throw new ArgumentException("Can't use non-dm context to create DmContextAdapter", nameof(ctx));
|
||||
}
|
||||
|
||||
Strings = strings;
|
||||
|
||||
_services = services;
|
||||
|
||||
Channel = ch;
|
||||
Message = ctx.Message;
|
||||
|
||||
|
||||
_ebs = new(_services.GetRequiredService<IEmbedBuilderService>());
|
||||
_botStrings = new(_services.GetRequiredService<IBotStrings>);
|
||||
_localization = new(_services.GetRequiredService<ILocalization>());
|
||||
}
|
||||
|
||||
public override IEmbedBuilder Embed()
|
||||
=> _ebs.Value.Create();
|
||||
|
||||
public override string GetText(string key, object[]? args = null)
|
||||
{
|
||||
var cultureInfo = _localization.Value.GetCultureInfo(default(ulong?));
|
||||
var output = Strings.GetText(key, cultureInfo, args ?? Array.Empty<object>());
|
||||
if (!string.IsNullOrWhiteSpace(output))
|
||||
return output;
|
||||
|
||||
return _botStrings.Value.GetText(key, cultureInfo, args);
|
||||
}
|
||||
}
|
51
src/NadekoBot/Common/Medusa/Adapters/GuildContextAdapter.cs
Normal file
51
src/NadekoBot/Common/Medusa/Adapters/GuildContextAdapter.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
public sealed class GuildContextAdapter : GuildContext
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ICommandContext _ctx;
|
||||
private readonly Lazy<IEmbedBuilderService> _ebs;
|
||||
private readonly Lazy<IBotStrings> _botStrings;
|
||||
private readonly Lazy<ILocalization> _localization;
|
||||
|
||||
public override IMedusaStrings Strings { get; }
|
||||
public override IGuild Guild { get; }
|
||||
public override ITextChannel Channel { get; }
|
||||
public override IUserMessage Message
|
||||
=> _ctx.Message;
|
||||
|
||||
public override IGuildUser User { get; }
|
||||
|
||||
public override IEmbedBuilder Embed()
|
||||
=> _ebs.Value.Create();
|
||||
|
||||
public GuildContextAdapter(ICommandContext ctx, IMedusaStrings strings, IServiceProvider services)
|
||||
{
|
||||
if (ctx.Guild is not IGuild guild || ctx.Channel is not ITextChannel channel)
|
||||
{
|
||||
throw new ArgumentException("Can't use non-guild context to create GuildContextAdapter", nameof(ctx));
|
||||
}
|
||||
|
||||
Strings = strings;
|
||||
User = (IGuildUser)ctx.User;
|
||||
|
||||
_services = services;
|
||||
_ebs = new(_services.GetRequiredService<IEmbedBuilderService>());
|
||||
_botStrings = new(_services.GetRequiredService<IBotStrings>);
|
||||
_localization = new(_services.GetRequiredService<ILocalization>());
|
||||
|
||||
(_ctx, Guild, Channel) = (ctx, guild, channel);
|
||||
}
|
||||
|
||||
public override string GetText(string key, object[]? args = null)
|
||||
{
|
||||
args ??= Array.Empty<object>();
|
||||
|
||||
var cultureInfo = _localization.Value.GetCultureInfo(_ctx.Guild.Id);
|
||||
var output = Strings.GetText(key, cultureInfo, args);
|
||||
if (!string.IsNullOrWhiteSpace(output))
|
||||
return output;
|
||||
|
||||
return _botStrings.Value.GetText(key, cultureInfo, args);
|
||||
}
|
||||
}
|
32
src/NadekoBot/Common/Medusa/Adapters/ParamParserAdapter.cs
Normal file
32
src/NadekoBot/Common/Medusa/Adapters/ParamParserAdapter.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
public sealed class ParamParserAdapter<T> : TypeReader
|
||||
{
|
||||
private readonly ParamParser<T> _parser;
|
||||
private readonly IMedusaStrings _strings;
|
||||
private readonly IServiceProvider _services;
|
||||
|
||||
public ParamParserAdapter(ParamParser<T> parser,
|
||||
IMedusaStrings strings,
|
||||
IServiceProvider services)
|
||||
{
|
||||
_parser = parser;
|
||||
_strings = strings;
|
||||
_services = services;
|
||||
}
|
||||
|
||||
public override async Task<Discord.Commands.TypeReaderResult> ReadAsync(
|
||||
ICommandContext context,
|
||||
string input,
|
||||
IServiceProvider services)
|
||||
{
|
||||
var medusaContext = ContextAdapterFactory.CreateNew(context,
|
||||
_strings,
|
||||
_services);
|
||||
|
||||
var result = await _parser.TryParseAsync(medusaContext, input);
|
||||
|
||||
if(result.IsSuccess)
|
||||
return Discord.Commands.TypeReaderResult.FromSuccess(result.Data);
|
||||
|
||||
return Discord.Commands.TypeReaderResult.FromError(CommandError.Unsuccessful, "Invalid input");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user