Crux vdev 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.

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/0s.

Serializes permissions into a map keyed by name/0 with a boolean indicating whether the permission is set.

Link to this section Types

Link to this type

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

Union type of all valid permission name atoms.

Link to this type

resolvable() View Source (since 0.2.0)
resolvable() :: t() | non_neg_integer() | name() | [resolvable()]

All valid types which can be directly resolved into a permissions bitfield.

Link to this type

t() View Source (since 0.1.3)
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

Link to this function

add(base, to_add) View Source (since 0.1.3)
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}

Returns the integer value of all permissions summed up.

Link to this function

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()

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.

Link to this function

flags() View Source (since 0.2.0)
flags() :: %{optional(name()) => non_neg_integer()}

Returns a map of all permissions.

Link to this function

has(have, want) View Source (since 0.1.3)
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
Link to this function

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()

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.

Link to this function

missing(have, want) View Source (since 0.2.0)
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}

Link to this function

names() View Source (since 0.2.0)
names() :: [name()]

Returns a list of all permission keys.

Link to this function

new(permissions \\ 0) View Source (since 0.1.3)
new(permissions :: resolvable()) :: t()

Creates a new Crux.Structs.Permissions struct from a valid resolvable/0.

Link to this function

remove(base, to_remove) View Source (since 0.1.3)
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}
Link to this function

resolve(permissions) View Source (since 0.1.3)
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
Link to this function

to_list(permissions) View Source (since 0.1.3)
to_list(permissions :: resolvable()) :: [name()]

Serializes permissions into a list of set name/0s.

Examples

iex> 0x30
...> |> Crux.Structs.Permissions.to_list()
[:manage_guild, :manage_channels]
Link to this function

to_map(permissions) View Source (since 0.1.3)
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.