【问题标题】:Discord.NET How can I give every person a role by one command?Discord.NET 如何通过一个命令给每个人一个角色?
【发布时间】:2020-04-21 07:03:14
【问题描述】:

我怎样才能通过一个命令给服务器上的每个人一个特定的角色?

【问题讨论】:

  • 我是 Discord.NET 的新手,所以我不知道如何正确编写这部分代码,我试图查找信息,但没有找到任何东西。
  • 我的错误,误读了标签,正在考虑另一个不和谐库。
  • 将问题分解成各个部分。了解如何遍历服务器的所有成员,并了解如何将角色添加到成员。然后将它们组合在一起。

标签: c# discord roles discord.net


【解决方案1】:

您可以使用下面的代码来完成。有多种方法可以改进命令的功能,所以不妨考虑一下。

[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();
}

【讨论】:

  • 正如我所说,我是 Discord.NET 的新手,所以我真的不知道在哪里可以使用 GetUsersAsync(),文档中也没有关于它的内容。
  • @Niko 我明白了。我已经编辑了我的消息并提供了解决方案。我希望这会有所帮助。
  • 是的,我试过var users = await Context.Guild.GetUsersAsync(); 并且在GetUsersAsync() 部分有错误,它说:'SocketGuiId' 不包含'GetUsersAsync' 的定义并且没有可访问的扩展可以找到接受“SocketGuiId”类型的第一个参数的方法“GetUsersAsync”
  • 是的,刚刚试过,现在可以正常使用了。非常感谢,你真的帮助了我,而不是像其他人那样给我的问题 -rep,祝你有美好的一天;)
  • 如果您有SocketGuild,只需执行guild.Users....无需将其转换为IGuild,只需使用GetUsersAsync即可。
猜你喜欢
  • 2021-08-13
  • 2021-10-28
  • 2020-09-26
  • 2021-10-19
  • 1970-01-01
  • 1970-01-01
  • 2022-11-20
  • 2018-03-18
  • 2020-12-04
相关资源
最近更新 更多