From 577d62a1c1019043b0be059acccc7eda7c8acd50 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Tue, 7 May 2024 22:55:56 +0000 Subject: [PATCH] dev: Renamed some classes, making the new string model work --- src/NadekoBot/_common/Attributes/CmdAttribute.cs | 5 ++++- src/NadekoBot/_common/CommandNameLoadHelper.cs | 9 +++++++++ src/NadekoBot/_common/Sender/ResponseBuilder.cs | 6 ++++++ .../_common/TypeReaders/ModuleTypeReader.cs | 14 ++++++++------ src/NadekoBot/_common/Yml/Yaml.cs | 4 +++- src/NadekoBot/_common/_Extensions/Extensions.cs | 2 +- 6 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/NadekoBot/_common/Attributes/CmdAttribute.cs b/src/NadekoBot/_common/Attributes/CmdAttribute.cs index 39162ecc4..79e4209ce 100644 --- a/src/NadekoBot/_common/Attributes/CmdAttribute.cs +++ b/src/NadekoBot/_common/Attributes/CmdAttribute.cs @@ -15,4 +15,7 @@ public sealed class CmdAttribute : CommandAttribute Remarks = memberName.ToLowerInvariant(); Summary = memberName.ToLowerInvariant(); } -} \ No newline at end of file +} + +[AttributeUsage(AttributeTargets.Method)] +public sealed class PromptableAttribute: Attribute; \ No newline at end of file diff --git a/src/NadekoBot/_common/CommandNameLoadHelper.cs b/src/NadekoBot/_common/CommandNameLoadHelper.cs index d115fd331..635f74c39 100644 --- a/src/NadekoBot/_common/CommandNameLoadHelper.cs +++ b/src/NadekoBot/_common/CommandNameLoadHelper.cs @@ -1,3 +1,4 @@ +using NadekoBot.Common.Yml; using YamlDotNet.Serialization; namespace NadekoBot.Common.Attributes; @@ -15,6 +16,14 @@ public static class CommandNameLoadHelper return _deserializer.Deserialize>(text); } + public static Dictionary LoadCommandStrings( + string commandsFilePath = "data/strings/commands.yml") + { + var text = File.ReadAllText(commandsFilePath); + + return Yaml.Deserializer.Deserialize>(text); + } + public static string[] GetAliasesFor(string methodName) => _lazyCommandAliases.Value.TryGetValue(methodName.ToLowerInvariant(), out var aliases) && aliases.Length > 1 ? aliases.ToArray() diff --git a/src/NadekoBot/_common/Sender/ResponseBuilder.cs b/src/NadekoBot/_common/Sender/ResponseBuilder.cs index 598c2b2b3..7ab65c615 100644 --- a/src/NadekoBot/_common/Sender/ResponseBuilder.cs +++ b/src/NadekoBot/_common/Sender/ResponseBuilder.cs @@ -179,9 +179,15 @@ public sealed partial class ResponseBuilder if (text is SmartPlainText spt) plainText = spt.Text; else if (text is SmartEmbedText set) + { + plainText = set.PlainText ?? plainText; embedBuilder = set.GetEmbed(); + } else if (text is SmartEmbedTextArray ser) + { + plainText = ser.Content ?? plainText; embeds = ser.GetEmbedBuilders(); + } return this; } diff --git a/src/NadekoBot/_common/TypeReaders/ModuleTypeReader.cs b/src/NadekoBot/_common/TypeReaders/ModuleTypeReader.cs index e1620772d..33908dda0 100644 --- a/src/NadekoBot/_common/TypeReaders/ModuleTypeReader.cs +++ b/src/NadekoBot/_common/TypeReaders/ModuleTypeReader.cs @@ -21,30 +21,32 @@ public sealed class ModuleTypeReader : NadekoTypeReader } } -public sealed class ModuleOrCrTypeReader : NadekoTypeReader +public sealed class ModuleOrExprTypeReader : NadekoTypeReader { private readonly CommandService _cmds; - public ModuleOrCrTypeReader(CommandService cmds) + public ModuleOrExprTypeReader(CommandService cmds) => _cmds = cmds; - public override ValueTask> ReadAsync(ICommandContext context, string input) + public override ValueTask> ReadAsync(ICommandContext context, string input) { input = input.ToUpperInvariant(); var module = _cmds.Modules.GroupBy(m => m.GetTopLevelModule()) .FirstOrDefault(m => m.Key.Name.ToUpperInvariant() == input) ?.Key; if (module is null && input != "ACTUALEXPRESSIONS") - return new(TypeReaderResult.FromError(CommandError.ParseFailed, "No such module found.")); + return new(TypeReaderResult.FromError(CommandError.ParseFailed, "No such module found.")); - return new(TypeReaderResult.FromSuccess(new ModuleOrCrInfo + return new(TypeReaderResult.FromSuccess(new ModuleOrExpr { Name = input })); } } -public sealed class ModuleOrCrInfo +// todo chagne commands.en-us to have the new type name + +public sealed class ModuleOrExpr { public string Name { get; set; } } \ No newline at end of file diff --git a/src/NadekoBot/_common/Yml/Yaml.cs b/src/NadekoBot/_common/Yml/Yaml.cs index 25fdc9a55..07daba0fe 100644 --- a/src/NadekoBot/_common/Yml/Yaml.cs +++ b/src/NadekoBot/_common/Yml/Yaml.cs @@ -7,7 +7,9 @@ namespace NadekoBot.Common.Yml; public class Yaml { public static ISerializer Serializer - => new SerializerBuilder().WithTypeInspector(inner => new CommentGatheringTypeInspector(inner)) + => new SerializerBuilder() + .WithTypeInspector(inner => new CommentGatheringTypeInspector(inner)) + .DisableAliases() .WithEmissionPhaseObjectGraphVisitor(args => new CommentsObjectGraphVisitor(args.InnerVisitor)) .WithEventEmitter(args => new MultilineScalarFlowStyleEmitter(args)) diff --git a/src/NadekoBot/_common/_Extensions/Extensions.cs b/src/NadekoBot/_common/_Extensions/Extensions.cs index 19c965e35..ed18aa1e6 100644 --- a/src/NadekoBot/_common/_Extensions/Extensions.cs +++ b/src/NadekoBot/_common/_Extensions/Extensions.cs @@ -154,7 +154,7 @@ public static class Extensions } else { - args = strings.GetCommandStrings(cmd.Summary, culture).Args; + args = strings.GetCommandStrings(cmd.Summary, culture).Examples; } return args.Map(arg => GetFullUsage(cmd.Aliases.First(), arg, prefix));