Class: Rubycord::Channel

Inherits:
Object
  • Object
show all
Includes:
IDObject
Defined in:
lib/rubycord/data/channel.rb

Overview

A Discord channel, including data like the topic

Constant Summary collapse

TYPES =

Map of channel types

{
  text: 0,
  dm: 1,
  voice: 2,
  group: 3,
  category: 4,
  news: 5,
  store: 6,
  news_thread: 10,
  public_thread: 11,
  private_thread: 12,
  stage_voice: 13,
  directory: 14,
  forum: 15
}.freeze

Instance Attribute Summary collapse

Attributes included from IDObject

#id

Threads collapse

Instance Method Summary collapse

Methods included from IDObject

#==, #creation_time, synthesise

Instance Attribute Details

#archive_timestampTime? (readonly)

Returns The timestamp of when this threads status last changed.

Returns:

  • (Time, nil)

    The timestamp of when this threads status last changed.



78
79
80
# File 'lib/rubycord/data/channel.rb', line 78

def archive_timestamp
  @archive_timestamp
end

#archivedtrue, ... (readonly)

Returns Whether or not this thread is archived.

Returns:

  • (true, false, nil)

    Whether or not this thread is archived.



72
73
74
# File 'lib/rubycord/data/channel.rb', line 72

def archived
  @archived
end

#auto_archive_durationInteger? (readonly)

Returns How long after the last message before a thread is automatically archived.

Returns:

  • (Integer, nil)

    How long after the last message before a thread is automatically archived.



75
76
77
# File 'lib/rubycord/data/channel.rb', line 75

def auto_archive_duration
  @auto_archive_duration
end

#bitrateInteger

Returns the bitrate (in bps) of the channel.

Returns:

  • (Integer)

    the bitrate (in bps) of the channel



48
49
50
# File 'lib/rubycord/data/channel.rb', line 48

def bitrate
  @bitrate
end

#invitabletrue, false (readonly)

Returns For private threads, determines whether non-moderators can add other non-moderators to a thread.

Returns:

  • (true, false)

    For private threads, determines whether non-moderators can add other non-moderators to a thread.



92
93
94
# File 'lib/rubycord/data/channel.rb', line 92

def invitable
  @invitable
end

#join_timestampTime? (readonly)

Returns When the current user joined this thread.

Returns:

  • (Time, nil)

    When the current user joined this thread.



85
86
87
# File 'lib/rubycord/data/channel.rb', line 85

def join_timestamp
  @join_timestamp
end

#lockedtrue, ... (readonly) Also known as: locked?

Returns Whether this thread is locked or not.

Returns:

  • (true, false, nil)

    Whether this thread is locked or not.



81
82
83
# File 'lib/rubycord/data/channel.rb', line 81

def locked
  @locked
end

#member_countInteger? (readonly)

Returns An approximate count of members in a thread. Stops counting at 50.

Returns:

  • (Integer, nil)

    An approximate count of members in a thread. Stops counting at 50.



69
70
71
# File 'lib/rubycord/data/channel.rb', line 69

def member_count
  @member_count
end

#member_flagsInteger? (readonly)

Returns Member flags for this thread, used for notifications.

Returns:

  • (Integer, nil)

    Member flags for this thread, used for notifications.



88
89
90
# File 'lib/rubycord/data/channel.rb', line 88

def member_flags
  @member_flags
end

#message_countInteger? (readonly)

Returns An approximate count of messages sent in a thread. Stops counting at 50.

Returns:

  • (Integer, nil)

    An approximate count of messages sent in a thread. Stops counting at 50.



66
67
68
# File 'lib/rubycord/data/channel.rb', line 66

def message_count
  @message_count
end

#nameString

Returns this channel's name.

Returns:

  • (String)

    this channel's name.



27
28
29
# File 'lib/rubycord/data/channel.rb', line 27

def name
  @name
end

#nsfwtrue, false Also known as: nsfw?

Returns if this channel is marked as nsfw.

Returns:

  • (true, false)

    if this channel is marked as nsfw



58
59
60
# File 'lib/rubycord/data/channel.rb', line 58

def nsfw
  @nsfw
end

#owner_idInteger? (readonly)

Returns the ID of the owner of the group channel or nil if this is not a group channel. If this channel is a thread, this is the member that started the thread.

Returns:

  • (Integer, nil)

    the ID of the owner of the group channel or nil if this is not a group channel. If this channel is a thread, this is the member that started the thread.



39
40
41
# File 'lib/rubycord/data/channel.rb', line 39

def owner_id
  @owner_id
end

#parent_idInteger? (readonly)

Returns the ID of the parent channel, if this channel is inside a category. If this channel is a thread, this is the text channel it is a child to.

Returns:

  • (Integer, nil)

    the ID of the parent channel, if this channel is inside a category. If this channel is a thread, this is the text channel it is a child to.



