crux_structs v0.1.6 Crux.Structs.Permissions View Source
Custom non discord api struct to help with working with permissions.
Link to this section Summary
Types
Union type of all valid permission name atoms
All valid types which can be directly resolved into a permissions bitfield
Represents a Crux.Struct.Permissions
Functions
Adds permissions to the base permissions
Resolves permissions for a user in a guild, optionally including channel permission overwrites
Check whether the second permissions are all present in the first
Creates a new Crux.Structs.Permissions
struct from a valid permissions/0
Returns the integer value of all permissions together
Returns a list of permission keys
Returns a map of permissions, keyed under their permission name with the bit value
Removes permissions from the base permissions
Resolves a permissions/0
into a bitfield representing the set permissions
Serializes permissions into a list of set permission_name/0
s
Serializes permissions into a map keyed by permission_name/0
with a boolean indicating whether the permission_name is set
Link to this section Types
permission_name() :: :create_instant_invite | :kick_members | :ban_members | :administrator | :manage_channels | :manage_guild | :add_reactions | :view_audit_log | :priority_speaker | :view_channel | :send_messages | :send_tts_message | :manage_messages | :embed_links | :attach_files | :read_message_histroy | :mention_everyone | :use_external_emojis | :connect | :speak | :mute_members | :deafen_members | :move_members | :use_vad | :change_nickname | :manage_nicknames | :manage_roles | :manage_webhooks | :manage_emojis
Union type of all valid permission name atoms
permissions() :: t() | [permission_name()] | non_neg_integer() | permission_name()
All valid types which can be directly resolved into a permissions bitfield.
t() :: %Crux.Structs.Permissions{bitfield: non_neg_integer()}
Represents a Crux.Struct.Permissions
.
Link to this section Functions
add(base :: permissions(), to_add :: permissions()) :: t()
Adds permissions to the base permissions.
Examples
iex> :administrator
...> |> Crux.Structs.Permissions.add(:manage_guild)
%Crux.Structs.Permissions{bitfield: 0x28}
from( member :: Crux.Structs.Member.t() | Crux.Structs.User.t() | Crux.Rest.snowflake(), guild :: Crux.Structs.Guild.t(), channel :: Crux.Structs.Channel.t() | nil ) :: t()
Resolves permissions for a user in a guild, optionally including channel permission overwrites.
Raises when the member is not cached.
The administrator flag or being owner implicitly grants all permissions.
has(permissions(), permissions()) :: boolean()
Check whether the second permissions are all present in the first.
Examples
# All permissions except administrator set, but administrator overrides
iex> Crux.Structs.Permissions.has(0x8, 0x7ff7fcf7)
true
iex> Crux.Structs.Permissions.has([:send_messages, :view_channel, :read_message_history], [:send_messages, :view_channel])
true
iex> Crux.Structs.Permissions.has(:administrator, 0x8)
true
iex> Crux.Structs.Permissions.has(0x8, :administrator)
true
new(permissions :: permissions()) :: t()
Creates a new Crux.Structs.Permissions
struct from a valid permissions/0
.
Returns the integer value of all permissions together.
permission_names() :: [permission_name()]
Returns a list of permission keys.
permissions() :: %{optional(permission_name()) => non_neg_integer()}
Returns a map of permissions, keyed under their permission name with the bit value.
remove(base :: permissions(), to_remove :: permissions()) :: t()
Removes permissions from the base permissions
Examples
iex> [0x8, 0x10, 0x20]
...> |> Crux.Structs.Permissions.remove([0x10, 0x20])
%Crux.Structs.Permissions{bitfield: 0x8}
resolve(permissions :: permissions()) :: non_neg_integer()
Resolves a permissions/0
into a bitfield representing the set permissions.
Examples
# A single bitflag
iex> 0x8
...> |> Crux.Structs.Permissions.resolve()
0x8
# A single permissions constant
iex> :administrator
...> |> Crux.Structs.Permissions.resolve()
0x8
# A list of bitflags
iex> [0x8, 0x4]
...> |> Crux.Structs.Permissions.resolve()
0xC
# A list of permissions constants
iex> [:administrator, :ban_members]
...> |> Crux.Structs.Permissions.resolve()
0xC
iex> [:manage_roles, 0x400, 0x800, :add_reactions]
...> |> Crux.Structs.Permissions.resolve()
0x10000C40
to_list(permissions :: permissions()) :: [permission_name()]
Serializes permissions into a list of set permission_name/0
s.
The administrator flag implicitly grants all permissions.
Examples
iex> 0x30
...> |> Crux.Structs.Permissions.to_list()
[:manage_guild, :manage_channels]
to_map(permissions :: permissions()) :: %{ optional(permission_name()) => boolean() }
Serializes permissions into a map keyed by permission_name/0
with a boolean indicating whether the permission_name is set.
The administrator flag implicitly grants all permissions.