Discord.net打印所有用户角色

时间:2020-06-08 10:46:10

标签: c# discord discord.net

因此,我一直在研究Discord机器人,并尝试打印用户具有的每个角色。我不断收到“ Discord.CollectionWrapper`1 [Discord.WebSocket.SocketRole]”错误,似乎无法修复。有没有办法让我显示所有用户角色?

        {
            if (!Context.Message.MentionedUsers.Any())
            {
                await Context.Channel.SendMessageAsync("Can't give you any information if you don't specify a member");
            }

            var user = Context.Message.MentionedUsers.First();
            SocketGuildUser U = (SocketGuildUser)Context.Message.MentionedUsers.First();
            var GuildUser = Context.Guild.GetUser(Context.User.Id);
            var typingChannel = Context.Channel;
            await typingChannel.TriggerTypingAsync();




            EmbedBuilder eb = new EmbedBuilder()
            {
                Title = "Hello world!",
                Description = "I am a description set by initializer.",
            };
            eb.AddField("ID:", $"{user.Id}")
                .WithAuthor(Context.Client.CurrentUser)
                .WithColor(Color.DarkPurple)
                .WithTitle($"***{user.Username}***")
                .WithDescription($"{user.Username}#{user.Discriminator} \n status: {user.Status}\n Account created at date: {user.CreatedAt}\nJoined at: {U.JoinedAt}" +
                $"\nRoles: {U.Roles.ToString()}\n Permissions: {U.GuildPermissions}\n")
                .WithCurrentTimestamp()
                .Build();

            await Context.Channel.SendMessageAsync("", false, eb.Build());
        }```

1 个答案:

答案 0 :(得分:1)

“ Discord.CollectionWrapper`1 [Discord.WebSocket.SocketRole]”

这不是错误-这是默认ToString()实现的输出。实际上很少有类会覆盖Object.ToString()

使用Linq的.SelectString.Join来获取每个值:

String.Join( separator: ", ", values: U.Roles.Select( r => r.ToString() ) )

像这样:

$"\nRoles: { String.Join( separator: ", ", values: U.Roles.Select( r => r.ToString() ) ) }"