31
32
33
# File 'lib/rubycord/data/channel.rb', line 31

def parent_id
  @parent_id
end

#positionInteger

Returns the channel's position on the channel list.

Returns:

  • (Integer)

    the channel's position on the channel list



55
56
57
# File 'lib/rubycord/data/channel.rb', line 55

def position
  @position
end

#rate_limit_per_userInteger Also known as: slowmode_rate

Returns the amount of time (in seconds) users need to wait to send in between messages.

Returns:

  • (Integer)

    the amount of time (in seconds) users need to wait to send in between messages.



62
63
64
# File 'lib/rubycord/data/channel.rb', line 62

def rate_limit_per_user
  @rate_limit_per_user
end

#recipientsArray<Recipient>? (readonly)

Returns the array of recipients of the private messages, or nil if this is not a Private channel.

Returns:

  • (Array<Recipient>, nil)

    the array of recipients of the private messages, or nil if this is not a Private channel



42
43
44
# File 'lib/rubycord/data/channel.rb', line 42

def recipients
  @recipients
end

#topicString

Returns the channel's topic.

Returns:

  • (String)

    the channel's topic



45
46
47
# File 'lib/rubycord/data/channel.rb', line 45

def topic
  @topic
end

#typeInteger (readonly)

Returns the type of this channel.

Returns:

  • (Integer)

    the type of this channel

See Also:



35
36
37
# File 'lib/rubycord/data/channel.rb', line 35

def type
  @type
end

#user_limitInteger Also known as: limit

Returns the amount of users that can be in the channel. 0 means it is unlimited.

Returns:

  • (Integer)

    the amount of users that can be in the channel. 0 means it is unlimited.



51
52
53
# File 'lib/rubycord/data/channel.rb', line 51

def user_limit
  @user_limit
end

Instance Method Details

#add_group_users(user_ids) ⇒ Channel Also known as: add_group_user

Adds a user to a group channel.

Parameters:

Returns:



769
770
771
772
773
774
775
776
777
# File 'lib/rubycord/data/channel.rb', line 769

def add_group_users(user_ids)
  raise "Attempted to add a user to a non-group channel!" unless group?

  user_ids = [user_ids] unless user_ids.is_a? Array
  user_ids.each do |user_id|
    API::Channel.add_group_user(@bot.token, @id, user_id.resolve_id)
  end
  self
end

#add_member(member) ⇒ Object

Add a member to the thread

Parameters:



876
877
878
# File 'lib/rubycord/data/channel.rb', line 876

def add_member(member)
  @bot.add_thread_member(@id, member)
end

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

Deprecated.

Will be changed to blocking behavior in v4.0. Use #await! instead.

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:



720
721
722
# File 'lib/rubycord/data/channel.rb', line 720

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

#await!(attributes = {}) ⇒ Object

Add a blocking 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:



727
728
729
# File 'lib/rubycord/data/channel.rb', line 727

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

#categoryChannel? Also known as: parent

Returns the category channel, if this channel is in a category.

Returns:

  • (Channel, nil)

    the category channel, if this channel is in a category



231
232
233
# File 'lib/rubycord/data/channel.rb', line 231

def category
  @bot.channel(@parent_id) if @parent_id
end

#category=(channel) ⇒ Object Also known as: parent=

Sets this channels parent category

Parameters:

Raises:

  • (ArgumentError)

    if the target channel isn't a category



240
241
242
243
244
245
# File 'lib/rubycord/data/channel.rb', line 240

def category=(channel)
  channel = @bot.channel(channel)
  raise ArgumentError, "Cannot set parent category to a channel that isn't a category" unless channel.category?

  update_channel_data(parent_id: channel.id)
end

#category?true, false

Returns whether or not this channel is a category channel.

Returns:

  • (true, false)

    whether or not this channel is a category channel.



196
197
198
# File 'lib/rubycord/data/channel.rb', line 196

def category?
  @type == 4
end

#childrenArray<Channel> Also known as: channels

Returns the children of this channel, if it is a category. Otherwise returns an empty array.

Returns:



379
380
381
382
383
# File 'lib/rubycord/data/channel.rb', line 379

def children
  return [] unless category?

  server.channels.select { |c| c.parent_id == id }
end

#create_group(user_ids) ⇒ Channel

Creates a Group channel

Parameters:

  • user_ids (Array<Integer>)

    Array of user IDs to add to the new group channel (Excluding the recipient of the PM channel).

Returns:

  • (Channel)

    the created channel.



758
759
760
761
762
763
764
# File 'lib/rubycord/data/channel.rb', line 758

def create_group(user_ids)
  raise "Attempted to create group channel on a non-pm channel!" unless pm?

  response = API::Channel.create_group(@bot.token, @id, user_ids.shift)
  channel = Channel.new(JSON.parse(response), @bot)
  channel.add_group_users(user_ids)
end

