Class: Discorb::ForumChannel

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

Overview

Represents a forum channel.

Defined Under Namespace

Classes: Post, Tag

Constant Summary collapse

DEFAULT_SORT_ORDER =
{ 1 => :latest_activity, 2 => :creation_date }.freeze

Instance Attribute Summary collapse

Attributes inherited from GuildChannel

#permission_overwrites, #position

Attributes inherited from Channel

#id, #name

Instance Method Summary collapse

Methods inherited from GuildChannel

#<=>, #==, #create_invite, #delete, #delete_permissions, #fetch_invites, #guild, #mention, #move, #parent, #set_permissions, #to_s

Methods inherited from Channel

#==, #type

Methods inherited from DiscordModel

#==, #eql?

Constructor Details

#initialize(client, data, no_cache: false) ⇒ ForumChannel

Returns a new instance of ForumChannel.



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

def initialize(client, data, no_cache: false)
  super
  _set_data(data)
end

Instance Attribute Details

#default_auto_archive_durationInteger (readonly)

Returns The default value of duration of auto archive.

Returns:

  • (Integer)

    The default value of duration of auto archive.



21
22
23
# File 'lib/discorb/channel/forum.rb', line 21

def default_auto_archive_duration
  @default_auto_archive_duration
end

#default_sort_order:latest_activity, :creation_date (readonly)

Returns The default sort order of the channel.

Returns:

  • (:latest_activity, :creation_date)

    The default sort order of the channel.



23
24
25
# File 'lib/discorb/channel/forum.rb', line 23

def default_sort_order
  @default_sort_order
end

#guidelineString (readonly) Also known as: topic

Returns The guideline of the channel.

Returns:

  • (String)

    The guideline of the channel.



9
10
11
# File 'lib/discorb/channel/forum.rb', line 9

def guideline
  @guideline
end

#message_rate_limit_per_userInteger (readonly) Also known as: message_slowmode

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

Returns:

  • (Integer)

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



18
19
20
# File 'lib/discorb/channel/forum.rb', line 18

def message_rate_limit_per_user
  @message_rate_limit_per_user
end

#nsfwBoolean (readonly)

Returns Whether the channel is nsfw.

Returns:

  • (Boolean)

    Whether the channel is nsfw.



12
13
14
# File 'lib/discorb/channel/forum.rb', line 12

def nsfw
  @nsfw
end

#rate_limit_per_userInteger (readonly) Also known as: slowmode, thread_slowmode

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

Returns:

  • (Integer)

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



14
15
16
# File 'lib/discorb/channel/forum.rb', line 14

def rate_limit_per_user
  @rate_limit_per_user
end

#require_tagBoolean (readonly) Also known as: require_tag?

Returns Whether at least one tag is required.

Returns:

  • (Boolean)

    Whether at least one tag is required.



27
28
29
# File 'lib/discorb/channel/forum.rb', line 27

def require_tag
  @require_tag
end

#tagsArray<Discorb::ForumChannel::Tag> (readonly)

Returns The tags in the channel.

Returns:



25
26
27
# File 'lib/discorb/channel/forum.rb', line 25

def tags
  @tags
end

#threadsObject (readonly)



34
35
36
# File 'lib/discorb/channel/forum.rb', line 34

def threads
  guild.threads.select { |thread| thread.parent == self }
end

Instance Method Details

#create_post(title, content = nil, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, attachment: nil, attachments: nil, tags: nil, auto_archive_duration: nil, rate_limit_per_user: nil, slowmode: nil, reason: nil) ⇒ Async::Task<Discorb::ForumChannel::Post> Also known as: start_thread

Create post in the channel.

Parameters:

  • title (Title)

    The title of the thread.

  • content (String) (defaults to: nil)

    The message content.

  • embed (Discorb::Embed) (defaults to: nil)

    The embed to send.

  • embeds (Array<Discorb::Embed>) (defaults to: nil)

    The embeds to send.

  • allowed_mentions (Discorb::AllowedMentions) (defaults to: nil)

    The allowed mentions.

  • components (Array<Discorb::Component>, Array<Array<Discorb::Component>>) (defaults to: nil)

    The components to send.

  • attachment (Discorb::Attachment) (defaults to: nil)

    The attachment to send.

  • attachments (Array<Discorb::Attachment>) (defaults to: nil)

    The attachments to send.

  • tags (Array<Discorb::ForumChannel::Tag>) (defaults to: nil)

    The tags to attach.

  • auto_archive_duration (:hour, :day, :three_days, :week) (defaults to: nil)

    The duration of auto-archiving.

  • rate_limit_per_user (Integer) (defaults to: nil)

    The rate limit per user.

  • slowmode (Integer) (defaults to: nil)

    Alias of rate_limit_per_user.

