github.com/diamondburned/arikawa/v2@v2.1.0/discord/permission.go (about)

     1  package discord
     2  
     3  type Permissions uint64
     4  
     5  var (
     6  	// Allows creation of instant invites
     7  	PermissionCreateInstantInvite Permissions = 1 << 0
     8  	// Allows kicking members
     9  	PermissionKickMembers Permissions = 1 << 1
    10  	// Allows banning members
    11  	PermissionBanMembers Permissions = 1 << 2
    12  	// Allows all permissions and bypasses channel permission overwrites
    13  	PermissionAdministrator Permissions = 1 << 3
    14  	// Allows management and editing of channels
    15  	PermissionManageChannels Permissions = 1 << 4
    16  	// Allows management and editing of the guild
    17  	PermissionManageGuild Permissions = 1 << 5
    18  	// Allows for the addition of reactions to messages
    19  	PermissionAddReactions Permissions = 1 << 6
    20  	// Allows for viewing of audit logs
    21  	PermissionViewAuditLog Permissions = 1 << 7
    22  	// Allows for using priority speaker in a voice channel
    23  	PermissionPrioritySpeaker Permissions = 1 << 8
    24  	// Allows the user to go live
    25  	PermissionStream Permissions = 1 << 9
    26  	// Allows guild members to view a channel, which includes reading messages
    27  	// in text channels
    28  	PermissionViewChannel Permissions = 1 << 10
    29  	// Allows for sending messages in a channel
    30  	PermissionSendMessages Permissions = 1 << 11
    31  	// Allows for sending of /tts messages
    32  	PermissionSendTTSMessages Permissions = 1 << 12
    33  	// Allows for deletion of other users messages
    34  	PermissionManageMessages Permissions = 1 << 13
    35  	// Links sent by users with this permission will be auto-embedded
    36  	PermissionEmbedLinks Permissions = 1 << 14
    37  	// Allows for uploading images and files
    38  	PermissionAttachFiles Permissions = 1 << 15
    39  	// Allows for reading of message history
    40  	PermissionReadMessageHistory Permissions = 1 << 16
    41  	// Allows for using the @everyone tag to notify all users in a channel,
    42  	// and the @here tag to notify all online users in a channel
    43  	PermissionMentionEveryone Permissions = 1 << 17
    44  	// Allows the usage of custom emojis from other servers
    45  	PermissionUseExternalEmojis Permissions = 1 << 18
    46  
    47  	// ?
    48  
    49  	// Allows for joining of a voice channel
    50  	PermissionConnect Permissions = 1 << 20
    51  	// Allows for speaking in a voice channel
    52  	PermissionSpeak Permissions = 1 << 21
    53  	// Allows for muting members in a voice channel
    54  	PermissionMuteMembers Permissions = 1 << 22
    55  	// Allows for deafening of members in a voice channel
    56  	PermissionDeafenMembers Permissions = 1 << 23
    57  	// Allows for moving of members between voice channels
    58  	PermissionMoveMembers Permissions = 1 << 24
    59  	// Allows for using voice-activity-detection in a voice channel
    60  	PermissionUseVAD Permissions = 1 << 25
    61  	// Allows for modification of own nickname
    62  	PermissionChangeNickname Permissions = 1 << 26
    63  	// Allows for modification of other users nicknames
    64  	PermissionManageNicknames Permissions = 1 << 27
    65  	// Allows management and editing of roles
    66  	PermissionManageRoles Permissions = 1 << 28
    67  	// Allows management and editing of webhooks
    68  	PermissionManageWebhooks Permissions = 1 << 29
    69  	// Allows management and editing of emojis
    70  	PermissionManageEmojis Permissions = 1 << 30
    71  
    72  	PermissionAllText = 0 |
    73  		PermissionViewChannel |
    74  		PermissionSendMessages |
    75  		PermissionSendTTSMessages |
    76  		PermissionManageMessages |
    77  		PermissionEmbedLinks |
    78  		PermissionAttachFiles |
    79  		PermissionReadMessageHistory |
    80  		PermissionMentionEveryone |
    81  		PermissionUseExternalEmojis
    82  
    83  	PermissionAllVoice = 0 |
    84  		PermissionConnect |
    85  		PermissionSpeak |
    86  		PermissionMuteMembers |
    87  		PermissionDeafenMembers |
    88  		PermissionMoveMembers |
    89  		PermissionUseVAD |
    90  		PermissionPrioritySpeaker
    91  
    92  	PermissionAllChannel = 0 |
    93  		PermissionAllText |
    94  		PermissionAllVoice |
    95  		PermissionCreateInstantInvite |
    96  		PermissionManageRoles |
    97  		PermissionManageChannels |
    98  		PermissionAddReactions |
    99  		PermissionViewAuditLog
   100  
   101  	PermissionAll = 0 |
   102  		PermissionAllChannel |
   103  		PermissionKickMembers |
   104  		PermissionBanMembers |
   105  		PermissionManageGuild |
   106  		PermissionAdministrator |
   107  		PermissionManageWebhooks |
   108  		PermissionManageEmojis |
   109  		PermissionManageNicknames |
   110  		PermissionChangeNickname
   111  )
   112  
   113  func (p Permissions) Has(perm Permissions) bool {
   114  	return HasFlag(uint64(p), uint64(perm))
   115  }
   116  
   117  func (p Permissions) Add(perm Permissions) Permissions {
   118  	return p | perm
   119  }
   120  
   121  func CalcOverwrites(guild Guild, channel Channel, member Member) Permissions {
   122  	if guild.OwnerID == member.User.ID {
   123  		return PermissionAll
   124  	}
   125  
   126  	var perm Permissions
   127  
   128  	for _, role := range guild.Roles {
   129  		if role.ID == RoleID(guild.ID) {
   130  			perm |= role.Permissions
   131  			break
   132  		}
   133  	}
   134  
   135  	for _, role := range guild.Roles {
   136  		for _, id := range member.RoleIDs {
   137  			if id == role.ID {
   138  				perm |= role.Permissions
   139  				break
   140  			}
   141  		}
   142  	}
   143  
   144  	if perm.Has(PermissionAdministrator) {
   145  		return PermissionAll
   146  	}
   147  
   148  	for _, overwrite := range channel.Permissions {
   149  		if GuildID(overwrite.ID) == guild.ID {
   150  			perm &= ^overwrite.Deny
   151  			perm |= overwrite.Allow
   152  			break
   153  		}
   154  	}
   155  
   156  	var deny, allow Permissions
   157  
   158  	for _, overwrite := range channel.Permissions {
   159  		for _, id := range member.RoleIDs {
   160  			if id == RoleID(overwrite.ID) && overwrite.Type == OverwriteRole {
   161  				deny |= overwrite.Deny
   162  				allow |= overwrite.Allow
   163  				break
   164  			}
   165  		}
   166  	}
   167  
   168  	perm &= ^deny
   169  	perm |= allow
   170  
   171  	for _, overwrite := range channel.Permissions {
   172  		if UserID(overwrite.ID) == member.User.ID {
   173  			perm &= ^overwrite.Deny
   174  			perm |= overwrite.Allow
   175  			break
   176  		}
   177  	}
   178  
   179  	if perm.Has(PermissionAdministrator) {
   180  		return PermissionAll
   181  	}
   182  
   183  	return perm
   184  }