简体   繁体   中英

How do I configure Discord role permissions using Discord.NET?

So I'm trying to code a Discord bot using Discord.NET API in C# and I came across a problem with configuring permissions for roles using the bot.

I'm trying to make a Text-Channel mute command by creating a " Muted " role. However, I cannot deny the permission for Send Messages . Here is what I have tried:

// Mute Command:
[Command("mute")]
[RequireUserPermission(GuildPermission.KickMembers)]
[RequireBotPermission(GuildPermission.KickMembers)]
public async Task Mute(IGuildUser user, int duration, string reason)
{
    var role = await Context.Guild.CreateRoleAsync("Muted");
    role.Permissions.SendMessages = false;
    Threading.Sleep(duration);
    await role.DeleteAsync();
}

Which does not work.

I have also tried replacing

role.Permissions.SendMessages = false;

with

role.Permissions.SendMessages.Equal(false);

which didn't work either. I've looked at the API documentation and still have not found a solution.

Any solutions to this?

The Permissions property in IRole only has a getter, meaning that you cannot set the Permissions objects or any of the properties within the Permissions object once it's been created. You may only get data from the object. You can see from the image below:

IRole definition

The CreateRoleAsync function has a GuildPermissions parameter (which is the 2nd parameter) which you can pass a custom GuildPermissions object into. When making a new GuildPermissions instance, you will need to use the constructor that takes in around 20 parameters which all set a different permission.

Here is some example code:

public void CreateRoleWithCustomPermissions()
{
   var myCustomPermissions = new GuildPermissions(false,
                                                false,
                                                false,
                                                false,
                                                false,
                                                false,
                                                false,
                                                false,
                                                false, // this one is for sendMessages
                                                false,
                                                false,
                                                false,
                                                false,
                                                false,
                                                true,
                                                false,
                                                false,
                                                true,
                                                false,
                                                false,
                                                false,
                                                false,
                                                false,
                                                false,
                                                false,
                                                false,
                                                false,
                                                false,
                                                false);

   var role = await Context.Guild.CreateRoleAsync("Muted", myCustomPermissions);
}

Here is a shorter version:

foreach (var channel in Context.Guild.Channels)
{
    await channel.AddPermissionOverwriteAsync(role, OverwritePermissions.DenyAll(channel).Modify(viewChannel: PermValue.Allow, readMessageHistory: PermValue.Allow ....and so on));
}

Here the longer version:

bool rExist=false;
ulong roleID=0;
   //Check if the role exist
foreach(var gRole in Context.Guild.Roles)
{
 if(gRole.Name.Equals("role name here"))
 {
   rExist=true;
   roleID=gRole.Id;
   break;
 }
}
if(!rExist)
{
  //if the roles doesnt exist u create it and set the perms of the channels
   var mRole = await Context.Guild.CreateRoleAsync(
    "MuteRole", Discord.GuildPermissions.None, 
     Discord.Color.DarkTeal/*what ever color*/,false,null
     );
   try
   {
     await user.AddRoleAsync(mRole);
     foreach (var channel in Context.Guild.Channels)
     {
        await channel.AddPermissionOverwriteAsync(mRole,
        OverwritePermissions.DenyAll(channel).Modify(
        viewChannel:PermValue.Allow, readMessageHistory: PermValue.Allow)
        );
     }
  }
  catch (Exception e)
  {
     //handel error if occures
  }
}else
{
  //if it exist just add it to the user
  var role= Context.Guild.GetRole(roleID);
  await user.AddRoleAsync(role);
  //Check if the role is added to every channel. Same as above
}

NOTE: It will add the role to EVERY channel in the server.

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