Returns:



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/discorb/channel/forum.rb', line 469

def create_post(
  title,
  content = nil,
  embed: nil,
  embeds: nil,
  allowed_mentions: nil,
  components: nil,
  attachment: nil,
  attachments: nil,
  tags: nil,
  auto_archive_duration: nil,
  rate_limit_per_user: nil,
  slowmode: nil,
  reason: nil
)
  Async do
    message_payload = {}
    message_payload[:content] = content if content
    tmp_embed =
      if embed
        [embed]
      elsif embeds
        embeds
      end
    message_payload[:embeds] = tmp_embed.map(&:to_hash) if tmp_embed
    message_payload[:allowed_mentions] = (
      if allowed_mentions
        allowed_mentions.to_hash(@client.allowed_mentions)
      else
        @client.allowed_mentions.to_hash
      end
    )
    message_payload[:components] = Component.to_payload(
      components
    ) if components
    attachments ||= attachment ? [attachment] : []

    message_payload[:attachments] = attachments.map.with_index do |a, i|
      { id: i, filename: a.filename, description: a.description }
    end

    payload = {}
    payload[:name] = title
    payload[
      :auto_archive_duration
    ] = auto_archive_duration if auto_archive_duration
    payload[:rate_limit_per_user] = rate_limit_per_user ||
      slowmode if rate_limit_per_user || slowmode
    payload[:message] = message_payload
    payload[:applied_tags] = tags.map(&:id) if tags

    _resp, data =
      @client
        .http
        .multipart_request(
          Route.new(
            "/channels/#{channel_id.wait}/threads",
            "//channels/:channel_id/threads",
            :post
          ),
          payload,
          attachments,
          audit_log_reason: reason
        )
        .wait
    Channel.make_channel(@client, data)
  end
end

#create_tags(*tags, reason: nil) ⇒ Async::Task<self>

Creates tags in the channel.

Parameters:

  • tags (Array<Discorb::ForumChannel::Tag>)

    The tags to create.

  • reason (String) (defaults to: nil)

    The reason of creating tags.

Returns:

  • (Async::Task<self>)

    The edited channel.

See Also:



378
379
380
# File 'lib/discorb/channel/forum.rb', line 378

def create_tags(*tags, reason: nil)
  edit(tags: self.tags + tags, reason:)
end

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

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:



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/discorb/channel/forum.rb', line 406

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

#delete_tags(*tags, reason: nil) ⇒ Async::Task<self>

Deletes tags from the channel.

Parameters:

  • tags (Array<Discorb::ForumChannel::Tag>)

    The tags to delete.

  • reason (String) (defaults to: nil)

    The reason of deleting tags.

Returns:

  • (Async::Task<self>)

    The edited channel.

See Also:



393
394
395
# File 'lib/discorb/channel/forum.rb', line 393

def delete_tags(*tags, reason: nil)
  edit(tags: self.tags - tags, reason:)
end

#edit(name: Discorb::Unset, position: Discorb::Unset, category: Discorb::Unset, parent: Discorb::Unset, topic: Discorb::Unset, guideline: Discorb::Unset, nsfw: Discorb::Unset, rate_limit_per_user: Discorb::Unset, slowmode: Discorb::Unset, thread_rate_limit_per_user: Discorb::Unset, thread_slowmode: Discorb::Unset, default_auto_archive_duration: Discorb::Unset, archive_in: Discorb::Unset, require_tag: Discorb::Unset, default_reaction_emoji: Discorb::Unset, default_sort_order: Discorb::Unset, tags: Discorb::Unset, reason: nil) ⇒ Async::Task<self> Also known as: modify

Note:

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

Edits the channel.

