Class: Discordrb::Channel
- Inherits:
-
Object
- Object
- Discordrb::Channel
- 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 }.freeze
Instance Attribute Summary collapse
-
#archive_timestamp ⇒ Time?
readonly
The timestamp of when this threads status last changed.
-
#archived ⇒ true, ...
(also: #archived?)
readonly
Whether or not this thread is archived.
-
#auto_archive_duration ⇒ Integer?
readonly
How long after the last message before a thread is automatically archived.
-
#bitrate ⇒ Integer
The bitrate (in bps) of the channel.
-
#invitable ⇒ true, ...
(also: #invitable?)
readonly
For private threads, determines whether non-moderators can add other non-moderators to a thread.
-
#join_timestamp ⇒ Time?
readonly
When the current user joined this thread.
-
#last_pin_timestamp ⇒ Time?
readonly
The time at when the last pinned message was pinned in this channel.
-
#locked ⇒ true, ...
(also: #locked?)
readonly
Whether this thread is locked or not.
-
#member_count ⇒ Integer?
readonly
An approximate count of members in a thread.
-
#member_flags ⇒ Integer?
readonly
Member flags for this thread, used for notifications.
-
#message_count ⇒ Integer?
readonly
An approximate count of messages sent in a thread, excluding deleted messages.
-
#name ⇒ String
This channel's name.
-
#nsfw ⇒ true, false
(also: #nsfw?)
If this channel is marked as nsfw.
-
#owner_id ⇒ Integer?
readonly
The ID of the owner of the group channel or nil if this is not a group channel.
-
#parent_id ⇒ Integer?
readonly
The ID of the parent channel, if this channel is inside a category.
-
#position ⇒ Integer
The channel's position on the channel list.
-
#rate_limit_per_user ⇒ Integer
(also: #slowmode_rate)
The amount of time (in seconds) users need to wait to send in between messages.
-
#recipients ⇒ Array<Recipient>?
readonly
The array of recipients of the private messages, or nil if this is not a Private channel.
-
#topic ⇒ String
The channel's topic.
-
#type ⇒ Integer
readonly
The type of this channel.
-
#user_limit ⇒ Integer
(also: #limit)
The amount of users that can be in the channel.
Attributes included from IDObject
Threads collapse
-
#add_member(member) ⇒ Object
Add a member to the thread.
-
#join_thread ⇒ Object
Join this thread.
-
#leave_thread ⇒ Object
Leave this thread.
-
#members ⇒ Object
Members in the thread.
- #remove_member(member) ⇒ Object
Instance Method Summary collapse
-
#add_group_users(user_ids) ⇒ Channel
(also: #add_group_user)
Adds a user to a group channel.
-
#await(key, attributes = {}, &block) ⇒ Object
deprecated
Deprecated.
Will be changed to blocking behavior in v4.0. Use #await! instead.
-
#await!(attributes = {}, &block) ⇒ Object
Add a blocking Await for a message in this channel.
-
#category ⇒ Channel?
(also: #parent)
The category channel, if this channel is in a category.
-
#category=(channel) ⇒ Object
(also: #parent=)
Sets this channels parent category.
-
#category? ⇒ true, false
Whether or not this channel is a category channel.
-
#children ⇒ Array<Channel>
(also: #channels)
Returns the children of this channel, if it is a category.
-
#create_group(user_ids) ⇒ Channel
Creates a Group channel.
-
#create_webhook(name, avatar = nil, reason = nil) ⇒ Webhook
Creates a webhook in this channel.
-
#default_channel? ⇒ true, false
(also: #default?)
Whether or not this channel is the default channel.
-
#define_overwrite(thing, allow = 0, deny = 0, reason: nil) ⇒ 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.
-
#delete(reason = nil) ⇒ Object
Permanently deletes this channel.
-
#delete_message(message) ⇒ Object
Deletes a message on this channel.
-
#delete_messages(messages, strict = false, reason = nil) ⇒ Integer
Deletes a collection of messages.
-
#delete_overwrite(target, reason = nil) ⇒ Object
Deletes a permission overwrite for this channel.
-
#group? ⇒ true, false
Whether or not this channel is a group channel.
-
#history(amount, before_id = nil, after_id = nil, around_id = nil) ⇒ Array<Message>
Retrieves some of this channel's message history.
-
#inspect ⇒ Object
The default
inspect
method is overwritten to give more useful output. -
#invites ⇒ Array<Invite>
Requests a list of Invites to the channel.
-
#leave_group ⇒ Object
(also: #leave)
Leaves the group.
-
#link ⇒ String
(also: #jump_link)
A URL that a user can use to navigate to this channel in the client.
-
#load_message(message_id) ⇒ Message?
(also: #message)
Returns a single message from this channel's history by ID.
-
#make_invite(max_age = 0, max_uses = 0, temporary = false, unique = false, reason = nil) ⇒ Invite
(also: #invite)
Creates a new invite to this channel.
-
#member_overwrites ⇒ Overwrite
Any member-type permission overwrites on this channel.
-
#mention ⇒ String
A string that will mention the channel as a clickable link on Discord.
-
#news? ⇒ true, false
Whether or not this channel is a news channel.
-
#news_thread? ⇒ true, false
Whether or not this channel is a news thread.
-
#permission_overwrites(type = nil) ⇒ Object
(also: #overwrites)
This channel's permission overwrites.
-
#permission_overwrites=(overwrites) ⇒ Object
Bulk sets this channels permission overwrites.
-
#pins(limit: 50) ⇒ Array<Message>
Requests the pinned messages in a channel.
-
#pm? ⇒ true, false
Whether or not this channel is a PM channel.
-
#private? ⇒ true, false
Whether or not this channel is a PM or group channel.
-
#private_thread? ⇒ true, false
Whether or not this channel is a private thread.
-
#prune(amount, strict = false, reason = nil) {|message| ... } ⇒ Integer
Delete the last N messages on this channel.
-
#public_thread? ⇒ true, false
Whether or not this channel is a public thread.
-
#recipient ⇒ Recipient?
The recipient of the private messages, or nil if this is not a PM channel.
-
#remove_group_users(user_ids) ⇒ Channel
(also: #remove_group_user)
Removes a user from a group channel.
-
#role_overwrites ⇒ Overwrite
Any role-type permission overwrites on this channel.
-
#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.
-
#send_file(file, caption: nil, tts: false, filename: nil, spoiler: nil) ⇒ Object
Sends a file to this channel.
-
#send_message(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0) ⇒ Message
(also: #send)
Sends a message to this channel.
-
#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.
-
#send_multiple(content) ⇒ Object
Sends multiple messages to a channel.
-
#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.
-
#server ⇒ Server?
The server this channel is on.
-
#slowmode? ⇒ true, false
Whether or not this channel has slowmode enabled.
-
#sort_after(other = nil, lock_permissions = false) ⇒ Object
Sorts this channel's position to follow another channel.
-
#split_send(content) ⇒ Object
Splits a message into chunks whose length is at most the Discord character limit, then sends them individually.
-
#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.
-
#start_thread(name, auto_archive_duration, message: nil, type: 11) ⇒ Channel
Start a thread.
-
#start_typing ⇒ Object
Starts typing, which displays the typing indicator on the client for five seconds.
-
#store? ⇒ true, false
Whether or not this channel is a store channel.
-
#sync_overwrites ⇒ Object
(also: #sync)
Syncs this channels overwrites with its parent category.
-
#synchronized? ⇒ true, ...
(also: #synced?)
Whether this channels permissions match the permission overwrites of the category that it's in, or nil if it is not in a category.
-
#text? ⇒ true, false
Whether or not this channel is a text channel.
-
#text_channels ⇒ Array<Channel>
Returns the text channels in this category, if it is a category channel.
-
#thread? ⇒ true, false
Whether or not this channel is a thread.
-
#users ⇒ Array<Member>
The list of users currently in this channel.
-
#voice? ⇒ true, false
Whether or not this channel is a voice channel.
-
#voice_channels ⇒ Array<Channel>
Returns the voice channels in this category, if it is a category channel.
-
#webhooks ⇒ Array<Webhook>
Requests a list of Webhooks on the channel.
Methods included from IDObject
#==, #creation_time, synthesise
Instance Attribute Details
#archive_timestamp ⇒ Time? (readonly)
Returns The timestamp of when this threads status last changed.
81 82 83 |
# File 'lib/discordrb/data/channel.rb', line 81 def @archive_timestamp end |
#archived ⇒ true, ... (readonly) Also known as: archived?
Returns Whether or not this thread is archived.
74 75 76 |
# File 'lib/discordrb/data/channel.rb', line 74 def archived @archived end |
#auto_archive_duration ⇒ Integer? (readonly)
Returns How long after the last message before a thread is automatically archived.
78 79 80 |
# File 'lib/discordrb/data/channel.rb', line 78 def auto_archive_duration @auto_archive_duration end |
#bitrate ⇒ Integer
Returns the bitrate (in bps) of the channel.
50 51 52 |
# File 'lib/discordrb/data/channel.rb', line 50 def bitrate @bitrate end |
#invitable ⇒ true, ... (readonly) Also known as: invitable?
Returns For private threads, determines whether non-moderators can add other non-moderators to a thread.
95 96 97 |
# File 'lib/discordrb/data/channel.rb', line 95 def invitable @invitable end |
#join_timestamp ⇒ Time? (readonly)
Returns When the current user joined this thread.
88 89 90 |
# File 'lib/discordrb/data/channel.rb', line 88 def @join_timestamp end |
#last_pin_timestamp ⇒ Time? (readonly)
Returns The time at when the last pinned message was pinned in this channel.
99 100 101 |
# File 'lib/discordrb/data/channel.rb', line 99 def @last_pin_timestamp end |
#locked ⇒ true, ... (readonly) Also known as: locked?
Returns Whether this thread is locked or not.
84 85 86 |
# File 'lib/discordrb/data/channel.rb', line 84 def locked @locked end |
#member_count ⇒ Integer? (readonly)
Returns An approximate count of members in a thread. Stops counting at 50.
71 72 73 |
# File 'lib/discordrb/data/channel.rb', line 71 def member_count @member_count end |
#member_flags ⇒ Integer? (readonly)
Returns Member flags for this thread, used for notifications.
91 92 93 |
# File 'lib/discordrb/data/channel.rb', line 91 def member_flags @member_flags end |
#message_count ⇒ Integer? (readonly)
Returns An approximate count of messages sent in a thread, excluding deleted messages.
68 69 70 |
# File 'lib/discordrb/data/channel.rb', line 68 def @message_count end |
#name ⇒ String
Returns this channel's name.
29 30 31 |
# File 'lib/discordrb/data/channel.rb', line 29 def name @name end |
#nsfw ⇒ true, false Also known as: nsfw?
Returns if this channel is marked as nsfw.
60 61 62 |
# File 'lib/discordrb/data/channel.rb', line 60 def nsfw @nsfw end |
#owner_id ⇒ Integer? (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.
41 42 43 |
# File 'lib/discordrb/data/channel.rb', line 41 def owner_id @owner_id end |
#parent_id ⇒ Integer? (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.
33 34 35 |
# File 'lib/discordrb/data/channel.rb', line 33 def parent_id @parent_id end |
#position ⇒ Integer
Returns the channel's position on the channel list.
57 58 59 |
# File 'lib/discordrb/data/channel.rb', line 57 def position @position end |
#rate_limit_per_user ⇒ Integer Also known as: slowmode_rate
Returns the amount of time (in seconds) users need to wait to send in between messages.
64 65 66 |
# File 'lib/discordrb/data/channel.rb', line 64 def rate_limit_per_user @rate_limit_per_user end |
#recipients ⇒ Array<Recipient>? (readonly)
Returns the array of recipients of the private messages, or nil if this is not a Private channel.
44 45 46 |
# File 'lib/discordrb/data/channel.rb', line 44 def recipients @recipients end |
#topic ⇒ String
Returns the channel's topic.
47 48 49 |
# File 'lib/discordrb/data/channel.rb', line 47 def topic @topic end |
#type ⇒ Integer (readonly)
Returns the type of this channel.
37 38 39 |
# File 'lib/discordrb/data/channel.rb', line 37 def type @type end |
#user_limit ⇒ Integer Also known as: limit
Returns the amount of users that can be in the channel. 0
means it is unlimited.
53 54 55 |
# File 'lib/discordrb/data/channel.rb', line 53 def user_limit @user_limit end |
Instance Method Details
#add_group_users(user_ids) ⇒ Channel Also known as: add_group_user
Adds a user to a group channel.
849 850 851 852 853 854 855 856 857 |
# File 'lib/discordrb/data/channel.rb', line 849 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
993 994 995 |
# File 'lib/discordrb/data/channel.rb', line 993 def add_member(member) @bot.add_thread_member(@id, member) end |
#await(key, attributes = {}, &block) ⇒ Object
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.
800 801 802 |
# File 'lib/discordrb/data/channel.rb', line 800 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.
807 808 809 |
# File 'lib/discordrb/data/channel.rb', line 807 def await!(attributes = {}, &block) @bot.add_await!(Discordrb::Events::MessageEvent, { in: @id }.merge(attributes), &block) end |
#category ⇒ Channel? Also known as: parent
Returns the category channel, if this channel is in a category.
239 240 241 |
# File 'lib/discordrb/data/channel.rb', line 239 def category @bot.channel(@parent_id) if @parent_id end |
#category=(channel) ⇒ Object Also known as: parent=
Sets this channels parent category
248 249 250 251 252 253 |
# File 'lib/discordrb/data/channel.rb', line 248 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
Returns whether or not this channel is a category channel.
204 205 206 |
# File 'lib/discordrb/data/channel.rb', line 204 def category? @type == 4 end |
#children ⇒ Array<Channel> Also known as: channels
Returns the children of this channel, if it is a category. Otherwise returns an empty array.
387 388 389 390 391 |
# File 'lib/discordrb/data/channel.rb', line 387 def children return [] unless category? server.channels.select { |c| c.parent_id == id } end |
#create_group(user_ids) ⇒ Channel
Creates a Group channel
838 839 840 841 842 843 844 |
# File 'lib/discordrb/data/channel.rb', line 838 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
891 892 893 894 895 896 897 |
# File 'lib/discordrb/data/channel.rb', line 891 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?
Returns whether or not this channel is the default channel.
418 419 420 |
# File 'lib/discordrb/data/channel.rb', line 418 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.
626 627 628 629 630 631 632 633 634 635 |
# File 'lib/discordrb/data/channel.rb', line 626 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.(@bot.token, @id, thing.id, thing.allow.bits, thing.deny.bits, thing.type, reason) end |
#delete(reason = nil) ⇒ Object
Permanently deletes this channel
563 564 565 |
# File 'lib/discordrb/data/channel.rb', line 563 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
557 558 559 |
# File 'lib/discordrb/data/channel.rb', line 557 def () API::Channel.(@bot.token, @id, .resolve_id) end |
#delete_messages(messages, strict = false, reason = nil) ⇒ Integer
Deletes a collection of messages
782 783 784 785 786 787 |
# File 'lib/discordrb/data/channel.rb', line 782 def (, strict = false, reason = nil) raise ArgumentError, 'Can only delete between 2 and 100 messages!' unless .count.between?(2, 100) .map!(&:resolve_id) bulk_delete(, strict, reason) end |
#delete_overwrite(target, reason = nil) ⇒ Object
Deletes a permission overwrite for this channel
640 641 642 643 644 |
# File 'lib/discordrb/data/channel.rb', line 640 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.(@bot.token, @id, target.resolve_id, reason) end |
#group? ⇒ true, false
Returns whether or not this channel is a group channel.
199 200 201 |
# File 'lib/discordrb/data/channel.rb', line 199 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.
694 695 696 697 |
# File 'lib/discordrb/data/channel.rb', line 694 def history(amount, before_id = nil, after_id = nil, around_id = nil) logs = API::Channel.(@bot.token, @id, amount, before_id, after_id, around_id) JSON.parse(logs).map { || Message.new(, @bot) } end |
#inspect ⇒ Object
The default inspect
method is overwritten to give more useful output.
1005 1006 1007 |
# File 'lib/discordrb/data/channel.rb', line 1005 def inspect "<Channel name=#{@name} id=#{@id} topic=\"#{@topic}\" type=#{@type} position=#{@position} server=#{@server || @server_id}>" end |
#invites ⇒ Array<Invite>
Requests a list of Invites to the channel.
910 911 912 913 914 915 |
# File 'lib/discordrb/data/channel.rb', line 910 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_thread ⇒ Object
Join this thread.
977 978 979 |
# File 'lib/discordrb/data/channel.rb', line 977 def join_thread @bot.join_thread(@id) end |
#leave_group ⇒ Object Also known as: leave
Leaves the group.
877 878 879 880 881 |
# File 'lib/discordrb/data/channel.rb', line 877 def leave_group raise 'Attempted to leave a non-group channel!' unless group? API::Channel.leave_group(@bot.token, @id) end |
#leave_thread ⇒ Object
Leave this thread
982 983 984 |
# File 'lib/discordrb/data/channel.rb', line 982 def leave_thread @bot.leave_thread(@id) end |
#link ⇒ String Also known as: jump_link
Returns a URL that a user can use to navigate to this channel in the client.
1059 1060 1061 |
# File 'lib/discordrb/data/channel.rb', line 1059 def link "https://discord.com/channels/#{@server_id || '@me'}/#{@channel.id}" end |
#load_message(message_id) ⇒ Message? Also known as: message
Returns a single message from this channel's history by ID.
710 711 712 713 714 715 716 717 |
# File 'lib/discordrb/data/channel.rb', line 710 def () raise ArgumentError, 'message_id cannot be nil' if .nil? response = API::Channel.(@bot.token, @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.
818 819 820 821 |
# File 'lib/discordrb/data/channel.rb', line 818 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 |
#member_overwrites ⇒ Overwrite
Returns any member-type permission overwrites on this channel.
408 409 410 |
# File 'lib/discordrb/data/channel.rb', line 408 def member_overwrites :member end |
#members ⇒ Object
Members in the thread.
987 988 989 |
# File 'lib/discordrb/data/channel.rb', line 987 def members @bot.thread_members[@id].collect { |id| @server_id ? @bot.member(@server_id, id) : @bot.user(id) } end |
#mention ⇒ String
Returns a string that will mention the channel as a clickable link on Discord.
107 108 109 |
# File 'lib/discordrb/data/channel.rb', line 107 def mention "<##{@id}>" end |
#news? ⇒ true, false
Returns whether or not this channel is a news channel.
209 210 211 |
# File 'lib/discordrb/data/channel.rb', line 209 def news? @type == 5 end |
#news_thread? ⇒ true, false
Returns whether or not this channel is a news thread.
219 220 221 |
# File 'lib/discordrb/data/channel.rb', line 219 def news_thread? @type == 10 end |
#permission_overwrites ⇒ Hash<Integer => Overwrite> #permission_overwrites(type) ⇒ Array<Overwrite> Also known as: overwrites
This channel's permission overwrites
341 342 343 344 345 |
# File 'lib/discordrb/data/channel.rb', line 341 def (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
351 352 353 |
# File 'lib/discordrb/data/channel.rb', line 351 def (overwrites) update_channel_data(permission_overwrites: overwrites) end |
#pins(limit: 50) ⇒ Array<Message>
Requests the pinned messages in a channel.
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 |
# File 'lib/discordrb/data/channel.rb', line 724 def pins(limit: 50) get_pins = proc do |fetch_limit, before = nil| resp = API::Channel.(@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.
189 190 191 |
# File 'lib/discordrb/data/channel.rb', line 189 def pm? @type == 1 end |
#private? ⇒ true, false
Returns whether or not this channel is a PM or group channel.
102 103 104 |
# File 'lib/discordrb/data/channel.rb', line 102 def private? pm? || group? end |
#private_thread? ⇒ true, false
Returns whether or not this channel is a private thread.
229 230 231 |
# File 'lib/discordrb/data/channel.rb', line 229 def private_thread? @type == 12 end |
#prune(amount, strict = false, reason = nil) {|message| ... } ⇒ Integer
Delete the last N messages on this channel.
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 |
# File 'lib/discordrb/data/channel.rb', line 754 def prune(amount, strict = false, reason = nil, &block) raise ArgumentError, 'Can only delete between 1 and 100 messages!' unless amount.between?(1, 100) = if block history(amount).select(&block).map(&:id) else history_ids(amount) end case .size when 0 0 when 1 API::Channel.(@bot.token, @id, .first, reason) 1 else bulk_delete(, strict, reason) end end |
#public_thread? ⇒ true, false
Returns whether or not this channel is a public thread.
224 225 226 |
# File 'lib/discordrb/data/channel.rb', line 224 def public_thread? @type == 11 end |
#recipient ⇒ Recipient?
Returns the recipient of the private messages, or nil if this is not a PM channel.
112 113 114 |
# File 'lib/discordrb/data/channel.rb', line 112 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.
864 865 866 867 868 869 870 871 872 |
# File 'lib/discordrb/data/channel.rb', line 864 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
998 999 1000 |
# File 'lib/discordrb/data/channel.rb', line 998 def remove_member(member) @bot.remove_thread_member(@id, member) end |
#role_overwrites ⇒ Overwrite
Returns any role-type permission overwrites on this channel.
413 414 415 |
# File 'lib/discordrb/data/channel.rb', line 413 def role_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.
476 477 478 479 480 481 482 483 |
# File 'lib/discordrb/data/channel.rb', line 476 def ( = '', = nil, = nil, tts = false, allowed_mentions = nil, = nil, components = nil, flags = 0) ||= Discordrb::Webhooks::Embed.new view = Discordrb::Webhooks::View.new yield(, view) if block_given? (, tts, , , allowed_mentions, , 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.
551 552 553 |
# File 'lib/discordrb/data/channel.rb', line 551 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.
439 440 441 |
# File 'lib/discordrb/data/channel.rb', line 439 def (content, tts = false, = nil, = nil, allowed_mentions = nil, = nil, components = nil, flags = 0) @bot.(@id, content, tts, , , allowed_mentions, , 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.
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'lib/discordrb/data/channel.rb', line 508 def (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 &.each { || builder << } 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.(@id, builder[:content], timeout, builder[:tts], builder[:embeds], , builder[:allowed_mentions], reference, components&.to_a || view.to_a, flags, nonce, enforce_nonce) else @bot.(@id, builder[:content], builder[:tts], builder[:embeds], , 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
532 533 534 |
# File 'lib/discordrb/data/channel.rb', line 532 def send_multiple(content) content.each { |text| (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.
455 456 457 |
# File 'lib/discordrb/data/channel.rb', line 455 def (content, timeout, tts = false, = nil, = nil, allowed_mentions = nil, = nil, components = nil, flags = 0) @bot.(@id, content, timeout, tts, , , allowed_mentions, , components, flags) end |
#server ⇒ Server?
Returns the server this channel is on. If this channel is a PM channel, it will be nil.
173 174 175 176 177 178 179 180 181 |
# File 'lib/discordrb/data/channel.rb', line 173 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.
425 426 427 |
# File 'lib/discordrb/data/channel.rb', line 425 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.
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 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 |
# File 'lib/discordrb/data/channel.rb', line 262 def sort_after(other = nil, = 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 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!
538 539 540 541 |
# File 'lib/discordrb/data/channel.rb', line 538 def split_send(content) send_multiple(Discordrb.(content)) nil 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.
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 |
# File 'lib/discordrb/data/channel.rb', line 954 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 &.each { || builder << } 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 = { 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, .compact, , rate_limit_per_user, auto_archive_duration, &.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.
924 925 926 927 928 929 930 931 932 933 934 935 |
# File 'lib/discordrb/data/channel.rb', line 924 def start_thread(name, auto_archive_duration, message: nil, type: 11) = &.id || type = TYPES[type] || type data = if API::Channel.(@bot.token, @id, , name, auto_archive_duration) else API::Channel.(@bot.token, @id, name, auto_archive_duration, type) end Channel.new(JSON.parse(data), @bot, @server) end |
#start_typing ⇒ Object
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)
830 831 832 |
# File 'lib/discordrb/data/channel.rb', line 830 def start_typing API::Channel.start_typing(@bot.token, @id) end |
#store? ⇒ true, false
Returns whether or not this channel is a store channel.
214 215 216 |
# File 'lib/discordrb/data/channel.rb', line 214 def store? @type == 6 end |
#sync_overwrites ⇒ Object Also known as: sync
Syncs this channels overwrites with its parent category
368 369 370 371 372 |
# File 'lib/discordrb/data/channel.rb', line 368 def sync_overwrites raise 'Cannot sync overwrites on a channel with no parent category' unless parent self. = parent. 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.
377 378 379 380 381 |
# File 'lib/discordrb/data/channel.rb', line 377 def synchronized? return unless parent == parent. end |
#text? ⇒ true, false
Returns whether or not this channel is a text channel.
184 185 186 |
# File 'lib/discordrb/data/channel.rb', line 184 def text? @type.zero? end |
#text_channels ⇒ Array<Channel>
Returns the text channels in this category, if it is a category channel. Otherwise returns an empty array.
397 398 399 |
# File 'lib/discordrb/data/channel.rb', line 397 def text_channels children.select(&:text?) end |
#thread? ⇒ true, false
Returns whether or not this channel is a thread.
234 235 236 |
# File 'lib/discordrb/data/channel.rb', line 234 def thread? news_thread? || public_thread? || private_thread? end |
#users ⇒ Array<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.
673 674 675 676 677 678 679 |
# File 'lib/discordrb/data/channel.rb', line 673 def users if text? server.online_members(include_idle: true).select { |u| u. 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.
194 195 196 |
# File 'lib/discordrb/data/channel.rb', line 194 def voice? @type == 2 end |
#voice_channels ⇒ Array<Channel>
Returns the voice channels in this category, if it is a category channel. Otherwise returns an empty array.
403 404 405 |
# File 'lib/discordrb/data/channel.rb', line 403 def voice_channels children.select(&:voice?) end |
#webhooks ⇒ Array<Webhook>
Requests a list of Webhooks on the channel.
901 902 903 904 905 906 |
# File 'lib/discordrb/data/channel.rb', line 901 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 |