Class: Discordrb::Channel

Inherits:
Object
  • Object
show all
Includes:
IDObject
Defined in:
lib/discordrb/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,
  media: 16
}.freeze
FLAGS =

Map of channel flags.

{
  pinned: 1 << 1,
  require_tag: 1 << 4,
  hide_download_options: 1 << 15
}.freeze
FORUM_LAYOUTS =

Map of forum layouts.

{
  not_set: 0,
  list_view: 1,
  gallery_view: 2
}.freeze
FORUM_SORT_ORDERS =

Map of forum sort orders.

{
  latest_activity: 0,
  creation_time: 1
}.freeze
VIDEO_QUALITY_MODES =

Map of video quality modes.

{
  auto: 1,
  full: 2
}.freeze

Instance Attribute Summary collapse

Attributes included from IDObject

#id

Threads collapse

Instance Method Summary collapse

Methods included from IDObject

#==, 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.



104
105
106
# File 'lib/discordrb/data/channel.rb', line 104

def archive_timestamp
  @archive_timestamp
end

#archivedtrue, ... (readonly) Also known as: archived?

Returns Whether or not this thread is archived.

Returns:

  • (true, false, nil)

    Whether or not this thread is archived.



97
98
99
# File 'lib/discordrb/data/channel.rb', line 97

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.



101
102
103
# File 'lib/discordrb/data/channel.rb', line 101

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



77
78
79
# File 'lib/discordrb/data/channel.rb', line 77

def bitrate
  @bitrate
end

#default_auto_archive_durationInteger? (readonly)

Returns The default client-side duration before a thread is automatically hidden.

Returns:

  • (Integer, nil)

    The default client-side duration before a thread is automatically hidden.



142
143
144
# File 'lib/discordrb/data/channel.rb', line 142

def default_auto_archive_duration
  @default_auto_archive_duration
end

#default_forum_layoutInteger? (readonly)

Returns The default layout used to display threads in this forum or media channel.

Returns:

  • (Integer, nil)

    The default layout used to display threads in this forum or media channel.



148
149
150
# File 'lib/discordrb/data/channel.rb', line 148

def default_forum_layout
  @default_forum_layout
end

#default_sort_orderInteger? (readonly)

Returns The default sorting order for threads in this forum or media channel.

Returns:

  • (Integer, nil)

    The default sorting order for threads in this forum or media channel.



145
146
147
# File 'lib/discordrb/data/channel.rb', line 145

def default_sort_order
  @default_sort_order
end

#default_thread_rate_limit_per_userInteger? (readonly)

Returns The initial slowmode rate set on newly created threads in this channel.

Returns:

  • (Integer, nil)

    The initial slowmode rate set on newly created threads in this channel.



151
152
153
# File 'lib/discordrb/data/channel.rb', line 151

def default_thread_rate_limit_per_user
  @default_thread_rate_limit_per_user
end

#flagsInteger (readonly)

Returns The flags set on this channel combined as a bitfield.

Returns:

  • (Integer)

    The flags set on this channel combined as a bitfield.



132
133
134
# File 'lib/discordrb/data/channel.rb', line 132

def flags
  @flags
end

#invitabletrue, ... (readonly) Also known as: invitable?

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

Returns:

  • (true, false, nil)

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



118
119
120
# File 'lib/discordrb/data/channel.rb', line 118

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.



111
112
113
# File 'lib/discordrb/data/channel.rb', line 111

def join_timestamp
  @join_timestamp
end

#last_message_idInteger? (readonly)

Returns The ID of the last message sent in this channel. This may not point to a valid message.

Returns:

  • (Integer, nil)

    The ID of the last message sent in this channel. This may not point to a valid message.



125
126
127
# File 'lib/discordrb/data/channel.rb', line 125

def last_message_id
  @last_message_id
end

#last_pin_timestampTime? (readonly)

Returns The time at when the last pinned message was pinned in this channel.

Returns:

  • (Time, nil)

    The time at when the last pinned message was pinned in this channel.



122
123
124
# File 'lib/discordrb/data/channel.rb', line 122

def last_pin_timestamp
  @last_pin_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.



107
108
109
# File 'lib/discordrb/data/channel.rb', line 107

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.



94
95
96
# File 'lib/discordrb/data/channel.rb', line 94

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.



114
115
116
# File 'lib/discordrb/data/channel.rb', line 114

def member_flags
  @member_flags
end

#message_countInteger? (readonly)

Returns An approximate count of messages sent in a thread, excluding deleted messages.

Returns:

  • (Integer, nil)

    An approximate count of messages sent in a thread, excluding deleted messages.



91
92
93
# File 'lib/discordrb/data/channel.rb', line 91