#create_webhook(name, avatar = nil, reason = nil) ⇒ Webhook

Creates a webhook in this channel

Parameters:

  • name (String)

    the default name of this webhook.

  • avatar (String) (defaults to: nil)

    the default avatar URL to give this webhook.

  • reason (String) (defaults to: nil)

    the reason for the webhook creation.

Returns:

  • (Webhook)

    the created webhook.

Raises:

  • (ArgumentError)

    if the channel isn't a text channel in a server.



811
812
813
814
815
816
817
# File 'lib/rubycord/data/channel.rb', line 811

def create_webhook(name, avatar = nil, reason = nil)
  raise ArgumentError, "Tried to create a webhook in a non-server channel" unless server
  raise ArgumentError, "Tried to create a webhook in a non-text channel" unless text?

  response = API::Channel.create_webhook(@bot.token, @id, name, avatar, reason)
  Webhook.new(JSON.parse(response), @bot)
end

#default_channel?true, false Also known as: default?

Returns whether or not this channel is the default channel.

Returns:

  • (true, false)

    whether or not this channel is the default channel



410
411
412
# File 'lib/rubycord/data/channel.rb', line 410

def default_channel?
  server.default_channel == self
end

#define_overwrite(overwrite) ⇒ Object #define_overwrite(thing, allow, deny) ⇒ Object

Defines a permission overwrite for this channel that sets the specified thing to the specified allow and deny permission sets, or change an existing one.

