Class: Discorb::TextChannel

Inherits:
GuildChannel show all
Includes:
Messageable
Defined in:
lib/discorb/channel.rb

Overview

Represents a text channel.

Direct Known Subclasses

NewsChannel

Instance Attribute Summary collapse

Attributes inherited from GuildChannel

#permission_overwrites, #position

Attributes inherited from Channel

#id, #name

Instance Method Summary collapse

Methods included from Messageable

#delete_message!, #edit_message, #fetch_message, #fetch_messages, #fetch_pins, #pin_message, #post, #typing, #unpin_message

Methods inherited from GuildChannel

#<=>, #==, #delete!, #guild, #inspect, #mention, #move, #parent, #to_s

Methods inherited from Channel

#==, #inspect, #type

Methods inherited from DiscordModel

#==, #eql?, #hash

Instance Attribute Details

#last_message_id -> Discorb::Snowflake (readonly)

Returns The id of the last message.

Returns:



237
238
239
# File 'lib/discorb/channel.rb', line 237

def last_message_id
  @last_message_id
end

#last_pin_timestamp -> Time (readonly) Also known as: last_pinned_at

Returns The time when the last pinned message was pinned.

Returns:

  • (Time)

    The time when the last pinned message was pinned.



242
243
244
# File 'lib/discorb/channel.rb', line 242

def last_pin_timestamp
  @last_pin_timestamp
end

#nsfw -> Boolean (readonly)

Returns Whether the channel is nsfw.

Returns:

  • (Boolean)

    Whether the channel is nsfw.



235
236
237
# File 'lib/discorb/channel.rb', line 235

def nsfw
  @nsfw
end

#rate_limit_per_user -> Integer (readonly) Also known as: slowmode

Returns The rate limit per user (Slowmode) in the channel.

Returns:

  • (Integer)

    The rate limit per user (Slowmode) in the channel.



239
240
241
# File 'lib/discorb/channel.rb', line 239

def rate_limit_per_user
  @rate_limit_per_user
end

#threads -> Array<Discorb::ThreadChannel> (readonly)

Returns The threads in the channel.

Returns:



245
246
247
# File 'lib/discorb/channel.rb', line 245

def threads
  @threads
end

#topic -> String (readonly)

Returns The topic of the channel.

Returns:

  • (String)

    The topic of the channel.



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

def topic
  @topic
end

Instance Method Details

#create_invite(max_age: nil, max_uses: nil, temporary: false, unique: false, reason: nil) -> Async::Task<Invite>

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Create an invite in the channel.

Parameters:

  • max_age (Integer) (defaults to: nil)

    The max age of the invite.

  • max_uses (Integer) (defaults to: nil)

    The max uses of the invite.

  • temporary (Boolean) (defaults to: false)

    Whether the invite is temporary.

  • unique (Boolean) (defaults to: false)

    Whether the invite is unique. @note if it's false it may return existing invite.

  • reason (String) (defaults to: nil)

    The reason of creating the invite.

Returns:

  • (Async::Task<Invite>)

    The created invite.

Raises:



445
446
447
448
449
450
451
452
453
454
455
# File 'lib/discorb/channel.rb', line 445

def create_invite(max_age: nil, max_uses: nil, temporary: false, unique: false, reason: nil)
  Async do
    _resp, data = @client.http.post("/channels/#{@id}/invites", {
      max_age: max_age,
      max_uses: max_uses,
      temporary: temporary,
      unique: unique,
    }, audit_log_reason: reason).wait
    Invite.new(@client, data)
  end
end

#create_webhook(name, avatar: nil) -> Async::Task<Discorb::Webhook::IncomingWebhook>

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Create webhook in the channel.

Parameters:

  • name (String)

    The name of the webhook.

  • avatar (Discorb::Image) (defaults to: nil)

    The avatar of the webhook.

Returns:

Raises:



315
316
317
318
319
320
321
322
323
# File 'lib/discorb/channel.rb', line 315

def create_webhook(name, avatar: nil)
  Async do
    payload = {}
    payload[:name] = name
    payload[:avatar] = avatar.to_s if avatar
    _resp, data = @client.http.post("/channels/#{@id}/webhooks", payload).wait
    Webhook.new([@client, data])
  end
end

#delete_messages!(*messages, force: false) -> Object Also known as: bulk_delete!, destroy_messages!

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Bulk delete messages in the channel.

Parameters:

  • messages (Discorb::Message)

    The messages to delete.

  • force (Boolean) (defaults to: false)

    Whether to ignore the validation for message (14 days limit).

Raises:



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/discorb/channel.rb', line 347

def delete_messages!(*messages, force: false)
  Async do
    messages = messages.first if messages.length == 1 && messages.first.is_a?(Array)
    unless force
      time = Time.now
      messages.delete_if do |message|
        next false unless message.is_a?(Message)

        time - message.created_at > 60 * 60 * 24 * 14
      end
    end

    message_ids = messages.map { |m| Discorb::Utils.try(m, :id).to_s }

    @client.http.post("/channels/#{@id}/messages/bulk-delete", { messages: message_ids }).wait
  end
