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

Instance Attribute Details

#idInteger (readonly) Also known as: resolve_id

Note:

If this channel is a #general channel, its ID will be equal to the server on which it is on.

Returns the channel's unique ID.

Returns:

  • (Integer)

    the channel's unique ID.



481
482
483
# File 'lib/discordrb/data.rb', line 481

def id
  @id
end

#is_privatetrue, false (readonly)

Deprecated.

Use #private? instead, it's guaranteed to be accurate.

Note:

This data is sent by Discord and it's possible for this to falsely be true for certain kinds of integration channels (like Twitch subscriber ones). This appears to be a Discord bug that I can't reproduce myself, due to not having any integrations in place. If this occurs to you please tell me.

Returns whether or not this channel is a private messaging channel.

Returns:

  • (true, false)

    whether or not this channel is a private messaging channel.



488
489
490
# File 'lib/discordrb/data.rb', line 488

def is_private
  @is_private
end

#nameString

Returns this channel's name.

Returns:

  • (String)

    this channel's name.



471
472
473
# File 'lib/discordrb/data.rb', line 471

def name
  @name
end

#permission_overwritesHash<Integer => OpenStruct> (readonly)

This channel's permission overwrites, represented as a hash of role/user ID to an OpenStruct which has the allow and deny properties which are Permissions objects respectively.

Returns:

  • (Hash<Integer => OpenStruct>)

    the channel's permission overwrites



502
503
504
# File 'lib/discordrb/data.rb', line 502

def permission_overwrites
  @permission_overwrites
end

#positionInteger

Returns the channel's position on the channel list.

Returns:

  • (Integer)

    the channel's position on the channel list



497
498
499
# File 'lib/discordrb/data.rb', line 497

def position
  @position
end

#recipientUser? (readonly)

Returns the recipient of the private messages, or nil if this is not a PM channel.

Returns:

  • (User, nil)

    the recipient of the private messages, or nil if this is not a PM channel



491
492
493
# File 'lib/discordrb/data.rb', line 491

def recipient
  @recipient
end

#serverServer (readonly)

Returns the server this channel is on.

Returns:

  • (Server)

    the server this channel is on.



474
475
476
# File 'lib/discordrb/data.rb', line 474

def server
  @server
end

#topicString

Returns the channel's topic.

Returns:

  • (String)

    the channel's topic



494
495
496
# File 'lib/discordrb/data.rb', line 494

def topic
  @topic
end

#typeString (readonly)

Returns the type of this channel (currently either 'text' or 'voice').

Returns:

  • (String)

    the type of this channel (currently either 'text' or 'voice')



477
478
479
# File 'lib/discordrb/data.rb', line 477

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object

ID based comparison



547
548
549
# File 'lib/discordrb/data.rb', line 547

def ==(other)
  Discordrb.id_compare(@id, other)
end

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

Add an Await for a message in this channel. This is identical in functionality to adding a Events::MessageEvent await with the in attribute as this channel.

See Also:



637
638
639
# File 'lib/discordrb/data.rb', line 637

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

#deleteObject

Permanently deletes this channel



565
566
567
# File 'lib/discordrb/data.rb', line 565

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

#history(amount, before_id = nil, after_id = nil) ⇒ Array<Message>

Retrieves some of this channel's message history.

Parameters:

  • amount (Integer)

    How many messages to retrieve. This must be less than or equal to 100, if it is higher than 100 it will be treated as 100 on Discord's side.

  • before_id (Integer) (defaults to: nil)

    The ID of the most recent message the retrieval should start at, or nil if it should start at the current message.

  • after_id (Integer) (defaults to: nil)

    The ID of the oldest message the retrieval should start at, or nil if it should start as soon as possible with the specified amount.

Returns:

  • (Array<Message>)

    the retrieved messages.



622
623
624
625
# File 'lib/discordrb/data.rb', line 622

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



641
642
643
644
# File 'lib/discordrb/data.rb', line 641

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?true, false

Returns whether or not this channel is a PM channel, with more accuracy than #is_private.

Returns:

  • (true, false)

    whether or not this channel is a PM channel, with more accuracy than #is_private.



507
508
509
# File 'lib/discordrb/data.rb', line 507

def private?
  @server.nil?
end

#send_file(file) ⇒ Object

Sends a file to this channel. If it is an image, it will be embedded.

Parameters:

  • file (File)

    The file to send. There's no clear size limit for this, you'll have to attempt it for yourself (most non-image files are fine, large images may fail to embed)



560
561
562
# File 'lib/discordrb/data.rb', line 560

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

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

Sends a message to this channel.

Parameters:

  • content (String)

    The content to send. Should not be longer than 2000 characters or it will result in an error.

Returns:

  • (Message)

    the message that was sent.



554
555
556
# File 'lib/discordrb/data.rb', line 554

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)



649
650
651
# File 'lib/discordrb/data.rb', line 649

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

#usersArray<User>

The list of users currently in this channel. This is mostly useful for a voice channel, for a text channel it will just return the users on the server that are online.

Returns:

  • (Array<User>)

    the users in this channel



604
605
606
607
608
609
610
611
612
# File 'lib/discordrb/data.rb', line 604

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