1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-05 06:26:08 +00:00
arikawa/discord/permission.go

185 lines
5.2 KiB
Go
Raw Normal View History

2020-01-04 04:19:24 +00:00
package discord
type Permissions uint64
2020-05-11 22:06:19 +00:00
var (
2020-01-04 04:19:24 +00:00
// Allows creation of instant invites
2020-05-11 22:06:19 +00:00
PermissionCreateInstantInvite Permissions = 1 << 0
2020-01-04 04:19:24 +00:00
// Allows kicking members
2020-05-11 22:06:19 +00:00
PermissionKickMembers Permissions = 1 << 1
2020-01-04 04:19:24 +00:00
// Allows banning members
2020-05-11 22:06:19 +00:00
PermissionBanMembers Permissions = 1 << 2
2020-01-04 04:19:24 +00:00
// Allows all permissions and bypasses channel permission overwrites
2020-05-11 22:06:19 +00:00
PermissionAdministrator Permissions = 1 << 3
2020-01-04 04:19:24 +00:00
// Allows management and editing of channels
2020-05-11 22:06:19 +00:00
PermissionManageChannels Permissions = 1 << 4
2020-01-04 04:19:24 +00:00
// Allows management and editing of the guild
2020-05-11 22:06:19 +00:00
PermissionManageGuild Permissions = 1 << 5
2020-01-04 04:19:24 +00:00
// Allows for the addition of reactions to messages
2020-05-11 22:06:19 +00:00
PermissionAddReactions Permissions = 1 << 6
2020-01-04 04:19:24 +00:00
// Allows for viewing of audit logs
2020-05-11 22:06:19 +00:00
PermissionViewAuditLog Permissions = 1 << 7
2020-01-04 04:19:24 +00:00
// Allows for using priority speaker in a voice channel
2020-05-11 22:06:19 +00:00
PermissionPrioritySpeaker Permissions = 1 << 8
2020-01-04 04:19:24 +00:00
// Allows the user to go live
2020-05-11 22:06:19 +00:00
PermissionStream Permissions = 1 << 9
2020-01-04 04:19:24 +00:00
// Allows guild members to view a channel, which includes reading messages
// in text channels
2020-05-11 22:06:19 +00:00
PermissionViewChannel Permissions = 1 << 10
2020-01-04 04:19:24 +00:00
// Allows for sending messages in a channel
2020-05-11 22:06:19 +00:00
PermissionSendMessages Permissions = 1 << 11
2020-01-04 04:19:24 +00:00
// Allows for sending of /tts messages
2020-05-11 22:06:19 +00:00
PermissionSendTTSMessages Permissions = 1 << 12
2020-01-04 04:19:24 +00:00
// Allows for deletion of other users messages
2020-05-11 22:06:19 +00:00
PermissionManageMessages Permissions = 1 << 13
2020-01-04 04:19:24 +00:00
// Links sent by users with this permission will be auto-embedded
2020-05-11 22:06:19 +00:00
PermissionEmbedLinks Permissions = 1 << 14
2020-01-04 04:19:24 +00:00
// Allows for uploading images and files
2020-05-11 22:06:19 +00:00
PermissionAttachFiles Permissions = 1 << 15
2020-01-04 04:19:24 +00:00
// Allows for reading of message history
2020-05-11 22:06:19 +00:00
PermissionReadMessageHistory Permissions = 1 << 16
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
2020-05-11 22:06:19 +00:00
PermissionMentionEveryone Permissions = 1 << 17
2020-01-04 04:19:24 +00:00
// Allows the usage of custom emojis from other servers
2020-05-11 22:06:19 +00:00
PermissionUseExternalEmojis Permissions = 1 << 18
2020-01-04 04:19:24 +00:00
2020-05-11 22:06:19 +00:00
// ?
2020-01-04 04:19:24 +00:00
// Allows for joining of a voice channel
2020-05-11 22:06:19 +00:00
PermissionConnect Permissions = 1 << 20
2020-01-04 04:19:24 +00:00
// Allows for speaking in a voice channel
2020-05-11 22:06:19 +00:00
PermissionSpeak Permissions = 1 << 21
2020-01-04 04:19:24 +00:00
// Allows for muting members in a voice channel
2020-05-11 22:06:19 +00:00
PermissionMuteMembers Permissions = 1 << 22
2020-01-04 04:19:24 +00:00
// Allows for deafening of members in a voice channel
2020-05-11 22:06:19 +00:00
PermissionDeafenMembers Permissions = 1 << 23
2020-01-04 04:19:24 +00:00
// Allows for moving of members between voice channels
2020-05-11 22:06:19 +00:00
PermissionMoveMembers Permissions = 1 << 24
2020-01-04 04:19:24 +00:00
// Allows for using voice-activity-detection in a voice channel
2020-05-11 22:06:19 +00:00
PermissionUseVAD Permissions = 1 << 25
2020-01-04 04:19:24 +00:00
// Allows for modification of own nickname
2020-05-11 22:06:19 +00:00
PermissionChangeNickname Permissions = 1 << 26
2020-01-04 04:19:24 +00:00
// Allows for modification of other users nicknames
2020-05-11 22:06:19 +00:00
PermissionManageNicknames Permissions = 1 << 27
2020-01-04 04:19:24 +00:00
// Allows management and editing of roles
2020-05-11 22:06:19 +00:00
PermissionManageRoles Permissions = 1 << 28
2020-01-04 04:19:24 +00:00
// Allows management and editing of webhooks
2020-05-11 22:06:19 +00:00
PermissionManageWebhooks Permissions = 1 << 29
2020-01-04 04:19:24 +00:00
// Allows management and editing of emojis
2020-05-11 22:06:19 +00:00
PermissionManageEmojis Permissions = 1 << 30
2020-01-04 04:19:24 +00:00
PermissionAllText = 0 |
PermissionViewChannel |
PermissionSendMessages |
PermissionSendTTSMessages |
PermissionManageMessages |
PermissionEmbedLinks |
PermissionAttachFiles |
PermissionReadMessageHistory |
PermissionMentionEveryone |
PermissionUseExternalEmojis
2020-01-04 04:19:24 +00:00
PermissionAllVoice = 0 |
PermissionConnect |
PermissionSpeak |
PermissionMuteMembers |
PermissionDeafenMembers |
PermissionMoveMembers |
PermissionUseVAD |
PermissionPrioritySpeaker
PermissionAllChannel = 0 |
PermissionAllText |
PermissionAllVoice |
PermissionCreateInstantInvite |
PermissionManageRoles |
PermissionManageChannels |
PermissionAddReactions |
PermissionViewAuditLog
PermissionAll = 0 |
PermissionAllChannel |
PermissionKickMembers |
PermissionBanMembers |
PermissionManageGuild |
PermissionAdministrator |
PermissionManageWebhooks |
PermissionManageEmojis |
PermissionManageNicknames |
PermissionChangeNickname
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.Permissions {
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.Permissions {
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.Permissions {
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
}