end

#delete_permissions(target, reason: nil) -> Object Also known as: delete_permission, destroy_permissions, destroy_permission

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Delete the channel's permission overwrite.

Parameters:

  • target (Discorb::Role, Discorb::Member)

    The target of the overwrite.

  • reason (String) (defaults to: nil)

    The reason of deleting the overwrite.

Raises:



407
408
409
410
411
# File 'lib/discorb/channel.rb', line 407

def delete_permissions(target, reason: nil)
  Async do
    @client.http.delete("/channels/#{@id}/permissions/#{target.id}", audit_log_reason: reason).wait
  end
end

#edit(name: :unset, position: :unset, category: :unset, parent: :unset, topic: :unset, nsfw: :unset, announce: :unset, rate_limit_per_user: :unset, slowmode: :unset, default_auto_archive_duration: :unset, archive_in: :unset, reason: nil) -> Async::Task<self> Also known as: modify

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Note:

The arguments of this method are defaultly set to :unset. Specify value to set the value, if not don't specify or specify :unset.

Edits the channel.

Parameters:

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

    The name of the channel.

  • position (Integer) (defaults to: :unset)

    The position of the channel.

  • category (Discorb::CategoryChannel, nil) (defaults to: :unset)

    The parent of channel. Specify nil to remove the parent.

  • parent (Discorb::CategoryChannel, nil) (defaults to: :unset)

    Alias of category.

  • topic (String) (defaults to: :unset)

    The topic of the channel.

  • nsfw (Boolean) (defaults to: :unset)

    Whether the channel is nsfw.

  • announce (Boolean) (defaults to: :unset)

    Whether the channel is announce channel.

  • rate_limit_per_user (Integer) (defaults to: :unset)

    The rate limit per user (Slowmode) in the channel.

  • slowmode (Integer) (defaults to: :unset)

    Alias of rate_limit_per_user.

  • default_auto_archive_duration (Integer) (defaults to: :unset)

    The default auto archive duration of the channel.

  • archive_in (Integer) (defaults to: :unset)

    Alias of default_auto_archive_duration.

  • reason (String) (defaults to: nil)

    The reason of editing the channel.

Returns:

  • (Async::Task<self>)

    The edited channel.

Raises:



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/discorb/channel.rb', line 278

def edit(name: :unset, position: :unset, category: :unset, parent: :unset,
         topic: :unset, nsfw: :unset, announce: :unset,
         rate_limit_per_user: :unset, slowmode: :unset, default_auto_archive_duration: :unset,
         archive_in: :unset, reason: nil)
  Async do
    payload = {}
    payload[:name] = name if name != :unset
    payload[:announce] = announce ? 5 : 0 if announce != :unset
    payload[:position] = position if position != :unset
    payload[:topic] = topic || "" if topic != :unset
    payload[:nsfw] = nsfw if nsfw != :unset

    slowmode = rate_limit_per_user if slowmode == :unset
    payload[:rate_limit_per_user] = slowmode || 0 if slowmode != :unset
    parent = category if parent == :unset
    payload[:parent_id] = parent&.id if parent != :unset

    default_auto_archive_duration ||= archive_in
    payload[:default_auto_archive_duration] = default_auto_archive_duration if default_auto_archive_duration != :unset

    @client.http.patch("/channels/#{@id}", payload, audit_log_reason: reason).wait
    self
  end
end

#fetch_archived_private_threads -> Async::Task<Array<Discorb::ThreadChannel>>

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Fetch archived private threads in the channel.

Returns:

Raises:



544
545
546
547
548
549
# File 'lib/discorb/channel.rb', line 544

def fetch_archived_private_threads
  Async do
    _resp, data = @client.http.get("/channels/#{@id}/threads/archived/private").wait
    data.map { |thread| Channel.make_channel(@client, thread) }
  end
end

#fetch_archived_public_threads -> Async::Task<Array<Discorb::ThreadChannel>>

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Fetch archived threads in the channel.

Returns:

Raises:



530
531
532
533
534
535
# File 'lib/discorb/channel.rb', line 530

def fetch_archived_public_threads
  Async do
    _resp, data = @client.http.get("/channels/#{@id}/threads/archived/public").wait
    data.map { |thread| Channel.make_channel(@client, thread) }
  end
end

#fetch_invites -> Async::Task<Array<Discorb::Invite>>

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Fetch the channel's invites.

Returns:

Raises:



424
425
426
427
428
429
# File 'lib/discorb/channel.rb', line 424

def fetch_invites
  Async do
    _resp, data = @client.http.get("/channels/#{@id}/invites").wait
    data.map { |invite| Invite.new(@client, invite) }
  end
end

#fetch_joined_archived_private_threads(limit: nil, before: nil) -> Async::Task<Array<Discorb::ThreadChannel>>

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Fetch joined archived private threads in the channel.

Parameters:

  • limit (Integer) (defaults to: nil)

    The limit of threads to fetch.

  • before (Time) (defaults to: nil)

    <description>

