简体   繁体   中英

Discord.NET How do I grant an user a role?

So I've been coding my discord bot and wanted to add a function to give an user a specific role like you are a player in a game and you type in a "cheat code" it grants you the cheater role. How can I do this? Thanks for helping.

For anybody finding this in the future here is how I give a user a role:

var user = Context.User;
var role = Context.Guild.Roles.FirstOrDefault(x => x.Name == "RoleName");
await (user as IGuildUser).AddRoleAsync(role);

Hope this helps!

Please study the library here... : https://github.com/RogueException/Discord.Net

Back to the question , You can just do this await e.User.AddRoles(x); in the async operator command. x will be the role variable.

this should do the trick this is a command that gives the user a role that cant talk in any channels just one channel called jail

using System;
using System.Collections.Generic;
using System.Text;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Discord.Net;
using System.Linq;
using System.Threading.Tasks;

namespace ConsoleApp1.commands
{
public class jail : ModuleBase<SocketCommandContext>
{
[Command("jail")]
public async Task jail2 (IGuildUser user)
{

var role = Context.Guild.Roles.FirstOrDefault(x => x.Name.ToString() == "ROLE_NAME");     


await (user as IGuildUser).AddRoleAsync(role);


}

}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM