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

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)



82
83
84
# File 'lib/discordrb/data/channel.rb', line 82

def archive_timestamp
  @archive_timestamp
end

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



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

def archived
  @archived
end

#auto_archive_durationInteger? (readonly)



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

def auto_archive_duration
  @auto_archive_duration
end

#bitrateInteger



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

def bitrate
  @bitrate
end

#flagsInteger (readonly)



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

def flags
  @flags
end

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



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

def invitable
  @invitable
end

#join_timestampTime? (readonly)



89
90
91
# File 'lib/discordrb/data/channel.rb', line 89

def join_timestamp
  @join_timestamp
end

#last_message_idInteger? (readonly)



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

def last_message_id
  @last_message_id
end

#last_pin_timestampTime? (readonly)



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

def last_pin_timestamp
  @last_pin_timestamp
end

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



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

def locked
  @locked
end

#member_countInteger? (readonly)



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

def member_count
  @member_count
end

#member_flagsInteger? (readonly)



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

def member_flags
  @member_flags
end

#message_countInteger? (readonly)



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

def message_count
  @message_count
end

#nameString



30
31
32
# File 'lib/discordrb/data/channel.rb', line 30

def name
  @name
end

#nsfwtrue, false Also known as: nsfw?



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

def nsfw
  @nsfw
end

#owner_idInteger? (readonly)



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

def owner_id
  @owner_id
end

#parent_idInteger? (readonly)



34
35
36
# File 'lib/discordrb/data/channel.rb', line 34

def parent_id
  @parent_id
end

#positionInteger



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

def position
  @position
end

#rate_limit_per_userInteger Also known as: slowmode_rate



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

def rate_limit_per_user
  @rate_limit_per_user
end

#recipientsArray<Recipient>? (readonly)



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

def recipients
  @recipients
end

#topicString



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

def topic
  @topic
end

#total_message_sentInteger (readonly) Also known as: total_messages_sent



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

def total_message_sent
  @total_message_sent
end

#typeInteger (readonly)

Returns the type of this channel.

See Also:



38
39
40
# File 'lib/discordrb/data/channel.rb', line 38

def type
  @type
end

#user_limitInteger Also known as: limit



54
55
56
# File 'lib/discordrb/data/channel.rb', line 54

def user_limit
  @user_limit
end

#video_quality_modeInteger? (readonly)



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

def video_quality_mode
  @video_quality_mode
end

#voice_regionString? (readonly)



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

def voice_region
  @voice_region
end

Instance Method Details

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

Adds a user to a group channel.



898
899
900
901
902
903
904
905
906
# File 'lib/discordrb/data/channel.rb', line 898

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



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

def add_member(member)
  @bot.add_thread_member(@id, member)
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:



849
850
851
# File 'lib/discordrb/data/channel.rb', line 849

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:



856
857
858
# File 'lib/discordrb/data/channel.rb', line 856

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

#categoryChannel? Also known as: parent



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

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

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

Sets this channels parent category

Raises:

  • (ArgumentError)

    if the target channel isn't a category



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

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



228
229
230
# File 'lib/discordrb/data/channel.rb', line 228

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.



431
432
433
434
435
# File 'lib/discordrb/data/channel.rb', line 431

def children
  return [] unless category?

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

#create_group(user_ids) ⇒ Channel

Creates a Group channel



887
888
889
890
891
892
893
# File 'lib/discordrb/data/channel.rb', line 887

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

Raises:

  • (ArgumentError)

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



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

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?



462
463
464
# File 'lib/discordrb/data/channel.rb', line 462

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(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)
    


670
671
672
673
674
675
676
677
678
679
# File 'lib/discordrb/data/channel.rb', line 670

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



607
608
609
# File 'lib/discordrb/data/channel.rb', line 607

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



601
602
603
# File 'lib/discordrb/data/channel.rb', line 601

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

Raises:

  • (ArgumentError)

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



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

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



684
685
686
687
688
# File 'lib/discordrb/data/channel.rb', line 684

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

#directory?true, false



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

def directory?
  @type == 14
end

#forum?true, false



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

def forum?
  @type == 15
end

#group?true, false



223
224
225
# File 'lib/discordrb/data/channel.rb', line 223

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)


743
744
745
746
# File 'lib/discordrb/data/channel.rb', line 743

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.



1067
1068
1069
# File 'lib/discordrb/data/channel.rb', line 1067

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.



959
960
961
962
963
964
# File 'lib/discordrb/data/channel.rb', line 959

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.



1039
1040
1041
# File 'lib/discordrb/data/channel.rb', line 1039

def join_thread
  @bot.join_thread(@id)