def message_count
  @message_count
end

#nameString

Returns this channel's name.

Returns:

  • (String)

    this channel's name.



56
57
58
# File 'lib/discordrb/data/channel.rb', line 56

def name
  @name
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.



68
69
70
# File 'lib/discordrb/data/channel.rb', line 68

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.



60
61
62
# File 'lib/discordrb/data/channel.rb', line 60

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



84
85
86
# File 'lib/discordrb/data/channel.rb', line 84

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.



87
88
89
# File 'lib/discordrb/data/channel.rb', line 87

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



71
72
73
# File 'lib/discordrb/data/channel.rb', line 71

def recipients
  @recipients
end

#topicString

Returns the channel's topic.

Returns:

  • (String)

    the channel's topic



74
75
76
# File 'lib/discordrb/data/channel.rb', line 74

def topic
  @topic
end

#total_message_sentInteger (readonly) Also known as: total_messages_sent

Returns An approximate count of messages sent in this thread, including deleted messages.

Returns:

  • (Integer)

    An approximate count of messages sent in this thread, including deleted messages.



128
129
130
# File 'lib/discordrb/data/channel.rb', line 128

def total_message_sent
  @total_message_sent
end

#typeInteger (readonly)

Returns the type of this channel.

Returns:

  • (Integer)

    the type of this channel

See Also:



64
65
66
# File 'lib/discordrb/data/channel.rb', line 64

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.



80
81
82
# File 'lib/discordrb/data/channel.rb', line 80

def user_limit
  @user_limit
end

#video_quality_modeInteger? (readonly)

Returns The video quality mode of this voice or stage channel.

Returns:

  • (Integer, nil)

    The video quality mode of this voice or stage channel.



139
140
141
# File 'lib/discordrb/data/channel.rb', line 139

def video_quality_mode
  @video_quality_mode
end

#voice_regionString? (readonly)

Returns The ID of the RTC voice region for this voice or stage channel. A region of nil means the the voice region will automatically be determined by Discord.

Returns:

  • (String, nil)

    The ID of the RTC voice region for this voice or stage channel. A region of nil means the the voice region will automatically be determined by Discord.



136
137
138
# File 'lib/discordrb/data/channel.rb', line 136

def voice_region
  @voice_region
end

Instance Method Details

#add_member(member) ⇒ Object

Add a member to the thread

Parameters:



1108
1109
1110
# File 'lib/discordrb/data/channel.rb', line 1108

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

#add_tags(tags, reason: nil) ⇒ Object Also known as: add_tag

Add one or more tags to this thread channel.

Parameters:



1061
1062
1063
1064
1065
# File 'lib/discordrb/data/channel.rb', line 1061

def add_tags(tags, reason: nil)
  raise 'Cannot add tags to this channel' unless parent&.thread_only?

  modify(tags: @applied_tags + Array(tags).map(&:resolve_id), reason: reason)
end

#await(key, attributes = {}, &block) ⇒ 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:



884
885
886
# File 'lib/discordrb/data/channel.rb', line 884

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

#await!(attributes = {}, &block) ⇒ 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:



891
892
893
# File 'lib/discordrb/data/channel.rb', line 891

def await!(attributes = {}, &block)
  @bot.add_await!(Discordrb::Events::MessageEvent, { in: @id }.merge(attributes), &block)
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



333
334
335
# File 'lib/discordrb/data/channel.rb', line 333

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



342
343
344
345
346
347
# File 'lib/discordrb/data/channel.rb', line 342

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

  modify(parent: channel)
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.



272
273
274
# File 'lib/discordrb/data/channel.rb', line 272

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:



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

def children
  return [] unless category?

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

#create_tag(name:, moderated:, emoji: nil, reason: nil) ⇒ nil

Create a tag in this forum or media channel.

Parameters:

  • name (String)

    The 1-20 character name of the tag to create.

  • moderated (true, false)

    Whether or not the tag should be moderated.

  • emoji (Emoji, Integer, String, nil) (defaults to: nil)

    An optional emoji to set for the tag.

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

    The reason to show in the audit log for creating the tag.

Returns:

  • (nil)


1085
1086
1087
# File 'lib/discordrb/data/channel.rb', line 1085

def create_tag(name:, moderated:, emoji: nil, reason: nil)
  update_tags({ name:, moderated:, **Emoji.build_emoji_hash(emoji) }, reason) if thread_only?
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.



924
925
926
927
928
929
# File 'lib/discordrb/data/channel.rb', line 924

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

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

#creation_timeTime?

Get the time at when this channel was created at.

Returns:

  • (Time, nil)

    The time at when the channel was created at.



425
426
427
428
429
# File 'lib/discordrb/data/channel.rb', line 425

