如何为新成员不和添加角色

时间:2018-05-30 14:06:23

标签: c# discord

我尝试了不同的东西,但他们从不工作。我尝试将代码添加到我的新用户加入任务

public async Task AnnounceJoinedUser(SocketGuildUser user) //welcomes New Players
{
    var channel = Client.GetChannel(447147292617736203) as SocketTextChannel; //gets channel to send message in
    await channel.SendMessageAsync("Welcome " + user.Mention + " to the server! Have a great time"); //Welcomes the new user
}

但我不知道如何为新用户添加角色

1 个答案:

答案 0 :(得分:1)

首先从用户加入的公会获取角色,然后使用user.AddRoleAsync()将角色添加给用户。

private async Task UserJoined(SocketGuildUser socketGuildUser) {
    ulong roleID = 123; //Some hard-coded roleID
    var role = socketGuildUser.Guild.GetRole(roleID);
    await socketGuildUser.AddRoleAsync(role);

    //Without ID...
    string roleName = "The role name to add to user"; //Or some other property
    //Get the list of roles in the guild.
    var guildRoles = socketGuildUser.Guild.Roles;
    //Loop through the list of roles in the guild.
    foreach(var guildRole in guildRoles) {
        //If the current iteration of role matches the rolename
        if(guildRole.Name.Equals(roleName, StringComparison.OrdinalIgnoreCase)) {
            //Assign the role to the user.
            await socketGuildUser.AddRoleAsync(guildRole);
            //Exit Loop.
            break;
        }
    }
}
相关问题