Discord.net如何提及角色

Discord.net how to mention roles

如何使用 Discord.net 库在 C# 中提及公会角色?

我找到了一种方法:

MentionUtils.MentionRole(id)

也可以使用角色的原始提及,实际上是角色对象中 .Mention 属性 的内容。

格式如下:

<@&ROLE_ID>

它不同于通过 & 字符提及单个用户,后者指定它提及的是角色,而不是用户。

您可以从 .ID 属性 中获取角色 ID,如果您想对其进行硬编码,也可以通过右键单击角色列表中的角色来获取。

通过名称提及角色的示例命令:

        [Command("mention")]
        public async Task MentionRole(string name)
        {
            SocketRole role = Context.Guild.Roles.First(x => x.Name == name); //Get the first role that matches the name provided

            await ReplyAsync($"<@&{role.Id}>"); //Format the string that mentions the role (ex. <@&739515238742753371>)
            await ReplyAsync(role.Mention); //Optionally, you can use the integrated property that returns the exact same string as we formatted above

            //The mention is instantly sent as a reply to the command
        }

你也可以 iRole

[Command("roleinfo")]
    public async Task mentionRoleAsync(IRole role)
    {

        var embed = new EmbedBuilder();
        embed.WithDescription("Role ID : " + role.Id + Environment.NewLine + "Role Name : " + role.Name + Environment.NewLine + "Role Mention : " + role.Mention + Environment.NewLine + "Role Mention : " + role.Mention + Environment.NewLine + "Role Color : " + role.Color.ToString() + Environment.NewLine + "Role Created at : " + role.CreatedAt);
        await Context.Channel.SendMessageAsync("", false, embed);

    }

这是一个例子,您必须使用 IRole,如果您想提及它,只需发送消息:<&ROLEID>

[Command("role")]
    public async Task mentionRoleAsync(IRole role)
    {

        var embed = new EmbedBuilder()
        .WithTitle("Role Infos")
        .WithDescription($"Role ID{role.Id} \n Name: {role.Name}");


    }