Parameters:

  • name (String) (defaults to: Discorb::Unset)

    The name of the channel.

  • position (Integer) (defaults to: Discorb::Unset)

    The position of the channel.

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

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

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

    Alias of category.

  • topic (String) (defaults to: Discorb::Unset)

    The topic of the channel.

  • guideline (String) (defaults to: Discorb::Unset)

    Alias of topic.

  • nsfw (Boolean) (defaults to: Discorb::Unset)

    Whether the channel is nsfw.

  • rate_limit_per_user (Integer) (defaults to: Discorb::Unset)

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

  • slowmode (Integer) (defaults to: Discorb::Unset)

    Alias of rate_limit_per_user.

  • thread_rate_limit_per_user (Integer) (defaults to: Discorb::Unset)

    The rate limit per user (Slowmode) in threads.

  • thread_slowmode (Integer) (defaults to: Discorb::Unset)

    Alias of thread_rate_limit_per_user.

  • default_auto_archive_duration (Integer) (defaults to: Discorb::Unset)

    The default auto archive duration of the channel.

  • archive_in (Integer) (defaults to: Discorb::Unset)

    Alias of default_auto_archive_duration.

  • require_tag (Boolean) (defaults to: Discorb::Unset)

    Whether the channel requires tag to create thread.

  • default_reaction_emoji (Discorb::Emoji) (defaults to: Discorb::Unset)

    The default reaction emoji of the channel.

  • default_sort_order (:latest_activity, :creation_date) (defaults to: Discorb::Unset)

    The default sort order of the channel.

  • tags (Array<Discorb::ForumChannel::Tag>) (defaults to: Discorb::Unset)

    The tags of the channel.

  • reason (String) (defaults to: nil)

    The reason of editing the channel.

Returns:

  • (Async::Task<self>)

    The edited channel.

See Also:



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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
# File 'lib/discorb/channel/forum.rb', line 282

def edit(
  name: Discorb::Unset,
  position: Discorb::Unset,
  category: Discorb::Unset,
  parent: Discorb::Unset,
  topic: Discorb::Unset,
  guideline: Discorb::Unset,
  nsfw: Discorb::Unset,
  rate_limit_per_user: Discorb::Unset,
  slowmode: Discorb::Unset,
  thread_rate_limit_per_user: Discorb::Unset,
  thread_slowmode: Discorb::Unset,
  default_auto_archive_duration: Discorb::Unset,
  archive_in: Discorb::Unset,
  require_tag: Discorb::Unset,
  default_reaction_emoji: Discorb::Unset,
  default_sort_order: Discorb::Unset,
  tags: Discorb::Unset,
  reason: nil
)
  Async do
    payload = {}
    payload[:name] = name unless name == Discorb::Unset
    payload[:position] = position unless position == Discorb::Unset
    payload[:parent_id] = category.id unless category == Discorb::Unset
    payload[:parent_id] = parent.id unless parent == Discorb::Unset
    payload[:topic] = topic unless topic == Discorb::Unset
    payload[:topic] = guideline unless guideline == Discorb::Unset
    payload[:nsfw] = nsfw unless nsfw == Discorb::Unset
    payload[
      :rate_limit_per_user
    ] = rate_limit_per_user unless rate_limit_per_user == Discorb::Unset
    payload[:rate_limit_per_user] = slowmode unless slowmode ==
      Discorb::Unset
    payload[
      :thread_rate_limit_per_user
    ] = thread_rate_limit_per_user unless thread_rate_limit_per_user ==
      Discorb::Unset
    payload[
      :thread_rate_limit_per_user
    ] = thread_slowmode unless thread_slowmode == Discorb::Unset
    payload[
      :default_auto_archive_duration
    ] = default_auto_archive_duration unless default_auto_archive_duration ==
      Discorb::Unset
    payload[
      :default_auto_archive_duration
    ] = archive_in unless archive_in == Discorb::Unset
    payload[:flags] = (require_tag ? 1 << 4 : 0) unless require_tag ==
      Discorb::Unset
    payload[:default_reaction_emoji] = default_reaction_emoji
      .to_hash
      .then do |e|
      { emoji_name: e[:name], emoji_id: e[:id] }
    end unless default_reaction_emoji == Discorb::Unset
    payload[:default_sort_order] = DEFAULT_SORT_ORDER.key(
      default_sort_order
    ) unless default_sort_order == Discorb::Unset
    payload[:available_tags] = tags.map(&:to_hash) unless tags ==
      Discorb::Unset
    ret =
      @client
        .http
        .request(
          Route.new("/channels/#{id}", "//channels/:channel_id", :patch),
          reason:
        )
        .wait
    _set_data(ret)
    if tags != Discorb::Unset
      tags
        .reject(&:id)
        .each do |tag|
          tag.instance_variable_set(
            :@id,
            ret[:available_tags].find { |t| t.to_hash == tag.to_hash }[:id]
          )
        end
    end
    self
  end
end

#fetch_archived_threadsAsync::Task<Array<Discorb::ThreadChannel>>

Fetch archived threads in the channel.

Returns:



547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/discorb/channel/forum.rb', line 547

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

#fetch_webhooksAsync::Task<Array<Discorb::Webhook>>

Fetch webhooks in the channel.

Returns:



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/discorb/channel/forum.rb', line 433

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

#inspectObject



249
250
251
# File 'lib/discorb/channel/forum.rb', line 249

def inspect
  "#<#{self.class}: #{name} id=#{id}>"
end