3

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.

  • this is not a code factory, for that you can try [Hack hands](https://hackhands.com/) instead ... here, we help... shows what you tried and we will try to help form there... – balexandre Jan 16 '17 at 23:12

3 Answers3

14

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!

Hayden Meloche
  • 195
  • 3
  • 10
1

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.

WQYeo
  • 3,973
  • 2
  • 17
  • 26
0

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);


}

}
}
  • Please edit your answer to include your description of what the code does, don't just add a comment. – Doug E Fresh Sep 29 '17 at 19:37
  • I suggest using role ids instead of names, because the name could change and break your code, while the id is permanent. To get the role id, right click the role in discord, and select "Copy Id". Make sure developer mode is activated. – Sasino Jun 26 '20 at 23:31