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
|
2021-05-21 06:12:53 +00:00
|
|
|
const (
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows creation of instant invites
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionCreateInstantInvite Permissions = 1 << iota
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows kicking members
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionKickMembers
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows banning members
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionBanMembers
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows all permissions and bypasses channel permission overwrites
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionAdministrator
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows management and editing of channels
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionManageChannels
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows management and editing of the guild
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionManageGuild
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for the addition of reactions to messages
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionAddReactions
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for viewing of audit logs
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionViewAuditLog
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for using priority speaker in a voice channel
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionPrioritySpeaker
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows the user to go live
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionStream
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows guild members to view a channel, which includes reading messages
|
|
|
|
// in text channels
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionViewChannel
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for sending messages in a channel
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionSendMessages
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for sending of /tts messages
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionSendTTSMessages
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for deletion of other users messages
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionManageMessages
|
2020-01-04 04:19:24 +00:00
|
|
|
// Links sent by users with this permission will be auto-embedded
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionEmbedLinks
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for uploading images and files
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionAttachFiles
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for reading of message history
|
2021-05-21 06:12:53 +00:00
|
|
|
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
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionMentionEveryone
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows the usage of custom emojis from other servers
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionUseExternalEmojis
|
2020-05-11 22:06:19 +00:00
|
|
|
// ?
|
2021-05-21 06:12:53 +00:00
|
|
|
_
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for joining of a voice channel
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionConnect
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for speaking in a voice channel
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionSpeak
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for muting members in a voice channel
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionMuteMembers
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for deafening of members in a voice channel
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionDeafenMembers
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for moving of members between voice channels
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionMoveMembers
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for using voice-activity-detection in a voice channel
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionUseVAD
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for modification of own nickname
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionChangeNickname
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows for modification of other users nicknames
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionManageNicknames
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows management and editing of roles
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionManageRoles
|
2020-01-04 04:19:24 +00:00
|
|
|
// Allows management and editing of webhooks
|
2021-05-21 06:12:53 +00:00
|
|
|
PermissionManageWebhooks
|
2021-08-08 20:19:15 +00:00
|
|
|
// 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.
|
2021-09-24 04:37:50 +00:00
|
|
|
PermissionCreatePublicThreads
|
2021-08-08 20:19:15 +00:00
|
|
|
// Allows for creating and participating in private threads.
|
2021-09-24 04:37:50 +00:00
|
|
|
PermissionCreatePrivateThreads
|
2021-08-08 20:19:15 +00:00
|
|
|
// Allows the usage of custom stickers from other servers
|
|
|
|
PermissionUseExternalStickers
|
2021-09-24 04:37:50 +00:00
|
|
|
// Allows for sending messages in threads
|
|
|
|
PermissionSendMessagesInThreads
|
|
|
|
// Allows for launching activities (applications with the EMBEDDED flag)
|
|
|
|
// in a voice channel
|
|
|
|
PermissionStartEmbeddedActivities
|
2021-12-20 23:56:11 +00:00
|
|
|
// Allows for timing out users
|
|
|
|
PermissionModerateMembers
|
2020-01-04 04:19:24 +00:00
|
|
|
|
|
|
|
PermissionAllText = 0 |
|
|
|
|
PermissionViewChannel |
|
|
|
|
PermissionSendMessages |
|
|
|
|
PermissionSendTTSMessages |
|
|
|
|
PermissionManageMessages |
|
|
|
|
PermissionEmbedLinks |
|
|
|
|
PermissionAttachFiles |
|
|
|
|
PermissionReadMessageHistory |
|
2020-01-27 19:51:56 +00:00
|
|
|
PermissionMentionEveryone |
|
2021-08-08 20:19:15 +00:00
|
|
|
PermissionUseExternalEmojis |
|
|
|
|
PermissionUseSlashCommands |
|
2021-09-24 04:37:50 +00:00
|
|
|
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 |
|
2021-08-08 20:19:15 +00:00
|
|
|
PermissionPrioritySpeaker |
|
2021-09-24 04:37:50 +00:00
|
|
|
PermissionRequestToSpeak |
|
|
|
|
PermissionStartEmbeddedActivities
|
2020-01-04 04:19:24 +00:00
|
|
|
|
|
|
|
PermissionAllChannel = 0 |
|
|
|
|
PermissionAllText |
|
|
|
|
PermissionAllVoice |
|
|
|
|
PermissionCreateInstantInvite |
|
|
|
|
PermissionManageRoles |
|
2021-09-24 04:37:50 +00:00
|
|
|
PermissionManageChannels
|
2020-01-04 04:19:24 +00:00
|
|
|
|
|
|
|
PermissionAll = 0 |
|
|
|
|
PermissionAllChannel |
|
|
|
|
PermissionKickMembers |
|
|
|
|
PermissionBanMembers |
|
|
|
|
PermissionManageGuild |
|
|
|
|
PermissionAdministrator |
|
|
|
|
PermissionManageWebhooks |
|
2021-08-08 20:19:15 +00:00
|
|
|
PermissionManageEmojisAndStickers |
|
2020-01-27 19:51:56 +00:00
|
|
|
PermissionManageNicknames |
|
2021-08-08 20:19:15 +00:00
|
|
|
PermissionChangeNickname |
|
2021-09-24 04:37:50 +00:00
|
|
|
PermissionViewAuditLog
|
2020-01-04 04:19:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (p Permissions) Has(perm Permissions) bool {
|
2020-05-05 06:13:15 +00:00
|
|
|
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 {
|
2020-07-21 20:27:59 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-08 20:19:15 +00:00
|
|
|
for _, overwrite := range channel.Overwrites {
|
2020-07-21 20:27:59 +00:00
|
|
|
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
|
|
|
|
|
2021-08-08 20:19:15 +00:00
|
|
|
for _, overwrite := range channel.Overwrites {
|
2020-01-04 04:19:24 +00:00
|
|
|
for _, id := range member.RoleIDs {
|
2020-10-28 21:49:30 +00:00
|
|
|
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
|
|
|
|
|
2021-08-08 20:19:15 +00:00
|
|
|
for _, overwrite := range channel.Overwrites {
|
2020-07-21 20:27:59 +00:00
|
|
|
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
|
|
|
|
}
|