1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-11 07:54:58 +00:00
arikawa/discord/permission.go

215 lines
5.8 KiB
Go
Raw Normal View History

2020-01-04 04:19:24 +00:00
package discord
type Permissions uint64
2021-05-21 23:40:11 +00:00
// https://discord.com/developers/docs/topics/permissions#permissions-bitwise-permission-flags
const (
2020-01-04 04:19:24 +00:00
// Allows creation of instant invites
PermissionCreateInstantInvite Permissions = 1 << iota
2020-01-04 04:19:24 +00:00
// Allows kicking members
PermissionKickMembers
2020-01-04 04:19:24 +00:00
// Allows banning members
PermissionBanMembers
2020-01-04 04:19:24 +00:00
// Allows all permissions and bypasses channel permission overwrites
PermissionAdministrator
2020-01-04 04:19:24 +00:00
// Allows management and editing of channels
PermissionManageChannels
2020-01-04 04:19:24 +00:00
// Allows management and editing of the guild
PermissionManageGuild
2020-01-04 04:19:24 +00:00
// Allows for the addition of reactions to messages
PermissionAddReactions
2020-01-04 04:19:24 +00:00
// Allows for viewing of audit logs
PermissionViewAuditLog
2020-01-04 04:19:24 +00:00
// Allows for using priority speaker in a voice channel
PermissionPrioritySpeaker
2020-01-04 04:19:24 +00:00
// Allows the user to go live
PermissionStream
2020-01-04 04:19:24 +00:00
// Allows guild members to view a channel, which includes reading messages
// in text channels
PermissionViewChannel
2020-01-04 04:19:24 +00:00
// Allows for sending messages in a channel
PermissionSendMessages
2020-01-04 04:19:24 +00:00
// Allows for sending of /tts messages
PermissionSendTTSMessages
2020-01-04 04:19:24 +00:00
// Allows for deletion of other users messages
PermissionManageMessages
2020-01-04 04:19:24 +00:00
// Links sent by users with this permission will be auto-embedded
PermissionEmbedLinks
2020-01-04 04:19:24 +00:00
// Allows for uploading images and files
PermissionAttachFiles
2020-01-04 04:19:24 +00:00
// Allows for reading of message history
PermissionReadMessageHistory
2020-01-04 04:19:24 +00:00
// Allows for using the @everyone tag to notify all users in a channel,
// and the @here tag to notify all online users in a channel
PermissionMentionEveryone
2020-01-04 04:19:24 +00:00
// Allows the usage of custom emojis from other servers
PermissionUseExternalEmojis
2020-05-11 22:06:19 +00:00
// ?
_
2020-01-04 04:19:24 +00:00
// Allows for joining of a voice channel
PermissionConnect
2020-01-04 04:19:24 +00:00
// Allows for speaking in a voice channel
PermissionSpeak
2020-01-04 04:19:24 +00:00
// Allows for muting members in a voice channel
PermissionMuteMembers
2020-01-04 04:19:24 +00:00
// Allows for deafening of members in a voice channel
PermissionDeafenMembers
2020-01-04 04:19:24 +00:00
// Allows for moving of members between voice channels
PermissionMoveMembers
2020-01-04 04:19:24 +00:00
// Allows for using voice-activity-detection in a voice channel
PermissionUseVAD
2020-01-04 04:19:24 +00:00
// Allows for modification of own nickname
PermissionChangeNickname
2020-01-04 04:19:24 +00:00
// Allows for modification of other users nicknames
PermissionManageNicknames
2020-01-04 04:19:24 +00:00
// Allows management and editing of roles
PermissionManageRoles
2020-01-04 04:19:24 +00:00
// Allows management and editing of webhooks
PermissionManageWebhooks
// Allows members to use slash commands in text channels
PermissionManageEmojisAndStickers
// Allows members to use slash commands in text channels
PermissionUseSlashCommands
// Allows for requesting to speak in stage channels. (This permission is
// under active development and may be changed or removed.)
PermissionRequestToSpeak
_
// Allows for deleting and archiving threads, and viewing all private
// threads
PermissionManageThreads
// Allows for creating and participating in threads.
PermissionCreatePublicThreads
// Allows for creating and participating in private threads.
PermissionCreatePrivateThreads
// Allows the usage of custom stickers from other servers
PermissionUseExternalStickers
// Allows for sending messages in threads
PermissionSendMessagesInThreads
// Allows for launching activities (applications with the EMBEDDED flag)
// in a voice channel
PermissionStartEmbeddedActivities
2020-01-04 04:19:24 +00:00
PermissionAllText = 0 |
PermissionViewChannel |
PermissionSendMessages |
PermissionSendTTSMessages |
PermissionManageMessages |
PermissionEmbedLinks |
PermissionAttachFiles |
PermissionReadMessageHistory |
PermissionMentionEveryone |
PermissionUseExternalEmojis |
PermissionUseSlashCommands |
PermissionManageThreads |
PermissionCreatePublicThreads |
PermissionCreatePrivateThreads |
PermissionUseExternalStickers |
PermissionAddReactions |
PermissionSendMessagesInThreads
2020-01-04 04:19:24 +00:00
PermissionAllVoice = 0 |
2021-05-21 06:21:49 +00:00
PermissionViewChannel |
2020-01-04 04:19:24 +00:00
PermissionConnect |
PermissionSpeak |
2021-05-21 06:21:49 +00:00
PermissionStream |
2020-01-04 04:19:24 +00:00
PermissionMuteMembers |
PermissionDeafenMembers |
PermissionMoveMembers |
PermissionUseVAD |
PermissionPrioritySpeaker |
PermissionRequestToSpeak |
PermissionStartEmbeddedActivities
2020-01-04 04:19:24 +00:00
PermissionAllChannel = 0 |
PermissionAllText |
PermissionAllVoice |
PermissionCreateInstantInvite |
PermissionManageRoles |
PermissionManageChannels
2020-01-04 04:19:24 +00:00
PermissionAll = 0 |
PermissionAllChannel |
PermissionKickMembers |
PermissionBanMembers |
PermissionManageGuild |
PermissionAdministrator |
PermissionManageWebhooks |
PermissionManageEmojisAndStickers |
PermissionManageNicknames |
PermissionChangeNickname |
PermissionViewAuditLog
2020-01-04 04:19:24 +00:00
)
func (p Permissions) Has(perm Permissions) bool {
return HasFlag(uint64(p), uint64(perm))
2020-01-04 04:19:24 +00:00
}
func (p Permissions) Add(perm Permissions) Permissions {
return p | perm
}
func CalcOverwrites(guild Guild, channel Channel, member Member) Permissions {
if guild.OwnerID == member.User.ID {
return PermissionAll
}
var perm Permissions
for _, role := range guild.Roles {
if role.ID == RoleID(guild.ID) {
2020-01-04 04:19:24 +00:00
perm |= role.Permissions
break
}
}
for _, role := range guild.Roles {
for _, id := range member.RoleIDs {
if id == role.ID {
perm |= role.Permissions
break
}
}
}
if perm.Has(PermissionAdministrator) {
return PermissionAll
}
for _, overwrite := range channel.Overwrites {
if GuildID(overwrite.ID) == guild.ID {
2020-01-04 04:19:24 +00:00
perm &= ^overwrite.Deny
perm |= overwrite.Allow
break
}
}
var deny, allow Permissions
for _, overwrite := range channel.Overwrites {
2020-01-04 04:19:24 +00:00
for _, id := range member.RoleIDs {
if id == RoleID(overwrite.ID) && overwrite.Type == OverwriteRole {
2020-01-04 04:19:24 +00:00
deny |= overwrite.Deny
allow |= overwrite.Allow
break
}
}
}
perm &= ^deny
perm |= allow
for _, overwrite := range channel.Overwrites {
if UserID(overwrite.ID) == member.User.ID {
2020-01-04 04:19:24 +00:00
perm &= ^overwrite.Deny
perm |= overwrite.Allow
break
}
}
if perm.Has(PermissionAdministrator) {
return PermissionAll
}
return perm
}