discord.net中的[Summary(“”)]是什么?

时间:2018-10-17 07:59:06

标签: c# discord discord.net

我想在discord.net中进行帮助,而我想知道[Summary("")]的作用。是否可以获取命令摘要? discord.net中的摘要是否像description:中的discord.js-commando一样?我已经在Google上搜索过,但找不到任何结果。

如果我想执行帮助命令,我将如何获得摘要?

2 个答案:

答案 0 :(得分:0)

Summary属性用于提供有关类,方法或方法参数的一些信息。这实质上是一种用于记录代码的“元数据”形式,并允许您创建一个“帮助”命令,例如,该命令会自动收集命令组中的所有Summary属性,命令本身以及命令方法的输入参数。 这将自动更新“帮助”命令,因此您无需手动执行。

一些基本的代码来展示用法:

[Command("Help")]
public async Task Help()
{
    List<CommandInfo> commands = _commandService.Commands.ToList();
    EmbedBuilder embedBuilder = new EmbedBuilder();

    foreach (CommandInfo command in commands)
    {
        // Get the command Summary attribute information
        string embedFieldText = command.Summary ?? "No description available\n";

        embedBuilder.AddField(command.Name, embedFieldText);
    }

    await ReplyAsync("Here's a list of commands and their description: ", false, embedBuilder.Build());
}

答案 1 :(得分:0)

Summary是discord.net提供的属性。从技术上讲,它没有提供特定的功能,但是,如果您有兴趣创建帮助命令或为命令和/或参数提供其他元数据,则可以通过CommandService访问它们。

可以通过CommandInfo,ModuleInfo或ParameterInfo访问它们。意味着您可以使用bot模块,命令或参数上的属性,如下所示:

[Summary("The Game Module")]
public class ManualGameManagement : ModuleBase
{
    [Command("Win", RunMode = RunMode.Sync)]
    [Summary("Increments a user's win counter")]
    public async Task WinAsync([Summary("The user")]params SocketGuildUser[] users)
    {
      //Do stuff...
    }
}

如果您查看CommandService,可以执行类似操作,例如使用DependencyInjection使用属性或构造函数将其注入模块,然后使用它来访问CommandService#CommandsCommandService#Modules属性,并检索摘要。 这也适用于Remarks属性,该属性与Summary类似,而Alias则接受多个字符串,并用于具有多个名称来调用命令