【问题标题】:How can I auto-assign a role to a new member with my bot?如何使用我的机器人将角色自动分配给新成员?
【发布时间】:2019-07-03 20:07:10
【问题描述】:

标题暗示了这一点。机器人有什么方法可以检测用户何时加入公会并自动授予该用户特定角色?我想自动授予每个用户“成员”角色,我该如何实现?我对 C# 完全没有经验。

我试过了,没有成功:

 public async Task auto_role(SocketGuildUser user)
    {
        await user.AddRoleAsync((Context.Guild.Roles.FirstOrDefault(x => x.Name == "Member")));
    }

【问题讨论】:

  • 您是否授予机器人为新成员设置角色的正确权限?

标签: c# bots discord discord.net


【解决方案1】:

如果您想为任何新加入的公会成员添加角色,则根本不应该接触命令系统,因为它不是命令!

要做想要做的是挂钩类似UserJoined 事件,每当新用户加入公会时就会触发该事件。

因此,例如,您可能想要执行以下操作:

public class MemberAssignmentService
{
    private readonly ulong _roleId;
    public MemberAssignmentService(DiscordSocketClient client, ulong roleId)
    {
        // Hook the evnet
        client.UserJoined += AssignMemberAsync;

        // Note that we are using role identifier here instead
        // of name like your original solution; this is because
        // a role name check could easily be circumvented by a new role
        // with the exact name.
        _roleId = roleId;
    }

    private async Task AssignMemberAsync(SocketGuildUser guildUser)
    {
        var guild = guildUser.Guild;
        // Check if the desired role exist within this guild.
        // If not, we simply bail out of the handler.
        var role = guild.GetRole(_roleId);
        if (role == null) return;
        // Check if the bot user has sufficient permission
        if (!guild.CurrentUser.GuildPermissions.Has(GuildPermissions.ManageRoles)) return;

        // Finally, we call AddRoleAsync
        await guildUser.AddRoleAsync(role);
    }
}

【讨论】:

    猜你喜欢
    • 2021-05-10
    • 1970-01-01
    • 2021-03-14
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 2021-07-13
    • 2021-11-18
    相关资源
    最近更新 更多