我想为我的服务器创建一个机器人,当用户键入“!setlol(其名称)”时,它将扮演一个角色并将其分配给他们。该角色会说“ LoL :(他们的名字)”。我一直在寻找有关如何执行此操作的文档,但是每个文档都涉及如何分配已经存在的角色。
我已经尝试过自己解决它,但是我似乎做不到。
var user = Context.User;
var roles = Context.Guild.Roles;
var role = Context.Guild.CreateRoleAsync($"LoL: {name}");
var exRole = roles.FirstOrDefault(x => x.Guild.Name == $"{name}");
if (user == null)
{
await Context.Channel.SendMessageAsync("User is null u dumb fuck");
}
else
{
await Context.Channel.SendMessageAsync("What u worrying about twat?!");
}
if (user != null && exRole != null)
{
await Context.Channel.SendMessageAsync("Work please");
await (user as IGuildUser).AddRoleAsync(exRole);
}
else
{
await Context.Channel.SendMessageAsync("User `enter code here`or role is null");
}
await Context.Channel.SendMessageAsync($"Role {name} was added to your list of roles!");
与错误不符的图片:https://i.stack.imgur.com/ZkvK4.png
答案 0 :(得分:1)
Discord.Net Documentation - CreateRoleAsync
//Context assumes that you are in a command module
//user assumes your command accepts an IGuildUser as a parameter
var role = await Context.Guild.CrateRoleAsync($"LoL: {user.UserName}");
await user.AddRoleAsync(role);