def creation_time
  return @create_timestamp if @create_timestamp

  Time.at(((@id >> 22) + Discordrb::DISCORD_EPOCH) / 1000.0)
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



526
527
528
# File 'lib/discordrb/data/channel.rb', line 526

def default_channel?
  server.default_channel == self
end

#default_reactionObject



1029
1030
1031
# File 'lib/discordrb/data/channel.rb', line 1029

def default_reaction
  @default_reaction.is_a?(Integer) ? server.emojis[@default_reaction] : @default_reaction
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 = Discordrb::Permissions.new
    allow.can_mention_everyone = true
    allow.can_send_tts_messages = true
    
    deny = Discordrb::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.



734
735
736
737
738
739
740
741
742
743
# File 'lib/discordrb/data/channel.rb', line 734

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.



671
672
673
# File 'lib/discordrb/data/channel.rb', line 671

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:



665
666
667
# File 'lib/discordrb/data/channel.rb', line 665

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



866
867
868
869
870
871
# File 'lib/discordrb/data/channel.rb', line 866

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:



748
749
750
751
752
# File 'lib/discordrb/data/channel.rb', line 748

def delete_overwrite(target, reason = nil)
  raise 'Tried deleting a overwrite for an invalid target' unless target.respond_to?(:resolve_id)

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

#directory?true, false

Returns whether or not this channel is a directory channel.

Returns:

  • (true, false)

    whether or not this channel is a directory channel.



312
313
314
# File 'lib/discordrb/data/channel.rb', line 312

def directory?
  @type == 14
end

#follow(target, reason: nil) ⇒ Integer

Follow the announcement (news) channel to send crossposted messages to a target channel.

Parameters:

  • target (Channel, Integer, String)

    The target channel to send crossposted messages to.

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

    The audit log reason shown for the created webhook in the target channel.

Returns:

  • (Integer)

    the ID of the created webhook in the target channel.



953
954
955
956
957
# File 'lib/discordrb/data/channel.rb', line 953

def follow(target, reason: nil)
  raise 'Cannot follow a non-announcement channel' unless news?

  JSON.parse(API::Channel.follow_channel(@bot.token, @id, target.resolve_id, reason))['webhook_id'].to_i
end

#forum?true, false

Returns whether or not this channel is a forum channel.

Returns:

  • (true, false)

    whether or not this channel is a forum channel.



317
318
319
# File 'lib/discordrb/data/channel.rb', line 317

def forum?
  @type == 15
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.



267
268
269
# File 'lib/discordrb/data/channel.rb', line 267

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.



778
779
780
781
# File 'lib/discordrb/data/channel.rb', line 778

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.



1210
1211
1212
# File 'lib/discordrb/data/channel.rb', line 1210

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.



942
943
944
945
946
947
# File 'lib/discordrb/data/channel.rb', line 942

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.



1092
1093
1094
# File 'lib/discordrb/data/channel.rb', line 1092

def join_thread
  @bot.join_thread(@id)
end

#last_messageMessage, ...

Returns the last message or forum post created in this channel.

Returns:

  • (Message, Channel, nil)

    the last message sent in this channel, the most recent forum post if this is a forum or media channel, or nil.



962
963
964
965
966
967
968
969
970
# File 'lib/discordrb/data/channel.rb', line 962

def last_message
  return unless @last_message_id

  if forum? || media?
    @bot.channel(@last_message_id)
  else
    load_message(@last_message_id)
  end
end

#leave_threadObject

Leave this thread



1097
1098
1099
# File 'lib/discordrb/data/channel.rb', line 1097

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



1289
1290
1291
# File 'lib/discordrb/data/channel.rb', line 1289

def link
  "https://discord.com/channels/#{@server_id || '@me'}/#{@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.



794
795
796
797
798
799
800
801
# File 'lib/discordrb/data/channel.rb', line 794

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 Discordrb::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.



902
903
904
905
# File 'lib/discordrb/data/channel.rb', line 902

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

#media?true, false

Returns whether or not this channel is a media channel.

Returns:

  • (true, false)

    whether or not this channel is a media channel.



322
323
324
# File 'lib/discordrb/data/channel.rb', line 322

def media?
  @type == 16
end

#member_overwritesOverwrite

Returns any member-type permission overwrites on this channel.

Returns:

  • (Overwrite)

    any member-type permission overwrites on this channel



516
517
518
# File 'lib/discordrb/data/channel.rb', line 516

def member_overwrites
  permission_overwrites :member
end

#membersObject

Members in the thread.



1102
1103
1104
# File 'lib/discordrb/data/channel.rb', line 1102

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.



159
160
161
# File 'lib/discordrb/data/channel.rb', line 159

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

#modify(name: :undef, type: :undef, topic: :undef, nsfw: :undef, rate_limit_per_user: :undef, bitrate: :undef, user_limit: :undef, permission_overwrites: :undef, parent: :undef, voice_region: :undef, video_quality_mode: :undef, default_auto_archive_duration: :undef, flags: :undef, tags: :undef, default_reaction: :undef, default_sort_order: :undef, default_forum_layout: :undef, archived: :undef, locked: :undef, invitable: :undef, add_flags: :undef, remove_flags: :undef, position: :undef, auto_archive_duration: :undef, default_thread_rate_limit_per_user: :undef, reason: nil) ⇒ nil

Modify the properties of the channel.

Parameters:

  • name (String) (defaults to: :undef)

    The new 1-100 character name of the channel.

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

    The new type of the channel. You can only convert between text and announcement channels.

  • topic (String, nil) (defaults to: :undef)

    The 0-1024 character topic of the channel; 0-4096 characters for forum channels.

  • nsfw (true, false, nil) (defaults to: :undef)

    Whether or not the channel should be marked as age-restricted.

  • rate_limit_per_user (Integer, nil) (defaults to: :undef)

    The new slowmode-rate of the channel; between 0-21600 (in seconds).

  • bitrate (Integer, nil) (defaults to: :undef)

    The new bitrate of the voice or stage channel; minimum of 8000 (in bits).

  • user_limit (Integer, nil) (defaults to: :undef)

    The maximum number of users who can join the voice or stage channel; 0 for no limit.

  • permission_overwrites (Array<Overwrite, Hash, #to_hash>, nil) (defaults to: :undef)

    The new permission overwrites to set for the channel.

  • parent (Channel, Integer, String, nil) (defaults to: :undef)

    The new category channel to set, or nil to orphan the chnanel.

  • voice_region (VoiceRegion, String, nil) (defaults to: :undef)

    The new voice region to set for the voice or stage channel.

  • video_quality_mode (Symbol, Integer, nil) (defaults to: :undef)

    The new camera video quality mode to set for the voice or stage channel.

  • default_auto_archive_duration (Integer, nil) (defaults to: :undef)

    The default client-side duration before a thread is archived due to inactivity.

  • flags (Symbol, Integer, Array<Symbol, Integer>) (defaults to: :undef)

    The flags to set for the channel.

  • tags (Array<ChannelTag, #to_h, #resolve_id>) (defaults to: :undef)

    The tags to set on the thread channel, or the new tags that will be available in the forum channel.

  • default_reaction (Integer, String, Emoji, nil) (defaults to: :undef)

    The emoji to display on threads created in the forum channel.

  • default_sort_order (Integer, Symbol, nil) (defaults to: :undef)

    The default order used to order threads in the forum channel.

  • default_forum_layout (Integer, Symbol) (defaults to: :undef)

    The default layout type used to display threads in the forum channel.

  • archived (true, false) (defaults to: :undef)

    Whether or not the thread should be archived.

  • locked (true, false) (defaults to: :undef)

    Whether or not the thread should be locked.

  • invitable (true, false) (defaults to: :undef)

    Whether or not non-moderators should be able to add other non-moderators to the private thread.

  • add_flags (Symbol, Integer, Array<Symbol, Integer>) (defaults to: :undef)

    The flags to add to the channel. Mutually exclusive with flags:.

  • remove_flags (Symbol, Integer, Array<Symbol, Integer>) (defaults to: :undef)

    The flags to remove from the channel. Mutually exclusive with flags:.

  • position (Integer, nil) (defaults to: :undef)

    The new sorting position of the channel. Generally, this parameter should not be used. Please use #sort_after instead.

  • auto_archive_duration (Integer) (defaults to: :undef)

    The amount of minutes after which the thread will stop showing in the channel list.

  • default_thread_rate_limit_per_user (Integer) (defaults to: :undef)

    The default slowmode rate to set on threads created in the text or forum channel.

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

    The reason to show in the server's audit log for modifying the channel.

Returns:

  • (nil)


1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
# File 'lib/discordrb/data/channel.rb', line 1147

def modify(
  name: :undef, type: :undef, topic: :undef, nsfw: :undef, rate_limit_per_user: :undef, bitrate: :undef,
  user_limit: :undef, permission_overwrites: :undef, parent: :undef, voice_region: :undef, video_quality_mode: :undef,
  default_auto_archive_duration: :undef, flags: :undef, tags: :undef, default_reaction: :undef, default_sort_order: :undef,
  default_forum_layout: :undef, archived: :undef, locked: :undef, invitable: :undef, add_flags: :undef, remove_flags: :undef,
  position: :undef, auto_archive_duration: :undef, default_thread_rate_limit_per_user: :undef, reason: nil
)
  data = {
    name: name,
    type: TYPES[type] || type,
    topic: topic,
    nsfw: nsfw,
    position: position,
    rate_limit_per_user: rate_limit_per_user,
    bitrate: bitrate,
    user_limit: user_limit,
    permission_overwrites: permission_overwrites == :undef ? permission_overwrites : permission_overwrites&.map(&:to_hash),
    parent_id: parent == :undef ? parent : parent&.resolve_id,
    rtc_region: voice_region == :undef ? voice_region : voice_region&.to_s,
    video_quality_mode: VIDEO_QUALITY_MODES[video_quality_mode] || video_quality_mode,
    default_auto_archive_duration: default_auto_archive_duration,
    default_reaction_emoji: default_reaction == :undef ? default_reaction : Emoji.build_emoji_hash(default_reaction),
    default_sort_order: FORUM_SORT_ORDERS[default_sort_order] || default_sort_order,
    default_forum_layout: FORUM_LAYOUTS[default_forum_layout] || default_forum_layout,
    archived: archived,
    flags: flags == :undef ? flags : Array(flags).map { |bit| FLAGS[bit] || bit.to_i }.reduce(&:|),
    default_thread_rate_limit_per_user: default_thread_rate_limit_per_user,
    auto_archive_duration: auto_archive_duration,
    locked: locked,
    invitable: invitable
  }

  if tags != :undef && (thread_only? || thread?)
    tags = (thread? ? tags&.map(&:resolve_id) : tags&.map(&:to_h))

    data[thread_only? ? :available_tags : :applied_tags] = tags
  end

  if data[:type] != :undef
    if news? && data[:type] != TYPES[:text]
      raise ArgumentError, 'Can only convert news channels to text channels'
    elsif text? && data[:type] != TYPES[:news]
      raise ArgumentError, 'Can only convert text channels to news channels'
    elsif !text? && !news?
      raise ArgumentError, 'Can only convert between text and news channels'
    end
  end

  if add_flags != :undef || remove_flags != :undef
    raise ArgumentError, "'add_flags' and 'remove_flags' cannot be used with 'flags'" if flags != :undef

    to_flags = lambda do |value|
      [*(value == :undef ? 0 : value)].map { |bit| FLAGS[bit] || bit.to_i }.reduce(&:|)
    end

    data[:flags] = ((@flags & ~to_flags.call(remove_flags)) | to_flags.call(add_flags))
  end

  update_data(JSON.parse(API::Channel.update!(@bot.token, @id, **data, reason: reason)))
  nil
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.



277
278
279
# File 'lib/discordrb/data/channel.rb', line 277

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.



287
288
289
# File 'lib/discordrb/data/channel.rb', line 287

def news_thread?
  @type == 10
end

#nsfw=(nsfw) ⇒ Object

Sets whether this channel is NSFW

Parameters:

  • nsfw (true, false)

Raises:

  • (ArgumentError)

    if value isn't one of true, false



434
435
436
437
438
# File 'lib/discordrb/data/channel.rb', line 434

def nsfw=(nsfw)
  raise ArgumentError, 'nsfw value must be true or false' unless nsfw.is_a?(TrueClass) || nsfw.is_a?(FalseClass)

  modify(nsfw: nsfw)
end

#nsfw?true, false

Check if this channel is marked as NSFW.

Returns:

  • (true, false)

    Whether or not this channel is marked as NSFW.



419
420
421
# File 'lib/discordrb/data/channel.rb', line 419

def nsfw?
  thread? ? parent.nsfw? : @nsfw
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:



449
450
451
452
453
# File 'lib/discordrb/data/channel.rb', line 449

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:



459
460
461
# File 'lib/discordrb/data/channel.rb', line 459

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

#pins(limit: 50) ⇒ Array<Message>

Requests the pinned messages in a channel.

Parameters:

  • limit (Integer, nil) (defaults to: 50)

    the limit of how many pinned messages to retrieve. nil will return all the pinned messages.

Returns:

  • (Array<Message>)

    the messages pinned in the channel.



808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
# File 'lib/discordrb/data/channel.rb', line 808

def pins(limit: 50)
  get_pins = proc do |fetch_limit, before = nil|
    resp = API::Channel.pinned_messages(@bot.token, @id, fetch_limit, before&.iso8601)
    JSON.parse(resp)['items'].map { |pin| Message.new(pin['message'].merge({ 'pinned_at' => pin['pinned_at'] }), @bot) }
  end

  # Can be done without pagination.
  return get_pins.call(limit) if limit && limit <= 50

  paginator = Paginator.new(limit, :down) do |last_page|
    if last_page && last_page.count < 50
      []
    else
      get_pins.call(50, last_page&.last&.pinned_at)
    end
  end

  paginator.to_a
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.



257
258
259
# File 'lib/discordrb/data/channel.rb', line 257

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.



154
155
156
# File 'lib/discordrb/data/channel.rb', line 154

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.



297
298
299
# File 'lib/discordrb/data/channel.rb', line 297

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



838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
# File 'lib/discordrb/data/channel.rb', line 838

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.



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

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



164
165
166
# File 'lib/discordrb/data/channel.rb', line 164

def recipient
  @recipients.first if pm?
end

#remove_member(member) ⇒ Object

Parameters:

  • member (Member, Integer, String)

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



1113
1114
1115
# File 'lib/discordrb/data/channel.rb', line 1113

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

#remove_tags(tags, reason: nil) ⇒ Object Also known as: remove_tag

Remove one or more tag from this thread channel.

Parameters:



1071
1072
1073
1074
1075
# File 'lib/discordrb/data/channel.rb', line 1071

def remove_tags(tags, reason: nil)
  raise 'Cannot remove tags from this channel' unless parent&.thread_only?

  modify(tags: @applied_tags - Array(tags).map(&:resolve_id), reason: reason)
end

#role_overwritesOverwrite

Returns any role-type permission overwrites on this channel.

Returns:

  • (Overwrite)

    any role-type permission overwrites on this channel



521
522
523
# File 'lib/discordrb/data/channel.rb', line 521

def role_overwrites
  permission_overwrites :role
end

#send_embed(message = '', embed = nil, attachments = nil, tts = false, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0) {|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 = Discordrb::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 (Discordrb::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, Discordrb::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.

  • flags (Integer) (defaults to: 0)

    Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2), SUPPRESS_NOTIFICATIONS (1 << 12), and IS_COMPONENTS_V2 (1 << 15) can be set.

Yields:

  • (embed)

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

Yield Parameters:

Returns:

  • (Message)

    The resulting message.



584
585
586
587
588
589
590
591
# File 'lib/discordrb/data/channel.rb', line 584

def send_embed(message = '', embed = nil, attachments = nil, tts = false, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0)
  embed ||= Discordrb::Webhooks::Embed.new
  view = Discordrb::Webhooks::View.new

  yield(embed, view) if block_given?

  send_message(message, tts, embed, attachments, allowed_mentions, message_reference, components || view.to_a, flags)
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.



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

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, flags = 0) ⇒ 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, Discordrb::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, Discordrb::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.

  • flags (Integer) (defaults to: 0)

    Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2), SUPPRESS_NOTIFICATIONS (1 << 12), and IS_COMPONENTS_V2 (1 << 15) can be set.

Returns:

  • (Message)

    the message that was sent.



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

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

#send_message!(content: '', timeout: nil, tts: false, embeds: [], attachments: nil, allowed_mentions: nil, reference: nil, components: nil, flags: 0, has_components: false, nonce: nil, enforce_nonce: false) {|builder, view| ... } ⇒ Message?

Send a message to this channel.

Examples:

This sends a silent message with an embed.

channel.send_message!(content: 'Hi <@171764626755813376>', flags: :suppress_notifications) do |builder|
  builder.add_embed do |embed|
    embed.title = 'The Ruby logo'
    embed.image = Discordrb::Webhooks::EmbedImage.new(url: 'https://www.ruby-lang.org/images/header-ruby-logo.png')
  end
end

Parameters:

  • content (String) (defaults to: '')

    The content of the message. Should not be longer than 2000 characters or it will result in an error.

  • timeout (Float, nil) (defaults to: nil)

    The amount of time in seconds after which the message sent will be deleted, or nil if the message should not be deleted.

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

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

  • embeds (Array<Hash, Webhooks::Embed>) (defaults to: [])

    The embeds that should be attached to the message.

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

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

  • allowed_mentions (Hash, Discordrb::AllowedMentions, nil) (defaults to: nil)

    Mentions that are allowed to ping on this message.

  • reference (Message, String, Integer, Hash, nil) (defaults to: nil)

    The optional message, or message ID, to reply to or forward.

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

    Interaction components to associate with this message.

  • flags (Integer, Symbol, Array<Symbol, Integer>) (defaults to: 0)

    Flags for this message. Currently only :suppress_embeds (1 << 2), :suppress_notifications (1 << 12), and :uikit_components (1 << 15) can be set.

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

    Whether this message includes any V2 components. Enabling this disables sending content and embeds.

  • nonce (nil, String, Integer, false) (defaults to: nil)

    The 25 character nonce that should be used when sending this message.

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

    Whether the provided nonce should be enforced and used for message de-duplication.

