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

justarandomdiscordguy picture justarandomdiscordguy · Jan 16, 2017 · Viewed 14.6k times · Source

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.

Answer

Hayden Meloche picture Hayden Meloche · Aug 29, 2017

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!