Class: Discordrb::Overwrite

Inherits:
Object
  • Object
show all
Defined in:
lib/discordrb/data.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)

Raises:

  • (ArgumentError)

    if type is not :member or :role


1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
# File 'lib/discordrb/data.rb', line 1230

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 = if object.is_a?(User) || object.is_a?(Member) || object.is_a?(Recipient) || object.is_a?(Profile)
            :member
          elsif object.is_a? 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


1205
1206
1207
# File 'lib/discordrb/data.rb', line 1205

def allow
  @allow
end

#denyPermissions


1208
1209
1210
# File 'lib/discordrb/data.rb', line 1208

def deny
  @deny
end

#idInteger


1199
1200
1201
# File 'lib/discordrb/data.rb', line 1199

def id
  @id
end

#typeSymbol


1202
1203
1204
# File 'lib/discordrb/data.rb', line 1202

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object

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


1251
1252
1253
1254
1255
1256
1257
# File 'lib/discordrb/data.rb', line 1251

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