Reputation: 39
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.
Upvotes: 3
Views: 19069
Reputation: 195
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!
Upvotes: 14
Reputation: 23
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);
}
}
}
Upvotes: 0
Reputation: 4071
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.
Upvotes: 1