Overloads:

  • #define_overwrite(overwrite) ⇒ Object

    Parameters:

    • thing (Overwrite)

      an Overwrite object to apply to this channel

    • reason (String)

      The reason the for defining the overwrite.

  • #define_overwrite(thing, allow, deny) ⇒ Object

    Examples:

    Define a permission overwrite for a user that can then mention everyone and use TTS, but not create any invites

    allow = Rubycord::Permissions.new
    allow.can_mention_everyone = true
    allow.can_send_tts_messages = true
    
    deny = Rubycord::Permissions.new
    deny.can_create_instant_invite = true
    
    channel.define_overwrite(user, allow, deny)

    Parameters:

    • thing (User, Role)

      What to define an overwrite for.

    • allow (#bits, Permissions, Integer)

      The permission sets that should receive an allow override (i.e. a green checkmark on Discord)

    • deny (#bits, Permissions, Integer)

      The permission sets that should receive a deny override (i.e. a red cross on Discord)

    • reason (String)

      The reason the for defining the overwrite.



570
571
572
573
574
575
576
577
578
579
# File 'lib/rubycord/data/channel.rb', line 570

def define_overwrite(thing, allow = 0, deny = 0, reason: nil)
  unless thing.is_a? Overwrite
    allow_bits = allow.respond_to?(:bits) ? allow.bits : allow
    deny_bits = deny.respond_to?(:bits) ? deny.bits : deny

    thing = Overwrite.new thing, allow: allow_bits, deny: deny_bits
  end

  API::Channel.update_permission(@bot.token, @id, thing.id, thing.allow.bits, thing.deny.bits, thing.type, reason)
end

#delete(reason = nil) ⇒ Object

Permanently deletes this channel

Parameters:

  • reason (String) (defaults to: nil)

    The reason the for the channel deletion.



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

def delete(reason = nil)
  API::Channel.delete(@bot.token, @id, reason)
end

#delete_message(message) ⇒ Object

Deletes a message on this channel. Mostly useful in case a message needs to be deleted when only the ID is known

Parameters:



501
502
503
# File 'lib/rubycord/data/channel.rb', line 501

def delete_message(message)
  API::Channel.delete_message(@bot.token, @id, message.resolve_id)
end

#delete_messages(messages, strict = false, reason = nil) ⇒ Integer

Deletes a collection of messages

Parameters:

  • messages (Array<Message, String, Integer>)

    the messages (or message IDs) to delete. Total must be an amount between 2 and 100 (Discord limitation)

  • strict (true, false) (defaults to: false)

    Whether an error should be raised when a message is reached that is too old to be bulk deleted. If this is false only a warning message will be output to the console.

  • reason (String, nil) (defaults to: nil)

    The reason for deleting the messages

Returns:

  • (Integer)

    The amount of messages that were successfully deleted

Raises:

  • (ArgumentError)

    if the amount of messages is not a value between 2 and 100



702
703
704
705
706
707
# File 'lib/rubycord/data/channel.rb', line 702

def delete_messages(messages, strict = false, reason = nil)
  raise ArgumentError, "Can only delete between 2 and 100 messages!" unless messages.count.between?(2, 100)

  messages.map!(&:resolve_id)
  bulk_delete(messages, strict, reason)
end

#delete_overwrite(target, reason = nil) ⇒ Object

Deletes a permission overwrite for this channel

Parameters:



584
585
586
587
588
# File 'lib/rubycord/data/channel.rb', line 584

def delete_overwrite(target, reason = nil)
  raise "Tried deleting a overwrite for an invalid target" unless target.is_a?(Member) || target.is_a?(User) || target.is_a?(Role) || target.is_a?(Profile) || target.is_a?(Recipient) || target.respond_to?(:resolve_id)

  API::Channel.delete_permission(@bot.token, @id, target.resolve_id, reason)
end

#group?true, false

Returns whether or not this channel is a group channel.

Returns:

  • (true, false)

    whether or not this channel is a group channel.



191
192
193
# File 'lib/rubycord/data/channel.rb', line 191

def group?
  @type == 3
end

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

Retrieves some of this channel's message history.

Examples:

Count the number of messages in the last 50 messages that contain the letter 'e'.

message_count = channel.history(50).count {|message| message.content.include? "e"}

Get the last 10 messages before the provided message.

last_ten_messages = channel.history(10, message.id)

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.

  • around_id (Integer) (defaults to: nil)

    The ID of the message retrieval should start from, reading in both directions

Returns:

  • (Array<Message>)

    the retrieved messages.



630
631
632
633
# File 'lib/rubycord/data/channel.rb', line 630

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

#inspectObject

The default inspect method is overwritten to give more useful output.



888
889
890
# File 'lib/rubycord/data/channel.rb', line 888

def inspect
  "<Channel name=#{@name} id=#{@id} topic=\"#{@topic}\" type=#{@type} position=#{@position} server=#{@server || @server_id}>"
end

#invitesArray<Invite>

Requests a list of Invites to the channel.

Returns:

  • (Array<Invite>)

    invites to the channel.



830
831
832
833
834
835
# File 'lib/rubycord/data/channel.rb', line 830

def invites
  raise "Tried to request invites from a non-server channel" unless server

  invites = JSON.parse(API::Channel.invites(@bot.token, @id))
  invites.map { |invite_data| Invite.new(invite_data, @bot) }
end

#join_threadObject

Join this thread.



860
861
862
# File 'lib/rubycord/data/channel.rb', line 860

def join_thread
  @bot.join_thread(@id)
end

#leave_groupObject Also known as: leave

Leaves the group.



797
798
799
800
801
# File 'lib/rubycord/data/channel.rb', line 797

def leave_group
  raise "Attempted to leave a non-group channel!" unless group?

  API::Channel.leave_group(@bot.token, @id)
end

#leave_threadObject

Leave this thread



865
866
867
# File 'lib/rubycord/data/channel.rb', line 865

def leave_thread
  @bot.leave_thread(@id)
end

Returns a URL that a user can use to navigate to this channel in the client.

Returns:

  • (String)

    a URL that a user can use to navigate to this channel in the client



934
935
936
# File 'lib/rubycord/data/channel.rb', line 934

def link
  "https://discord.com/channels/#{@server_id || "@me"}/#{@channel.id}"
end

#load_message(message_id) ⇒ Message? Also known as: message

Returns a single message from this channel's history by ID.

Parameters:

  • message_id (Integer)

    The ID of the message to retrieve.

Returns:

  • (Message, nil)

    the retrieved message, or nil if it couldn't be found.



646
647
648
649
650
651
652
653
# File 'lib/rubycord/data/channel.rb', line 646

def load_message(message_id)
  raise ArgumentError, "message_id cannot be nil" if message_id.nil?

  response = API::Channel.message(@bot.token, @id, message_id)
  Message.new(JSON.parse(response), @bot)
rescue Rubycord::Errors::UnknownMessage
  nil
end

#make_invite(max_age = 0, max_uses = 0, temporary = false, unique = false, reason = nil) ⇒ Invite Also known as: invite

Creates a new invite to this channel.

Parameters:

  • max_age (Integer) (defaults to: 0)

    How many seconds this invite should last.

  • max_uses (Integer) (defaults to: 0)

    How many times this invite should be able to be used.

  • temporary (true, false) (defaults to: false)

    Whether membership should be temporary (kicked after going offline).

  • unique (true, false) (defaults to: false)

    If true, Discord will always send a unique invite instead of possibly re-using a similar one

  • reason (String) (defaults to: nil)

    The reason the for the creation of this invite.

Returns:

  • (Invite)

    the created invite.



738
739
740
741
# File 'lib/rubycord/data/channel.rb', line 738

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

#member_overwritesOverwrite

Returns any member-type permission overwrites on this channel.

Returns:

  • (Overwrite)

    any member-type permission overwrites on this channel



400
401
402
# File 'lib/rubycord/data/channel.rb', line 400

def member_overwrites
  permission_overwrites :member
end

#membersObject

Members in the thread.



870
871
872
# File 'lib/rubycord/data/channel.rb', line 870

def members
  @bot.thread_members[@id].collect { |id| @server_id ? @bot.member(@server_id, id) : @bot.user(id) }
end

#mentionString

Returns a string that will mention the channel as a clickable link on Discord.

Returns:

  • (String)

    a string that will mention the channel as a clickable link on Discord.



100
101
102
# File 'lib/rubycord/data/channel.rb', line 100

def mention
  "<##{@id}>"
end

#news?true, false

Returns whether or not this channel is a news channel.

Returns:

  • (true, false)

    whether or not this channel is a news channel.



201
202
203
# File 'lib/rubycord/data/channel.rb', line 201

def news?
  @type == 5
end

#news_thread?true, false

Returns whether or not this channel is a news thread.

Returns:

  • (true, false)

    whether or not this channel is a news thread.



211
212
213
# File 'lib/rubycord/data/channel.rb', line 211

def news_thread?
  @type == 10
end

#permission_overwritesHash<Integer => Overwrite> #permission_overwrites(type) ⇒ Array<Overwrite> Also known as: overwrites

This channel's permission overwrites

Overloads:

  • #permission_overwritesHash<Integer => Overwrite>

    The overwrites represented as a hash of role/user ID to an Overwrite object

    Returns:

  • #permission_overwrites(type) ⇒ Array<Overwrite>

    Return an array of a certain type of overwrite

    Parameters:

    • type (Symbol)

      the kind of overwrite to return

    Returns:



333
334
335
336
337
# File 'lib/rubycord/data/channel.rb', line 333

def permission_overwrites(type = nil)
  return @permission_overwrites unless type

  @permission_overwrites.values.select { |e| e.type == type }
end

#permission_overwrites=(overwrites) ⇒ Object

Bulk sets this channels permission overwrites

Parameters:



343
344
345
# File 'lib/rubycord/data/channel.rb', line 343

def permission_overwrites=(overwrites)
  update_channel_data(permission_overwrites: overwrites)
end

#pinsArray<Message>

Requests all pinned messages in a channel.

Returns:

  • (Array<Message>)

    the received messages.



659
660
661
662
# File 'lib/rubycord/data/channel.rb', line 659

def pins
  msgs = API::Channel.pinned_messages(@bot.token, @id)
  JSON.parse(msgs).map { |msg| Message.new(msg, @bot) }
end

#pm?true, false

Returns whether or not this channel is a PM channel.

Returns:

  • (true, false)

    whether or not this channel is a PM channel.



181
182
183
# File 'lib/rubycord/data/channel.rb', line 181

def pm?
  @type == 1
end

#private?true, false

Returns whether or not this channel is a PM or group channel.

Returns:

  • (true, false)

    whether or not this channel is a PM or group channel.



95
96
97
# File 'lib/rubycord/data/channel.rb', line 95

def private?
  pm? || group?
end

#private_thread?true, false

Returns whether or not this channel is a private thread.

Returns:

  • (true, false)

    whether or not this channel is a private thread.



221
222
223
# File 'lib/rubycord/data/channel.rb', line 221

def private_thread?
  @type == 12
end

#prune(amount, strict = false, reason = nil) {|message| ... } ⇒ Integer

Delete the last N messages on this channel.

Examples:

Pruning messages from a specific user ID

channel.prune(100) { |m| m.author.id == 83283213010599936 }

Parameters:

  • amount (Integer)

    The amount of message history to consider for pruning. Must be a value between 2 and 100 (Discord limitation)

  • strict (true, false) (defaults to: false)

    Whether an error should be raised when a message is reached that is too old to be bulk deleted. If this is false only a warning message will be output to the console.

  • reason (String, nil) (defaults to: nil)

    The reason for pruning

Yields:

  • (message)

    Yields each message in this channels history for filtering the messages to delete

Returns:

  • (Integer)

    The amount of messages that were successfully deleted

Raises:

  • (ArgumentError)

    if the amount of messages is not a value between 2 and 100



674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
# File 'lib/rubycord/data/channel.rb', line 674

def prune(amount, strict = false, reason = nil, &block)
  raise ArgumentError, "Can only delete between 1 and 100 messages!" unless amount.between?(1, 100)

  messages =
    if block
      history(amount).select(&block).map(&:id)
    else
      history_ids(amount)
    end

  case messages.size
  when 0
    0
  when 1
    API::Channel.delete_message(@bot.token, @id, messages.first, reason)
    1
  else
    bulk_delete(messages, strict, reason)
  end
end

#public_thread?true, false

Returns whether or not this channel is a public thread.

Returns:

  • (true, false)

    whether or not this channel is a public thread.



216
217
218
# File 'lib/rubycord/data/channel.rb', line 216

def public_thread?
  @type == 11
end

#recipientRecipient?

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

Returns:

  • (Recipient, nil)

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



105
106
107
# File 'lib/rubycord/data/channel.rb', line 105

def recipient
  @recipients.first if pm?
end

#remove_group_users(user_ids) ⇒ Channel Also known as: remove_group_user

Removes a user from a group channel.

Parameters:

Returns:



784
785
786
787
788
789
790
791
792
# File 'lib/rubycord/data/channel.rb', line 784

def remove_group_users(user_ids)
  raise "Attempted to remove a user from a non-group channel!" unless group?

  user_ids = [user_ids] unless user_ids.is_a? Array
  user_ids.each do |user_id|
    API::Channel.remove_group_user(@bot.token, @id, user_id.resolve_id)
  end
  self
end

#remove_member(member) ⇒ Object

Parameters:

  • member (Member, Integer, String)

    The member, or ID of the member, to remove from a thread.



881
882
883
# File 'lib/rubycord/data/channel.rb', line 881

def remove_member(member)
  @bot.remove_thread_member(@id, member)
end

#role_overwritesOverwrite

Returns any role-type permission overwrites on this channel.

Returns:

  • (Overwrite)

    any role-type permission overwrites on this channel



405
406
407
# File 'lib/rubycord/data/channel.rb', line 405

def role_overwrites
  permission_overwrites :role
end

#send_embed(message = "", embed = nil, attachments = nil, tts = false, allowed_mentions = nil, message_reference = nil, components = nil) {|embed| ... } ⇒ Message

Convenience method to send a message with an embed.

Examples:

Send a message with an embed

channel.send_embed do |embed|
  embed.title = 'The Ruby logo'
  embed.image = Rubycord::Webhooks::EmbedImage.new(url: 'https://www.ruby-lang.org/images/header-ruby-logo.png')
end

Parameters:

  • message (String) (defaults to: "")

    The message that should be sent along with the embed. If this is the empty string, only the embed will be shown.

  • embed (Rubycord::Webhooks::Embed, nil) (defaults to: nil)

    The embed to start the building process with, or nil if one should be created anew.

  • attachments (Array<File>) (defaults to: nil)

    Files that can be referenced in embeds via attachment://file.png

  • tts (true, false) (defaults to: false)

    Whether or not this message should be sent using Discord text-to-speech.

  • allowed_mentions (Hash, Rubycord::AllowedMentions, false, nil) (defaults to: nil)

    Mentions that are allowed to ping on this message. false disables all pings

  • message_reference (Message, String, Integer, nil) (defaults to: nil)

    The message, or message ID, to reply to if any.

  • components (View, Array<Hash>) (defaults to: nil)

    Interaction components to associate with this message.

Yields:

  • (embed)

    Yields the embed to allow for easy building inside a block.

Yield Parameters:

Returns:

  • (Message)

    The resulting message.



465
466
467
468
469
470
471
472
# File 'lib/rubycord/data/channel.rb', line 465

def send_embed(message = "", embed = nil, attachments = nil, tts = false, allowed_mentions = nil, message_reference = nil, components = nil)
  embed ||= Rubycord::Webhooks::Embed.new
  view = Rubycord::Webhooks::View.new

  yield(embed, view) if block_given?

  send_message(message, tts, embed, attachments, allowed_mentions, message_reference, components || view.to_a)
end

#send_file(file, caption: nil, tts: false, filename: nil, spoiler: nil) ⇒ Object

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

Examples:

Send a file from disk

channel.send_file(File.open('rubytaco.png', 'r'))

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)

  • caption (string) (defaults to: nil)

    The caption for the file.

  • tts (true, false) (defaults to: false)

    Whether or not this file's caption should be sent using Discord text-to-speech.

  • filename (String) (defaults to: nil)

    Overrides the filename of the uploaded file

  • spoiler (true, false) (defaults to: nil)

    Whether or not this file should appear as a spoiler.



495
496
497
# File 'lib/rubycord/data/channel.rb', line 495

def send_file(file, caption: nil, tts: false, filename: nil, spoiler: nil)
  @bot.send_file(@id, file, caption: caption, tts: tts, filename: filename, spoiler: spoiler)
end

#send_message(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil) ⇒ Message Also known as: send

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.

  • tts (true, false) (defaults to: false)

    Whether or not this message should be sent using Discord text-to-speech.

  • embed (Hash, Rubycord::Webhooks::Embed, nil) (defaults to: nil)

    The rich embed to append to this message.

  • attachments (Array<File>) (defaults to: nil)

    Files that can be referenced in embeds via attachment://file.png

  • allowed_mentions (Hash, Rubycord::AllowedMentions, false, nil) (defaults to: nil)

    Mentions that are allowed to ping on this message. false disables all pings

  • message_reference (Message, String, Integer, nil) (defaults to: nil)

    The message, or message ID, to reply to if any.

  • components (View, Array<Hash>) (defaults to: nil)

    Interaction components to associate with this message.

Returns:

  • (Message)

    the message that was sent.



430
431
432
# File 'lib/rubycord/data/channel.rb', line 430

def send_message(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil)
  @bot.send_message(@id, content, tts, embed, attachments, allowed_mentions, message_reference, components)
end

#send_multiple(content) ⇒ Object

Sends multiple messages to a channel

Parameters:

  • content (Array<String>)

    The messages to send.



476
477
478
# File 'lib/rubycord/data/channel.rb', line 476

def send_multiple(content)
  content.each { |e| send_message(e) }
end

#send_temporary_message(content, timeout, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil) ⇒ Object

Sends a temporary 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.

  • timeout (Float)

    The amount of time in seconds after which the message sent will be deleted.

  • tts (true, false) (defaults to: false)

    Whether or not this message should be sent using Discord text-to-speech.

  • embed (Hash, Rubycord::Webhooks::Embed, nil) (defaults to: nil)

    The rich embed to append to this message.

  • attachments (Array<File>) (defaults to: nil)

    Files that can be referenced in embeds via attachment://file.png

  • allowed_mentions (Hash, Rubycord::AllowedMentions, false, nil) (defaults to: nil)

    Mentions that are allowed to ping on this message. false disables all pings

  • message_reference (Message, String, Integer, nil) (defaults to: nil)

    The message, or message ID, to reply to if any.

  • components (View, Array<Hash>) (defaults to: nil)

    Interaction components to associate with this message.



445
446
447
# File 'lib/rubycord/data/channel.rb', line 445

def send_temporary_message(content, timeout, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil)
  @bot.send_temporary_message(@id, content, timeout, tts, embed, attachments, allowed_mentions, message_reference, components)
end

#serverServer?

Returns the server this channel is on. If this channel is a PM channel, it will be nil.

Returns:

  • (Server, nil)

    the server this channel is on. If this channel is a PM channel, it will be nil.

Raises:



165
166
167
168
169
170
171
172
173
# File 'lib/rubycord/data/channel.rb', line 165

def server
  return @server if @server
  return nil if private?

  @server = @bot.server(@server_id)
  raise Rubycord::Errors::NoPermission, "The bot does not have access to this server" unless @server

  @server
end

#slowmode?true, false

Returns whether or not this channel has slowmode enabled.

Returns:

  • (true, false)

    whether or not this channel has slowmode enabled



417
418
419
# File 'lib/rubycord/data/channel.rb', line 417

def slowmode?
  @rate_limit_per_user != 0
end

#sort_after(other = nil, lock_permissions = false) ⇒ Object

Sorts this channel's position to follow another channel.

Parameters:

  • other (Channel, String, Integer, nil) (defaults to: nil)

    The channel, or its ID, below which this channel should be sorted. If the given channel is a category, this channel will be sorted at the top of that category. If it is nil, the channel will be sorted at the top of the channel list.

  • lock_permissions (true, false) (defaults to: false)

    Whether the channel's permissions should be synced to the category's

Raises:

  • (TypeError)


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/rubycord/data/channel.rb', line 254

def sort_after(other = nil, lock_permissions = false)
  raise TypeError, "other must be one of Channel, NilClass, String, or Integer" unless other.is_a?(Channel) || other.nil? || other.respond_to?(:resolve_id)

  other = @bot.channel(other.resolve_id) if other

  # Container for the API request payload
  move_argument = []

  if other
    raise ArgumentError, "Can only sort a channel after a channel of the same type!" unless other.category? || (@type == other.type)

    raise ArgumentError, "Can only sort a channel after a channel in the same server!" unless other.server == server

    # Store `others` parent (or if `other` is a category itself)
    parent = if category? && other.category?
      # If we're sorting two categories, there is no new parent
      nil
    elsif other.category?
      # `other` is the category this channel will be moved into
      other
    else
      # `other`'s parent is the category this channel will be
      # moved into (if it exists)
      other.parent
    end
  end

  # Collect and sort the IDs within the context (category or not) that we
  # need to form our payload with
  ids = if parent
    parent.children
  else
    server.channels.reject(&:parent_id).select { |c| c.type == @type }
  end.sort_by(&:position).map(&:id)

  # Move our channel ID after the target ID by deleting it,
  # getting the index of `other`, and inserting it after.
  ids.delete(@id) if ids.include?(@id)
  index = other ? (ids.index { |c| c == other.id } || -1) + 1 : 0
  ids.insert(index, @id)

  # Generate `move_argument`, making the positions in order from how
  # we have sorted them in the above logic
  ids.each_with_index do |id, pos|
    # These keys are present in each element
    hash = {id: id, position: pos}

    # Conditionally add `lock_permissions` and `parent_id` if we're
    # iterating past ourselves
    if id == @id
      hash[:lock_permissions] = true if lock_permissions
      hash[:parent_id] = parent.nil? ? nil : parent.id
    end

    # Add it to the stack
    move_argument << hash
  end

  API::Server.update_channel_positions(@bot.token, @server_id, move_argument)
end

#split_send(content) ⇒ Object

Splits a message into chunks whose length is at most the Discord character limit, then sends them individually. Useful for sending long messages, but be wary of rate limits!



482
483
484
485
# File 'lib/rubycord/data/channel.rb', line 482

def split_send(content)
  send_multiple(Rubycord.split_message(content))
  nil
end

#start_thread(name, auto_archive_duration, message: nil, type: 11) ⇒ Channel

Start a thread.

Parameters:

  • name (String)

    The name of the thread.

  • auto_archive_duration (60, 1440, 4320, 10080)

    How long before a thread is automatically archived.

  • message (Message, Integer, String) (defaults to: nil)

    The message to reference when starting this thread.

  • type (Symbol, Integer) (defaults to: 11)

    The type of thread to create. Can be a key from TYPES or the value.

Returns:



844
845
846
847
848
849
850
851
852
853
854
855
# File 'lib/rubycord/data/channel.rb', line 844

def start_thread(name, auto_archive_duration, message: nil, type: 11)
  message_id = message&.id || message
  type = TYPES[type] || type

  data = if message
    API::Channel.start_thread_with_message(@bot.token, @id, message_id, name, auto_archive_duration)
  else
    API::Channel.start_thread_without_message(@bot.token, @id, name, auto_archive_duration, type)
  end

  Channel.new(JSON.parse(data), @bot, @server)
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)

