如何仅通过一个命令就可以赋予服务器上的每个人特定的角色?
答案 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();
}