如何使用我的机器人为新成员自动分配角色?

How can I auto-assign a role to a new member with my bot?

如题所示。机器人有没有办法检测用户何时加入公会并自动授予该用户特定角色?我想自动授予每个用户 "member" 角色,我该如何实现?我对 C# 一点经验都没有。

我试过了,没有成功:

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

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

想做的是挂钩类似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);
    }
}