Yield Parameters:

  • builder (Webhooks::Builder)

    An optional message builder. Arguments passed to the builder overwrite method data.

  • view (Webhooks::View)

    An optional component builder. Arguments passed to the builder overwrite method data.

Returns:

  • (Message, nil)

    The resulting message that was created, or nil if the timeout parameter was set to a non nil value.



616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/discordrb/data/channel.rb', line 616

def send_message!(content: '', timeout: nil, tts: false, embeds: [], attachments: nil, allowed_mentions: nil, reference: nil, components: nil, flags: 0, has_components: false, nonce: nil, enforce_nonce: false)
  builder = Discordrb::Webhooks::Builder.new
  view = Discordrb::Webhooks::View.new

  builder.tts = tts
  builder.content = content
  embeds&.each { |embed| builder << embed }
  builder.allowed_mentions = allowed_mentions

  yield(builder, view) if block_given?

  flags = Array(flags).map { |flag| Discordrb::Message::FLAGS[flag] || flag }.reduce(&:|)
  flags |= (1 << 15) if has_components
  builder = builder.to_json_hash

  if timeout
    @bot.send_temporary_message(@id, builder[:content], timeout, builder[:tts], builder[:embeds], attachments, builder[:allowed_mentions], reference, components&.to_a || view.to_a, flags, nonce, enforce_nonce)
  else
    @bot.send_message(@id, builder[:content], builder[:tts], builder[:embeds], attachments, builder[:allowed_mentions], reference, components&.to_a || view.to_a, flags, nonce, enforce_nonce)
  end
