Class: Discordrb::Overwrite

Inherits:
Object
  • Object
show all
Defined in:
lib/discordrb/data/overwrite.rb

Overview

A permissions overwrite, when applied to channels describes additional permissions a member needs to perform certain actions in context.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object = nil, type: nil, allow: 0, deny: 0) ⇒ Overwrite

Creates a new Overwrite object

Examples:

Create an overwrite for a role that can mention everyone, send TTS messages, but can't create instant invites

allow = Discordrb::Permissions.new
allow.can_mention_everyone = true
allow.can_send_tts_messages = true

deny = Discordrb::Permissions.new
deny.can_create_instant_invite = true

# Find some role by name
role = server.roles.find { |r| r.name == 'some role' }

Overwrite.new(role, allow: allow, deny: deny)

Create an overwrite by ID and permissions bits

Overwrite.new(120571255635181568, type: 'member', allow: 1024, deny: 0)

Parameters:

  • object (Integer, #id) (defaults to: nil)

    the ID or object this overwrite is for

  • type (String) (defaults to: nil)

    the type of object this overwrite is for (only required if object is an Integer)

  • allow (Integer, Permissions) (defaults to: 0)

    allowed permissions for this overwrite, by bits or a Permissions object

  • deny (Integer, Permissions) (defaults to: 0)

    denied permissions for this overwrite, by bits or a Permissions object

Raises:

  • (ArgumentError)

    if type is not :member or :role



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/discordrb/data/overwrite.rb', line 39

def initialize(object = nil, type: nil, allow: 0, deny: 0)
  if type
    type = type.to_sym
    raise ArgumentError, 'Overwrite type must be :member or :role' unless (type != :member) || (type != :role)
  end

  @id = object.respond_to?(:id) ? object.id : object

  @type = case object
          when User, Member, Recipient, Profile
            :member
          when Role
            :role
          else
            type
          end

  @allow = allow.is_a?(Permissions) ? allow : Permissions.new(allow)
  @deny = deny.is_a?(Permissions) ? deny : Permissions.new(deny)
end

Instance Attribute Details

#allowPermissions

Returns allowed permissions for this overwrite type.

Returns:

  • (Permissions)

    allowed permissions for this overwrite type



14
15
16
# File 'lib/discordrb/data/overwrite.rb', line 14

def allow
  @allow
end

#denyPermissions

Returns denied permissions for this overwrite type.

Returns:

  • (Permissions)

    denied permissions for this overwrite type



17
18
19
# File 'lib/discordrb/data/overwrite.rb', line 17

def deny
  @deny
end

#idInteger

Returns ID of the thing associated with this overwrite type.

Returns:

  • (Integer)

    ID of the thing associated with this overwrite type



8
9
10
# File 'lib/discordrb/data/overwrite.rb', line 8

def id
  @id
end

#typeSymbol

Returns either :role or :member.

Returns:

  • (Symbol)

    either :role or :member



11
12
13
# File 'lib/discordrb/data/overwrite.rb', line 11

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object

Comparison by attributes [:id, :type, :allow, :deny]



61
62
63
64
65
66
67
# File 'lib/discordrb/data/overwrite.rb', line 61

def ==(other)
  false unless other.is_a? Discordrb::Overwrite
  id == other.id &&
    type == other.type &&
    allow == other.allow &&
    deny == other.deny
end