Discord.NET如何通过一个命令赋予每个人一个角色?

时间:2020-01-02 16:01:00

标签: c# discord roles discord.net

如何仅通过一个命令就可以赋予服务器上的每个人特定的角色?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码进行操作。有多种改善命令功能的方法,因此请考虑一下。

[Command("addrole all"), Summary("Adds the Star role to all users")]
public async Task AddRoleStarCommand()
{
    // Gets all the users of the guild. Note that this may not completely
    // work for extremely large guilds (thousands of users).
    var users = await Context.Guild.GetUsersAsync();

    // Gets the role "Star".
    var role = Context.Guild.Roles.FirstOrDefault(x => x.Name == "Star");

    // Adds the role "Star" to each user in the guild.
    foreach (IGuildUser user in users)
    { 
        await user.AddRoleAsync(role);
    }
}

请记住,要使用GetUsersAsync(),您需要一个IGuild,而不是SocketGuild

public async Task SocketGuildDemoCommand()
{
    // Don't do this.
    // Does not exist, error returned.
    SocketGuild guild = Context.Guild;
    var users = await guild.GetUsersAsync();
}

public async Task IGuildDemoCommand()
{
    // Do this.
    // Exists, should work perfectly.
    IGuild guild = Context.Guild;
    var users = await guild.GetUsersAsync();
}