end

#send_multiple(content) ⇒ Object

Sends multiple messages to a channel

Parameters:

  • content (Array<String>)

    The messages to send.



640
641
642
# File 'lib/discordrb/data/channel.rb', line 640

def send_multiple(content)
  content.each { |text| send_message!(content: text) }
end

#send_temporary_message(content, timeout, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0) ⇒ 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, Discordrb::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, Discordrb::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.

  • flags (Integer) (defaults to: 0)

    Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2), SUPPRESS_NOTIFICATIONS (1 << 12), and IS_COMPONENTS_V2 (1 << 15) can be set.



563
564
565
# File 'lib/discordrb/data/channel.rb', line 563

def send_temporary_message(content, timeout, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0)
  @bot.send_temporary_message(@id, content, timeout, tts, embed, attachments, allowed_mentions, message_reference, components, flags)
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:



241
242
243
244
245
246
247
248
249
# File 'lib/discordrb/data/channel.rb', line 241

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

  @server = @bot.server(@server_id)
  raise Discordrb::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



533
534
535
# File 'lib/discordrb/data/channel.rb', line 533

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)


356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/discordrb/data/channel.rb', line 356

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!



646
647
648
649
# File 'lib/discordrb/data/channel.rb', line 646

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

#stage?true, false

Returns whether or not this channel is a stage channel.

Returns:

  • (true, false)

    whether or not this channel is a stage channel.



307
308
309
# File 'lib/discordrb/data/channel.rb', line 307

def stage?
  @type == 13
end

#start_forum_thread(name:, auto_archive_duration: nil, rate_limit_per_user: nil, tags: nil, content: nil, embeds: nil, allowed_mentions: nil, components: nil, stickers: nil, attachments: nil, flags: nil, has_components: false, reason: nil) {|builder, view| ... } ⇒ Message

Start a thread in a forum or media channel.