end

#last_messageMessage, ...

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



969
970
971
972
973
974
975
976
977
# File 'lib/discordrb/data/channel.rb', line 969

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_groupObject Also known as: leave

Leaves the group.



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

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



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

def leave_thread
  @bot.leave_thread(@id)
end


1129
1130
1131
# File 'lib/discordrb/data/channel.rb', line 1129

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.



759
760
761
762
763
764
765
766
# File 'lib/discordrb/data/channel.rb', line 759

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.



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

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



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

def media?
  @type == 16
end

#member_overwritesOverwrite



452
453
454
# File 'lib/discordrb/data/channel.rb', line 452

def member_overwrites
  permission_overwrites :member
end

#membersObject

Members in the thread.



1049
1050
1051
# File 'lib/discordrb/data/channel.rb', line 1049

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

#mentionString



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

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

#news?true, false



233
234
235
# File 'lib/discordrb/data/channel.rb', line 233

def news?
  @type == 5
end

#news_thread?true, false



243
244
245
# File 'lib/discordrb/data/channel.rb', line 243

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

  • #permission_overwrites(type) ⇒ Array<Overwrite>

    Return an array of a certain type of overwrite



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

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



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

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

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

Requests the pinned messages in a channel.



773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# File 'lib/discordrb/data/channel.rb', line 773

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



213
214
215
# File 'lib/discordrb/data/channel.rb', line 213

def pm?
  @type == 1
end

#private?true, false



120
121
122
# File 'lib/discordrb/data/channel.rb', line 120

def private?
  pm? || group?
end

#private_thread?true, false



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

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 }

Yields:

  • (message)

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

Raises:

  • (ArgumentError)

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



803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
# File 'lib/discordrb/data/channel.rb', line 803

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



248
249
250
# File 'lib/discordrb/data/channel.rb', line 248

def public_thread?
  @type == 11
end

#recipientRecipient?



130
131
132
# File 'lib/discordrb/data/channel.rb', line 130

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.



913
914
915
916
917
918
919
920
921
# File 'lib/discordrb/data/channel.rb', line 913

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



1060
1061
1062
# File 'lib/discordrb/data/channel.rb', line 1060

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

#role_overwritesOverwrite



457
458
459
# File 'lib/discordrb/data/channel.rb', line 457

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

Yields:

  • (embed)

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

Yield Parameters:

  • embed (Discordrb::Webhooks::Embed)

    The embed from the parameters, or a new one.



520
521
522
523
524
525
526
527
# File 'lib/discordrb/data/channel.rb', line 520

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'))


595
596
597
# File 'lib/discordrb/data/channel.rb', line 595

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.



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

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

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.



552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/discordrb/data/channel.rb', line 552

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



576
577
578
# File 'lib/discordrb/data/channel.rb', line 576

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.



499
500
501
# File 'lib/discordrb/data/channel.rb', line 499

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.

Raises:



197
198
199
200
201
202
203
204
205
# File 'lib/discordrb/data/channel.rb', line 197

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



469
470
471
# File 'lib/discordrb/data/channel.rb', line 469

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.

Raises:

  • (TypeError)


306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/discordrb/data/channel.rb', line 306

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!



582
583
584
585
# File 'lib/discordrb/data/channel.rb', line 582

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

#stage?true, false



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

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.

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.



1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
# File 'lib/discordrb/data/channel.rb', line 1016

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.



986
987
988
989
990
991
992
993
994
995
996
997
# File 'lib/discordrb/data/channel.rb', line 986

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()


879
880
881
# File 'lib/discordrb/data/channel.rb', line 879

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

#store?true, false



238
239
240
# File 'lib/discordrb/data/channel.rb', line 238

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



412
413
414
415
416
# File 'lib/discordrb/data/channel.rb', line 412

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?



421
422
423
424
425
# File 'lib/discordrb/data/channel.rb', line 421

def synchronized?
  return unless parent

  permission_overwrites == parent.permission_overwrites
end

#text?true, false



208
209
210
# File 'lib/discordrb/data/channel.rb', line 208

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.



441
442
443
# File 'lib/discordrb/data/channel.rb', line 441

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

#thread?true, false



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

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.



722
723
724
725
726
727
728
# File 'lib/discordrb/data/channel.rb', line 722

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



218
219
220
# File 'lib/discordrb/data/channel.rb', line 218

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.



447
448
449
# File 'lib/discordrb/data/channel.rb', line 447

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

#webhooksArray<Webhook>

Requests a list of Webhooks on the channel.



950
951
952
953
954
955
# File 'lib/discordrb/data/channel.rb', line 950

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