class Discorb::TextChannel
Represents a text channel.
Attributes
@return [Discorb::Snowflake] The id of the last message.
@return [Time] The time when the last pinned message was pinned.
@return [Time] The time when the last pinned message was pinned.
@return [Boolean] Whether the channel is nsfw.
@return [Integer] The rate limit per user (Slowmode) in the channel.
@return [Integer] The rate limit per user (Slowmode) in the channel.
@return [Array<Discorb::ThreadChannel>] The threads in the channel.
@return [String] The topic of the channel.
Public Class Methods
@!visibility private
Discorb::Channel::new
# File lib/discorb/channel.rb, line 252 def initialize(client, data, no_cache: false) super @threads = Dictionary.new end
Public Instance Methods
Create an invite in the channel. @macro async @macro http
@param [Integer] max_age The max age of the invite. @param [Integer] max_uses The max uses of the invite. @param [Boolean] temporary Whether the invite is temporary. @param [Boolean] unique Whether the invite is unique.
@note if it's `false` it may return existing invite.
@param [String] reason The reason of creating the invite.
@return [Invite] The created invite.
# 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 in the channel. @macro async @macro http
@param [String] name The name of the webhook. @param [Discorb::Image] avatar The avatar of the webhook.
@return [Discorb::Webhook::IncomingWebhook] The created webhook.
# 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
Bulk delete messages in the channel. @macro async @macro http
@param [Discorb::Message] messages The messages to delete. @param [Boolean] force Whether to ignore the validation for message (14 days limit).
# 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 the channel's permission overwrite. @macro async @macro http
@param [Discorb::Role, Discorb::Member] target The target of the overwrite. @param [String] reason The reason of deleting the overwrite.
# 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
Edits the channel. @macro async @macro http @macro edit
@param [String] name The name of the channel. @param [Integer] position The position of the channel. @param [Discorb::CategoryChannel, nil] category The parent of channel. Specify `nil` to remove the parent. @param [Discorb::CategoryChannel, nil] parent Alias of `category`. @param [String] topic The topic of the channel. @param [Boolean] nsfw Whether the channel is nsfw. @param [Boolean] announce Whether the channel is announce channel. @param [Integer] rate_limit_per_user
The rate limit per user (Slowmode) in the channel. @param [Integer] slowmode Alias of `rate_limit_per_user`. @param [Integer] default_auto_archive_duration The default auto archive duration of the channel. @param [Integer] archive_in Alias of `default_auto_archive_duration`. @param [String] reason The reason of editing the channel.
@return [self] The edited channel.
# 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 in the channel. @macro async @macro http
@return [Array<Discorb::ThreadChannel>] The archived private threads in the channel.
# File lib/discorb/channel.rb, line 579 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 threads in the channel. @macro async @macro http
@return [Array<Discorb::ThreadChannel>] The archived threads in the channel.
# File lib/discorb/channel.rb, line 565 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 the channel's invites. @macro async @macro http
@return [Array<Discorb::Invite>] The invites in the channel.
# 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 in the channel. @macro async @macro http
@param [Integer] limit The limit of threads to fetch. @param [Time] before <description>
@return [Array<Discorb::ThreadChannel>] The joined archived private threads in the channel.
# File lib/discorb/channel.rb, line 596 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 the pinned messages in the channel. @macro async @macro http
@return [Array<Discorb::Message>] The pinned messages in the channel.
# File lib/discorb/channel.rb, line 492 def fetch_pins Async do _resp, data = @client.http.get("/channels/#{@id}/pins").wait data.map { |pin| Message.new(@client, pin) } end end
Fetch webhooks in the channel. @macro async @macro http
@return [Array<Discorb::Webhook>] The webhooks in the channel.
# 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 the existing announcement channel. @macro async @macro http
@param [Discorb::NewsChannel] target The channel to follow. @param [String] reason The reason of following the channel.
# 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 the existing announcement channel from self. @macro async @macro http
@param [Discorb::TextChannel] target The channel to follow to. @param [String] reason The reason of following the channel.
# 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
Pin a message in the channel. @macro async @macro http
@param [Discorb::Message] message The message to pin. @param [String] reason The reason of pinning the message.
# File lib/discorb/channel.rb, line 507 def pin_message(message, reason: nil) Async do @client.http.put("/channels/#{@id}/pins/#{message.id}", {}, audit_log_reason: reason).wait end end
Set the channel's permission overwrite. @macro async @macro http
@param [Discorb::Role, Discorb::Member] target The target of the overwrite. @param [String] reason The reason of setting the overwrite. @param [Symbol => Boolean] perms The permission overwrites to replace.
# 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 in the channel. @macro async @macro http
@param [String] name The name of the thread. @param [Discorb::Message] message The message to start the thread. @param [Integer] auto_archive_duration The duration of auto-archiving. @param [Boolean] public Whether the thread is public. @param [String] reason The reason of starting the thread.
@return [Discorb::ThreadChannel] The started thread.
# File lib/discorb/channel.rb, line 540 def start_thread(name, message: nil, auto_archive_duration: 1440, public: true, 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, }, 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
Unpin a message in the channel. @macro async @macro http
@param [Discorb::Message] message The message to unpin. @param [String] reason The reason of unpinning the message.
# File lib/discorb/channel.rb, line 521 def unpin_message(message, reason: nil) Async do @client.http.delete("/channels/#{@id}/pins/#{message.id}", {}, audit_log_reason: reason).wait end end
Private Instance Methods
# File lib/discorb/channel.rb, line 618 def _set_data(data) @topic = data[:topic] @nsfw = data[:nsfw] @last_message_id = data[:last_message_id] @rate_limit_per_user = data[:rate_limit_per_user] @last_pin_timestamp = data[:last_pin_timestamp] && Time.iso8601(data[:last_pin_timestamp]) super end