Examples:

Send a typing indicator for the bot in a given channel.

channel.start_typing()


750
751
752
# File 'lib/rubycord/data/channel.rb', line 750

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

#store?true, false

Returns whether or not this channel is a store channel.

Returns:

  • (true, false)

    whether or not this channel is a store channel.



206
207
208
# File 'lib/rubycord/data/channel.rb', line 206

def store?
  @type == 6
end

#sync_overwritesObject Also known as: sync

Syncs this channels overwrites with its parent category

Raises:

  • (RuntimeError)

    if this channel is not in a category



360
361
362
363
364
# File 'lib/rubycord/data/channel.rb', line 360

def sync_overwrites
  raise "Cannot sync overwrites on a channel with no parent category" unless parent

  self.permission_overwrites = parent.permission_overwrites
end

#synchronized?true, ... Also known as: synced?

Returns whether this channels permissions match the permission overwrites of the category that it's in, or nil if it is not in a category.

Returns:

  • (true, false, nil)

    whether this channels permissions match the permission overwrites of the category that it's in, or nil if it is not in a category



369
370
371
372
373
# File 'lib/rubycord/data/channel.rb', line 369

def synchronized?
  return unless parent

  permission_overwrites == parent.permission_overwrites
end

#text?true, false

Returns whether or not this channel is a text channel.