Returns:

Raises:



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/discorb/channel.rb', line 561

def fetch_joined_archived_private_threads(limit: nil, before: nil)
  Async do
    if limit.nil?
      before = 0
      threads = []
      loop do
        _resp, data = @client.http.get("/channels/#{@id}/users/@me/threads/archived/private?before=#{before}").wait
        threads += data[:threads].map { |thread| Channel.make_channel(@client, thread) }
        before = data[:threads][-1][:id]

        break unless data[:has_more]
      end
      threads
    else
      _resp, data = @client.http.get("/channels/#{@id}/users/@me/threads/archived/private?limit=#{limit}&before=#{before}").wait
      data.map { |thread| Channel.make_channel(@client, thread) }
    end
  end
end

#fetch_webhooks -> Async::Task<Array<Discorb::Webhook>>

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Fetch webhooks in the channel.

Returns:

Raises:



332
333
334
335
336
337
# File 'lib/discorb/channel.rb', line 332

def fetch_webhooks
  Async do
    _resp, data = @client.http.get("/channels/#{@id}/webhooks").wait
    data.map { |webhook| Webhook.new([@client, webhook]) }
  end
end

#follow_from(target, reason: nil) -> Object

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Follow the existing announcement channel.

Parameters:

  • target (Discorb::NewsChannel)

    The channel to follow.

  • reason (String) (defaults to: nil)

    The reason of following the channel.

Raises:



465
466
467
468
469
# File 'lib/discorb/channel.rb', line 465

def follow_from(target, reason: nil)
  Async do
    @client.http.post("/channels/#{target.id}/followers", { webhook_channel_id: @id }, audit_log_reason: reason).wait
  end
end

#follow_to(target, reason: nil) -> Object

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Follow the existing announcement channel from self.

Parameters:

  • target (Discorb::TextChannel)

    The channel to follow to.

  • reason (String) (defaults to: nil)

    The reason of following the channel.

Raises:



479
480
481
482
483
# File 'lib/discorb/channel.rb', line 479

def follow_to(target, reason: nil)
  Async do
    @client.http.post("/channels/#{@id}/followers", { webhook_channel_id: target.id }, audit_log_reason: reason).wait
  end
end

#set_permissions(target, reason: nil, **perms) -> Object Also known as: modify_permissions, modify_permisssion, edit_permissions, edit_permission

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Set the channel's permission overwrite.

Parameters:

  • target (Discorb::Role, Discorb::Member)

    The target of the overwrite.

  • reason (String) (defaults to: nil)

    The reason of setting the overwrite.

  • perms (Symbol => Boolean)

    The permission overwrites to replace.

Raises:



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/discorb/channel.rb', line 377

def set_permissions(target, reason: nil, **perms)
  Async do
    allow_value = @permission_overwrites[target]&.allow_value.to_i
    deny_value = @permission_overwrites[target]&.deny_value.to_i
    perms.each do |perm, value|
      allow_value[Discorb::Permission.bits[perm]] = 1 if value == true
      deny_value[Discorb::Permission.bits[perm]] = 1 if value == false
    end
    payload = {
      allow: allow_value,
      deny: deny_value,
      type: target.is_a?(Member) ? 1 : 0,
    }
    @client.http.put("/channels/#{@id}/permissions/#{target.id}", payload, audit_log_reason: reason).wait
  end
end

#start_thread(name, message: nil, auto_archive_duration: 1440, public: true, rate_limit_per_user: nil, slowmode: nil, reason: nil) -> Async::Task<Discorb::ThreadChannel> Also known as: create_thread

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Start thread in the channel.

Parameters:

  • name (String)

    The name of the thread.

  • message (Discorb::Message) (defaults to: nil)

    The message to start the thread.

  • auto_archive_duration (Integer) (defaults to: 1440)

    The duration of auto-archiving.

  • public (Boolean) (defaults to: true)

    Whether the thread is public.

  • rate_limit_per_user (Integer) (defaults to: nil)

    The rate limit per user.

  • slowmode (Integer) (defaults to: nil)

    Alias of rate_limit_per_user.

  • reason (String) (defaults to: nil)

    The reason of starting the thread.

Returns:

Raises:



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/discorb/channel.rb', line 500

def start_thread(name, message: nil, auto_archive_duration: 1440, public: true, rate_limit_per_user: nil, slowmode: nil, reason: nil)
  Async do
    _resp, data = if message.nil?
        @client.http.post(
          "/channels/#{@id}/threads", {
            name: name,
            auto_archive_duration: auto_archive_duration,
            type: public ? 11 : 10,
            rate_limit_per_user: rate_limit_per_user || slowmode,
          },
          audit_log_reason: reason,
        ).wait
      else
        @client.http.post("/channels/#{@id}/messages/#{Utils.try(message, :id)}/threads", {
          name: name, auto_archive_duration: auto_archive_duration,
        }, audit_log_reason: reason).wait
      end
    Channel.make_channel(@client, data)
  end
end