Skip to content

Commit

Permalink
Update permission type (#544)
Browse files Browse the repository at this point in the history
* add and rename permissions

* use shifts instead of hex constants

* docs

* Unknown Permission
  • Loading branch information
lukellmann authored Feb 26, 2022
1 parent 1c47120 commit bc0e76b
Showing 1 changed file with 186 additions and 44 deletions.
230 changes: 186 additions & 44 deletions common/src/main/kotlin/entity/Permission.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import kotlin.contracts.contract
@Serializable(with = Permissions.Companion::class)
public data class Permissions(val code: DiscordBitSet) {
/**
* Returns this [Permissions] as a [Set] of [Permission]
* Returns this [Permissions] as a [Set] of [Permission]s, not including any [unknown][Permission.Unknown]
* permissions.
*/
val values: Set<Permission> = Permission.values.filter { it.code in code }.toSet()
val values: Set<Permission> get() = Permission.values.filter { it.code in code }.toSet()

public operator fun plus(permission: Permission): Permissions = Permissions(code + permission.code)

Expand Down Expand Up @@ -118,51 +119,189 @@ public fun Permissions(permissions: Iterable<Permissions>): Permissions = Permis
}


/** A [Permission](https://discord.com/developers/docs/topics/permissions) in Discord. */
public sealed class Permission(public val code: DiscordBitSet) {
protected constructor(vararg values: Long) : this(DiscordBitSet(values))

public object CreateInstantInvite : Permission(0x00000001)
public object KickMembers : Permission(0x00000002)
public object BanMembers : Permission(0x00000004)
public object Administrator : Permission(0x00000008)
public object ManageChannels : Permission(0x00000010)
public object ManageGuild : Permission(0x00000020)
public object AddReactions : Permission(0x00000040)
public object ViewAuditLog : Permission(0x00000080)
public object Stream : Permission(0x00000200)
public object ViewChannel : Permission(0x00000400)
public object SendMessages : Permission(0x00000800)
public object SendTTSMessages : Permission(0x00001000)
public object ManageMessages : Permission(0x00002000)
public object EmbedLinks : Permission(0x00004000)
public object AttachFiles : Permission(0x00008000)
public object ReadMessageHistory : Permission(0x00010000)
public object MentionEveryone : Permission(0x00020000)
public object UseExternalEmojis : Permission(0x00040000)
public object ViewGuildInsights : Permission(0x00080000)
public object Connect : Permission(0x00100000)
public object Speak : Permission(0x00200000)
public object MuteMembers : Permission(0x00400000)
public object DeafenMembers : Permission(0x00800000)
public object MoveMembers : Permission(0x01000000)
public object UseVAD : Permission(0x02000000)
public object PrioritySpeaker : Permission(0x00000100)
public object ChangeNickname : Permission(0x04000000)
public object ManageNicknames : Permission(0x08000000)
public object ManageRoles : Permission(0x10000000)
public object ManageWebhooks : Permission(0x20000000)
public object ManageEmojis : Permission(0x40000000)
public object UseSlashCommands : Permission(0x80000000)
public object RequestToSpeak : Permission(0x100000000)
public object ManageEvents : Permission(0x0200000000)
public object ManageThreads : Permission(0x0400000000)
public object CreatePublicThreads : Permission(0x0800000000)
public object CreatePrivateThreads : Permission(0x1000000000)
public object SendMessagesInThreads : Permission(0x4000000000)
public object ModerateMembers : Permission(0x0000010000000000)
/**
* A fallback [Permission] for permissions that haven't been added to Kord yet.
*
* You can use this to check if an instance of [Permissions] does [contain][Permissions.contains] an unknown
* permission:
*
* ```kotlin
* val permissions: Permissions = ...
* // 1 << 63 as an example for some new permission
* val hasNewPermission = Permission.Unknown(1L shl 63) in permissions
* ```
*
* See [here](https://discord.com/developers/docs/topics/permissions#permissions-bitwise-permission-flags) for a
* listing of the bitwise permission flags.
*/
public class Unknown : Permission {
public constructor(code: DiscordBitSet) : super(code)
public constructor(vararg values: Long) : super(*values)
}

/** Allows creation of instant invites. */
public object CreateInstantInvite : Permission(1L shl 0)

/** Allows kicking members. */
public object KickMembers : Permission(1L shl 1)

/** Allows banning members. */
public object BanMembers : Permission(1L shl 2)

/** Allows all permissions and bypasses channel permission overwrites. */
public object Administrator : Permission(1L shl 3)

/** Allows management and editing of channels. */
public object ManageChannels : Permission(1L shl 4)

/** Allows management and editing of the guild. */
public object ManageGuild : Permission(1L shl 5)

/** Allows for the addition of reactions to messages. */
public object AddReactions : Permission(1L shl 6)

/** Allows for viewing of audit logs. */
public object ViewAuditLog : Permission(1L shl 7)

/** Allows for using priority speaker in a voice channel. */
public object PrioritySpeaker : Permission(1L shl 8)

/** Allows the user to go live. */
public object Stream : Permission(1L shl 9)

/**
* Allows guild members to view a channel, which includes reading messages in text channels and joining voice
* channels.
*/
public object ViewChannel : Permission(1L shl 10)

/** Allows for sending messages in a channel (does not allow sending messages in threads). */
public object SendMessages : Permission(1L shl 11)

/** Allows for sending of `/tts` messages. */
public object SendTTSMessages : Permission(1L shl 12)

/** Allows for deletion of other users messages. */
public object ManageMessages : Permission(1L shl 13)

/** Links sent by users with this permission will be auto-embedded. */
public object EmbedLinks : Permission(1L shl 14)

/** Allows for uploading images and files. */
public object AttachFiles : Permission(1L shl 15)

/** Allows for reading of message history. */
public object ReadMessageHistory : Permission(1L shl 16)

/**
* 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.
*/
public object MentionEveryone : Permission(1L shl 17)

/** Allows the usage of custom emojis from other servers. */
public object UseExternalEmojis : Permission(1L shl 18)

/** Allows for viewing guild insights. */
public object ViewGuildInsights : Permission(1L shl 19)

/** Allows for joining of a voice channel. */
public object Connect : Permission(1L shl 20)

/** Allows for speaking in a voice channel. */
public object Speak : Permission(1L shl 21)

/** Allows for muting members in a voice channel. */
public object MuteMembers : Permission(1L shl 22)

/** Allows for deafening of members in a voice channel. */
public object DeafenMembers : Permission(1L shl 23)

/** Allows for moving of members between voice channels. */
public object MoveMembers : Permission(1L shl 24)

/** Allows for using voice-activity-detection in a voice channel. */
public object UseVAD : Permission(1L shl 25)

/** Allows for modification of own nickname. */
public object ChangeNickname : Permission(1L shl 26)

/** Allows for modification of other users nicknames. */
public object ManageNicknames : Permission(1L shl 27)

/** Allows management and editing of roles. */
public object ManageRoles : Permission(1L shl 28)

/** Allows management and editing of webhooks. */
public object ManageWebhooks : Permission(1L shl 29)

/** Allows management and editing of emojis and stickers. */
public object ManageEmojisAndStickers : Permission(1L shl 30)

/** Allows members to use application commands, including slash commands and context menu commands. */
public object UseApplicationCommands : Permission(1L shl 31)

/**
* Allows for requesting to speak in stage channels.
*
* _This permission is under active development and may be changed or removed._
*/
public object RequestToSpeak : Permission(1L shl 32)

/** Allows for creating, editing, and deleting scheduled events. */
public object ManageEvents : Permission(1L shl 33)

/** Allows for deleting and archiving threads, and viewing all private threads. */
public object ManageThreads : Permission(1L shl 34)

/** Allows for creating public and announcement threads. */
public object CreatePublicThreads : Permission(1L shl 35)

/** Allows for creating private threads. */
public object CreatePrivateThreads : Permission(1L shl 36)

/** Allows the usage of custom stickers from other servers. */
public object UseExternalStickers : Permission(1L shl 37)

/** Allows for sending messages in threads. */
public object SendMessagesInThreads : Permission(1L shl 38)

/** Allows for using Activities (applications with the `EMBEDDED` flag) in a voice channel. */
public object UseEmbeddedActivities : Permission(1L shl 39)

/**
* Allows for timing out users to prevent them from sending or reacting to messages in chat and threads, and from
* speaking in voice and stage channels.
*/
public object ModerateMembers : Permission(1L shl 40)


/** All [Permission]s combined into one. */
public object All : Permission(buildAll())


public companion object {

@Deprecated(
"Renamed to 'ManageEmojisAndStickers'.",
ReplaceWith("Permission.ManageEmojisAndStickers", "dev.kord.common.entity.Permission"),
DeprecationLevel.ERROR,
)
public val ManageEmojis: ManageEmojisAndStickers
get() = ManageEmojisAndStickers

@Deprecated(
"Renamed to 'UseApplicationCommands'.",
ReplaceWith("Permission.UseApplicationCommands", "dev.kord.common.entity.Permission"),
DeprecationLevel.ERROR,
)
public val UseSlashCommands: UseApplicationCommands
get() = UseApplicationCommands

// We cannot inline this into the "All" object, because that causes a weird compiler error
private fun buildAll(): DiscordBitSet = values.fold(EmptyBitSet()) { acc, value -> acc + value.code }

Expand All @@ -176,6 +315,8 @@ public sealed class Permission(public val code: DiscordBitSet) {
ManageGuild,
AddReactions,
ViewAuditLog,
PrioritySpeaker,
Stream,
ViewChannel,
SendMessages,
SendTTSMessages,
Expand All @@ -192,19 +333,20 @@ public sealed class Permission(public val code: DiscordBitSet) {
DeafenMembers,
MoveMembers,
UseVAD,
PrioritySpeaker,
ChangeNickname,
ManageNicknames,
ManageRoles,
ManageWebhooks,
ManageEmojis,
UseSlashCommands,
ManageEmojisAndStickers,
UseApplicationCommands,
RequestToSpeak,
ManageEvents,
ManageThreads,
CreatePublicThreads,
CreatePrivateThreads,
UseExternalStickers,
SendMessagesInThreads,
UseEmbeddedActivities,
ModerateMembers
)
}
Expand Down

0 comments on commit bc0e76b

Please sign in to comment.