Returns:

  • (true, false)

    whether or not this channel is a text channel



176
177
178
# File 'lib/rubycord/data/channel.rb', line 176

def text?
  @type.zero?
end

#text_channelsArray<Channel>

Returns the text channels in this category, if it is a category channel. Otherwise returns an empty array.

Returns:



389
390
391
# File 'lib/rubycord/data/channel.rb', line 389

def text_channels
  children.select(&:text?)
end

#thread?true, false

Returns whether or not this channel is a thread.

Returns:

  • (true, false)

    whether or not this channel is a thread.



226
227
228
# File 'lib/rubycord/data/channel.rb', line 226

def thread?
  news_thread? || public_thread? || private_thread?
end

#usersArray<Member>

The list of users currently in this channel. For a voice channel, it will return all the members currently in that channel. For a text channel, it will return all online members that have permission to read it.

Returns:

  • (Array<Member>)

    the users in this channel



609
610
611
612
613
614
615
# File 'lib/rubycord/data/channel.rb', line 609

def users
  if text?
    server.online_members(include_idle: true).select { |u| u.can_view_channel? self }
  elsif voice?
    server.voice_states.filter_map { |id, voice_state| server.member(id) if !voice_state.voice_channel.nil? && voice_state.voice_channel.id == @id }
  end
end

#voice?true, false

Returns whether or not this channel is a voice channel.

Returns:

  • (true, false)

    whether or not this channel is a voice channel.



186
187
188
# File 'lib/rubycord/data/channel.rb', line 186

def voice?
  @type == 2
end

#voice_channelsArray<Channel>

Returns the voice channels in this category, if it is a category channel. Otherwise returns an empty array.

Returns:



395
396
397
# File 'lib/rubycord/data/channel.rb', line 395

def voice_channels
  children.select(&:voice?)
end

#webhooksArray<Webhook>

Requests a list of Webhooks on the channel.

Returns:

  • (Array<Webhook>)

    webhooks on the channel.



821
822
823
824
825
826
# File 'lib/rubycord/data/channel.rb', line 821

def webhooks
  raise "Tried to request webhooks from a non-server channel" unless server

  webhooks = JSON.parse(API::Channel.webhooks(@bot.token, @id))
  webhooks.map { |webhook_data| Webhook.new(webhook_data, @bot) }
end