crux_structs v0.2.0 Crux.Structs.Permissions View Source
Custom non discord api struct to help with working with permissions.
For more informations see Discord Docs.
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.Structs.Permissions
Functions
Adds permissions to the base permissions
Returns the integer value of all permissions summed up
Resolves permissions for a user in a guild, optionally including channel permission overwrites
Returns a map of all permissions
Check whether the second permissions are all present in the first
Resolves permissions for a user in a guild, optionally including channel permission overwrites
Similar to has/2
but returns a Crux.Structs.Permissions
of the missing permissions
Returns a list of all permission keys
Creates a new Crux.Structs.Permissions
struct from a valid resolvable/0
Removes permissions from the base permissions
Resolves a resolvable/0
into a bitfield representing the set permissions
Serializes permissions into a list of set name/0
s
Serializes permissions into a map keyed by name/0
with a boolean indicating whether the permission is set
Link to this section Types
name()
View Source
(since 0.2.0)
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
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.
resolvable()
View Source
(since 0.2.0)
resolvable() :: t() | non_neg_integer() | name() | [resolvable()]
resolvable() :: t() | non_neg_integer() | name() | [resolvable()]
All valid types which can be directly resolved into a permissions bitfield.
t()
View Source
(since 0.1.3)
t() :: %Crux.Structs.Permissions{bitfield: non_neg_integer()}
t() :: %Crux.Structs.Permissions{bitfield: non_neg_integer()}
Represents a Crux.Structs.Permissions
.
:bitfield
: The raw bitfield of permission flags.
Link to this section Functions
add(base, to_add)
View Source
(since 0.1.3)
add(base :: resolvable(), to_add :: resolvable()) :: t()
add(base :: resolvable(), to_add :: resolvable()) :: t()
Adds permissions to the base permissions.
Examples
iex> :administrator
...> |> Crux.Structs.Permissions.add(:manage_guild)
%Crux.Structs.Permissions{bitfield: 0x28}
all()
View Source
(since 0.2.0)
all() :: pos_integer()
all() :: pos_integer()
Returns the integer value of all permissions summed up.
explicit(member, guild, channel \\ nil)
View Source
(since 0.2.0)
explicit(
member ::
Crux.Structs.Member.t() | Crux.Structs.User.t() | Crux.Rest.snowflake(),
guild :: Crux.Structs.Guild.t(),
channel :: Crux.Structs.Channel.t() | nil
) :: t()
explicit( 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 does not grant permissions, see
implicit/3
.
flags()
View Source
(since 0.2.0)
flags() :: %{optional(name()) => non_neg_integer()}
flags() :: %{optional(name()) => non_neg_integer()}
Returns a map of all permissions.
has(have, want)
View Source
(since 0.1.3)
has(have :: resolvable(), want :: resolvable()) :: boolean()
has(have :: resolvable(), want :: resolvable()) :: boolean()
Check whether the second permissions are all present in the first.
Examples
# Administrator won't grant any other permissions
iex> Crux.Structs.Permissions.has(0x8, Crux.Structs.Permissions.all())
false
# Resolving a list of `permissions_name`s
iex> Crux.Structs.Permissions.has([:send_messages, :view_channel, :read_message_history], [:send_messages, :view_channel])
true
# Resolving different types of `permissions`s
iex> Crux.Structs.Permissions.has(:administrator, 0x8)
true
# In different order
iex> Crux.Structs.Permissions.has(0x8, :administrator)
true
implicit(member, guild, channel \\ nil)
View Source
(since 0.2.0)
implicit(
member ::
Crux.Structs.Member.t() | Crux.Structs.User.t() | Crux.Rest.snowflake(),
guild :: Crux.Structs.Guild.t(),
channel :: Crux.Structs.Channel.t() | nil
) :: t()
implicit( 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 guild-wide administrator flag or being owner implicitly grants all permissions, see
explicit/3
.
missing(have, want)
View Source
(since 0.2.0)
missing(resolvable(), resolvable()) :: t()
missing(resolvable(), resolvable()) :: t()
Similar to has/2
but returns a Crux.Structs.Permissions
of the missing permissions.
## Examples
```
iex> Crux.Structs.Permissions.missing([:send_messages, :view_channel], [:send_messages, :view_channel, :embed_links])
%Crux.Structs.Permissions{bitfield: 0x4000}
# Administrator won't implicilty grant other permissions
iex> Crux.Structs.Permissions.missing([:administrator], [:send_messages])
%Crux.Structs.Permissions{bitfield: 0x800}
# Everything set
iex> Crux.Structs.Permissions.missing([:kick_members, :ban_members, :view_audit_log], [:kick_members, :ban_members])
%Crux.Structs.Permissions{bitfield: 0}
# No permissions
iex> Crux.Structs.Permissions.missing([:send_messages, :view_channel], [])
%Crux.Structs.Permissions{bitfield: 0}
names()
View Source
(since 0.2.0)
names() :: [name()]
names() :: [name()]
Returns a list of all permission keys.
new(permissions \\ 0)
View Source
(since 0.1.3)
new(permissions :: resolvable()) :: t()
new(permissions :: resolvable()) :: t()
Creates a new Crux.Structs.Permissions
struct from a valid resolvable/0
.
remove(base, to_remove)
View Source
(since 0.1.3)
remove(base :: resolvable(), to_remove :: resolvable()) :: t()
remove(base :: resolvable(), to_remove :: resolvable()) :: 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)
View Source
(since 0.1.3)
resolve(permissions :: resolvable()) :: non_neg_integer()
resolve(permissions :: resolvable()) :: non_neg_integer()
Resolves a resolvable/0
into a bitfield representing the set permissions.
Examples
# A single bitflag
iex> 0x8
...> |> Crux.Structs.Permissions.resolve()
0x8
# A single name
iex> :administrator
...> |> Crux.Structs.Permissions.resolve()
0x8
# A list of bitflags
iex> [0x8, 0x4]
...> |> Crux.Structs.Permissions.resolve()
0xC
# A list of names
iex> [:administrator, :ban_members]
...> |> Crux.Structs.Permissions.resolve()
0xC
# A mixture of both
iex> [:manage_roles, 0x400, 0x800, :add_reactions]
...> |> Crux.Structs.Permissions.resolve()
0x10000C40
# An empty list
iex> []
...> |> Crux.Structs.Permissions.resolve()
0x0
to_list(permissions)
View Source
(since 0.1.3)
to_list(permissions :: resolvable()) :: [name()]
to_list(permissions :: resolvable()) :: [name()]
Serializes permissions into a list of set name/0
s.
Examples
iex> 0x30
...> |> Crux.Structs.Permissions.to_list()
[:manage_guild, :manage_channels]
to_map(permissions)
View Source
(since 0.1.3)
to_map(permissions :: resolvable()) :: %{optional(name()) => boolean()}
to_map(permissions :: resolvable()) :: %{optional(name()) => boolean()}
Serializes permissions into a map keyed by name/0
with a boolean indicating whether the permission is set.