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

t()

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

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

Link to this type permission_name() View Source (since 0.1.3)
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

Link to this type permissions() View Source (since 0.1.3)
permissions() ::
  t() | [permission_name()] | non_neg_integer() | permission_name()

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.Struct.Permissions.

Link to this section Functions

Link to this function add(base, to_add) View Source (since 0.1.3)
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}
Link to this function from(member, guild, channel \\ nil) View Source (since 0.1.3)
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.

Link to this function has(current, want) View Source (since 0.1.3)

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

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

Link to this function permission_all() View Source (since 0.1.3)
permission_all() :: pos_integer()

Returns the integer value of all permissions together.

Link to this function permission_names() View Source (since 0.1.3)
permission_names() :: [permission_name()]

Returns a list of permission keys.

Link to this function permissions() View Source (since 0.1.3)
permissions() :: %{optional(permission_name()) => non_neg_integer()}

Returns a map of permissions, keyed under their permission name with the bit value.

Link to this function remove(base, to_remove) View Source (since 0.1.3)
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}
Link to this function resolve(permissions) View Source (since 0.1.3)
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
Link to this function to_list(permissions) View Source (since 0.1.3)
to_list(permissions :: permissions()) :: [permission_name()]

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

The administrator flag implicitly grants all permissions.

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 :: 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.