Skip to content

Checking Permissions with DiscordSharp

Mike Santiago edited this page Dec 29, 2015 · 1 revision

Preface

Recent versions of DiscordSharp have basic permission support, allowing you to check permissions and what not.

It's a pain

As of right now, it's a bit of a pain to do. Seeing as how users can have multiple permissions and what not (and I wrapped the HasPermission method in the actual permissions object itself...), you must iterate through the roles the user has and perform a HasPermission check on the permission you want to check. For example, if I want my bot to attempt editing the guild's icon, I would do this:

//somewhere inside the client.MessageReceived event
DiscordServer current = e.Channel.parent;
DiscordMember me = current.members.Find(x=>x.user.id == client.Me.user.id);
foreach(var role in me.roles)
{
   if(role.permissions.HasPermission(DiscordSpecialPermissions.ManageServer)
   {
      //do ya thing
   }
}

For a full list of the permissions DiscordSharp supports, check out the DiscordPermission class.