Parameters:

  • name (String)

    The name of the forum post to create.

  • auto_archive_duration (Integer, nil) (defaults to: nil)

    How long before the post is automatically archived.

  • rate_limit_per_user (Integer, nil) (defaults to: nil)

    The slowmode rate of the forum post to create.

  • tags (Array<#resolve_id>, nil) (defaults to: nil)

    The tags of the forum channel to apply onto the forum post.

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

    The content of the forum post's starter message.

  • embeds (Array<Hash, Webhooks::Embed>, nil) (defaults to: nil)

    The embeds that should be attached to the forum post's starter message.

  • allowed_mentions (Hash, Discordrb::AllowedMentions, nil) (defaults to: nil)

    Mentions that are allowed to ping on this forum post's starter message.

  • components (Webhooks::View, Array<#to_h>, nil) (defaults to: nil)

    The interaction components to associate with this forum post's starter message.

  • stickers (Array<#resolve_id>, nil) (defaults to: nil)

    The stickers to include in the forum post's starter message.

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

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

  • flags (Integer, Symbol, Array<Symbol, Integer>, nil) (defaults to: nil)

    The flags to set on the forum post's starter message. Currently only :suppress_embeds (1 << 2), :suppress_notifications (1 << 12), and :uikit_components (1 << 15) can be set.

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

    Whether the starter message for this forum post includes any V2 components. Enabling this disables sending content and embeds.

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

    The reason for creating this forum post.

Yield Parameters:

  • builder (Webhooks::Builder)

    An optional message builder. Arguments passed to the builder overwrite method data.

  • view (Webhooks::View)

    An optional component builder. Arguments passed to the builder overwrite method data.

Returns:

  • (Message)

    the starter message of the forum post. The forum post that was created can be accessed via Message#thread.



1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
# File 'lib/discordrb/data/channel.rb', line 1009

def start_forum_thread(name:, auto_archive_duration: nil, rate_limit_per_user: nil, tags: nil, content: nil, embeds: nil, allowed_mentions: nil, components: nil, stickers: nil, attachments: nil, flags: nil, has_components: false, reason: nil)
  builder = Discordrb::Webhooks::Builder.new
  view = Discordrb::Webhooks::View.new

  builder.content = content
  embeds&.each { |embed| builder << embed }
  builder.allowed_mentions = allowed_mentions

  yield(builder, view) if block_given?

  flags = Array(flags).map { |flag| Discordrb::Message::FLAGS[flag] || flag }.reduce(&:|)
  flags |= (1 << 15) if has_components
  builder = builder.to_json_hash

  message = { content: builder[:content], embeds: builder[:embeds], allowed_mentions: builder[:allowed_mentions], components: components&.to_a || view.to_a, sticker_ids: stickers&.map(&:resolve_id), flags: flags }
  response = JSON.parse(API::Channel.start_thread_in_forum_or_media_channel(@bot.token, @id, name, message.compact, attachments, rate_limit_per_user, auto_archive_duration, tags&.map(&:resolve_id), reason))

  Message.new(response['message'].merge!('channel_id' => response['id'], 'thread' => response), @bot)
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:



979
980
981
982
983
984
985
986
987
988
989
990
# File 'lib/discordrb/data/channel.rb', line 979

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

  @bot.ensure_channel(JSON.parse(data))
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()


914
915
916
# File 'lib/discordrb/data/channel.rb', line 914

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.



282
283
284
# File 'lib/discordrb/data/channel.rb', line 282

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



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

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



485
486
487
488
489
# File 'lib/discordrb/data/channel.rb', line 485

def synchronized?
  return unless parent

  permission_overwrites == parent.permission_overwrites
end

#tag(id) ⇒ ChannelTag?

Get a tag in this forum or media channel.

Parameters:

Returns:

  • (ChannelTag, nil)

    The tag that was found or nil if it couldn't be found.



1036
1037
1038
1039
# File 'lib/discordrb/data/channel.rb', line 1036

def tag(id)
  id = id.resolve_id
  @available_tags.find { |tag| tag == id }
end

#tag?(id) ⇒ true, false

Check if a specific tag has been applied to this thread.

Parameters:

Returns:

  • (true, false)

    Whether or not the thread has the tag applied.



1044
1045
1046
# File 'lib/discordrb/data/channel.rb', line 1044

def tag?(id)
  @applied_tags.any?(id&.resolve_id)
end

#tagsArray<ChannelTag>

Get the tags for this channel. If this channel is a thread channel, then the tags that have been applied to the thread will be returned, and if the channel is a forum or media channel, then the tags that can be applied onto threads created in this channel will be returned instead.

Returns:

  • (Array<ChannelTag>)

    The available or set channel tags for this channel.



1053
1054
1055
1056
1057
# File 'lib/discordrb/data/channel.rb', line 1053

def tags
  return @available_tags if forum? || !thread_only?

  @applied_tags.filter_map { |tag_id| parent&.tag(tag_id) }
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



252
253
254
# File 'lib/discordrb/data/channel.rb', line 252

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:



505
506
507
# File 'lib/discordrb/data/channel.rb', line 505

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.



302
303
304
# File 'lib/discordrb/data/channel.rb', line 302

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

#thread_only?true, false

Check if this channel is a forum or media channel.

Returns:

  • (true, false)

    whether or not only threads can be created in this channel.



328
329
330
# File 'lib/discordrb/data/channel.rb', line 328

def thread_only?
  forum? || media?
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



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

def users
  if text?
    server.online_members(include_idle: true).select { |u| u.can_read_messages? 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.



262
263
264
# File 'lib/discordrb/data/channel.rb', line 262

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:



511
512
513
# File 'lib/discordrb/data/channel.rb', line 511

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

#webhooksArray<Webhook>

Requests a list of Webhooks on the channel.

Returns:

  • (Array<Webhook>)

    webhooks on the channel.



933
934
935
936
937
938
# File 'lib/discordrb/data/channel.rb', line 933

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