Class: Discordrb::Channel

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

Overview

A Discord channel, including data like the topic

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, bot, server = nil) ⇒ Channel

Returns a new instance of Channel.



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/discordrb/data.rb', line 297

def initialize(data, bot, server = nil)
  @bot = bot

  # data is a sometimes a Hash and othertimes an array of Hashes, you only want the last one if it's an array
  data = data[-1] if data.is_a?(Array)

  @id = data['id'].to_i
  @type = data['type'] || 'text'
  @topic = data['topic']
  @position = data['position']

  @is_private = data['is_private']
  if @is_private
    @recipient = User.new(data['recipient'], bot)
    @name = @recipient.username
  else
    @name = data['name']
    @server = bot.server(data['guild_id'].to_i)
    @server ||= server
  end

  # Populate permission overwrites
  @permission_overwrites = {}
  return unless data['permission_overwrites']
  data['permission_overwrites'].each do |element|
    role_id = element['id'].to_i
    deny = Permissions.new(element['deny'])
    allow = Permissions.new(element['allow'])
    @permission_overwrites[role_id] = OpenStruct.new
    @permission_overwrites[role_id].deny = deny
    @permission_overwrites[role_id].allow = allow
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



291
292
293
# File 'lib/discordrb/data.rb', line 291

def id
  @id
end

#is_privateObject (readonly)

Returns the value of attribute is_private.



291
292
293
# File 'lib/discordrb/data.rb', line 291

def is_private
  @is_private
end

#nameObject

Returns the value of attribute name.



291
292
293
# File 'lib/discordrb/data.rb', line 291

def name
  @name
end

#permission_overwritesObject (readonly)

Returns the value of attribute permission_overwrites.



291
292
293
# File 'lib/discordrb/data.rb', line 291

def permission_overwrites
  @permission_overwrites
end

#positionObject

Returns the value of attribute position.



291
292
293
# File 'lib/discordrb/data.rb', line 291

def position
  @position
end

#recipientObject (readonly)

Returns the value of attribute recipient.



291
292
293
# File 'lib/discordrb/data.rb', line 291

def recipient
  @recipient
end

#serverObject (readonly)

Returns the value of attribute server.



291
292
293
# File 'lib/discordrb/data.rb', line 291

def server
  @server
end

#topicObject

Returns the value of attribute topic.



291
292
293
# File 'lib/discordrb/data.rb', line 291

def topic
  @topic
end

#typeObject (readonly)

Returns the value of attribute type.



291
292
293
# File 'lib/discordrb/data.rb', line 291

def type
  @type
end

Instance Method Details

#await(key, attributes = {}, &block) ⇒ Object

Add an await for a message in this channel



387
388
389
# File 'lib/discordrb/data.rb', line 387

def await(key, attributes = {}, &block)
  @bot.add_await(key, Discordrb::Events::MessageEvent, { in: @id }.merge(attributes), &block)
end

#deleteObject



339
340
341
# File 'lib/discordrb/data.rb', line 339

def delete
  API.delete_channel(@bot.token, @id)
end

#history(amount, before_id = nil, after_id = nil) ⇒ Object



377
378
379
380
# File 'lib/discordrb/data.rb', line 377

def history(amount, before_id = nil, after_id = nil)
  logs = API.channel_log(@bot.token, @id, amount, before_id, after_id)
  JSON.parse(logs).map { |message| Message.new(message, @bot) }
end

#make_invite(max_age = 0, max_uses = 0, temporary = false, xkcd = false) ⇒ Object Also known as: invite



391
392
393
394
# File 'lib/discordrb/data.rb', line 391

def make_invite(max_age = 0, max_uses = 0, temporary = false, xkcd = false)
  response = API.create_invite(@bot.token, @id, max_age, max_uses, temporary, xkcd)
  Invite.new(JSON.parse(response), @bot)
end

#private?Boolean

Returns:

  • (Boolean)


293
294
295
# File 'lib/discordrb/data.rb', line 293

def private?
  @server.nil?
end

#send_file(file) ⇒ Object



335
336
337
# File 'lib/discordrb/data.rb', line 335

def send_file(file)
  @bot.send_file(@id, file)
end

#send_message(content) ⇒ Object Also known as: send, message



331
332
333
# File 'lib/discordrb/data.rb', line 331

def send_message(content)
  @bot.send_message(@id, content)
end

#start_typingObject

Starts typing, which displays the typing indicator on the client for five seconds. If you want to keep typing you'll have to resend this every five seconds. (An abstraction for this will eventually be coming)



399
400
401
# File 'lib/discordrb/data.rb', line 399

def start_typing
  API.start_typing(@bot.token, @id)
end

#update_from(other) ⇒ Object



358
359
360
361
362
363
364
# File 'lib/discordrb/data.rb', line 358

def update_from(other)
  @topic = other.topic
  @name = other.name
  @is_private = other.is_private
  @recipient = other.recipient
  @permission_overwrites = other.permission_overwrites
end

#update_overwrites(overwrites) ⇒ Object



382
383
384
# File 'lib/discordrb/data.rb', line 382

def update_overwrites(overwrites)
  @permission_overwrites = overwrites
end

#usersObject

List of users currently in a channel



367
368
369
370
371
372
373
374
375
# File 'lib/discordrb/data.rb', line 367

def users
  if @type == 'text'
    @server.members.select { |u| u.status != :offline }
  else
    @server.members.select do |user|
      user.voice_channel.id == @id if user.voice_channel
    end
  end
end