Class: Discordrb::Channel
- Inherits:
-
Object
- Object
- Discordrb::Channel
- Includes:
- IDObject
- Defined in:
- lib/discordrb/data.rb
Overview
A Discord channel, including data like the topic
Constant Summary collapse
- TEXT_TYPE =
The type string that stands for a text channel
'text'.freeze
- VOICE_TYPE =
The type string that stands for a voice channel
'voice'.freeze
Instance Attribute Summary collapse
-
#name ⇒ String
This channel's name.
-
#permission_overwrites ⇒ Hash<Integer => OpenStruct>
readonly
This channel's permission overwrites, represented as a hash of role/user ID to an OpenStruct which has the
allowanddenyproperties which are Permissions objects respectively. -
#position ⇒ Integer
The channel's position on the channel list.
-
#recipient ⇒ Recipient?
readonly
The recipient of the private messages, or nil if this is not a PM channel.
-
#server ⇒ Server?
readonly
The server this channel is on.
-
#topic ⇒ String
The channel's topic.
-
#type ⇒ String
readonly
The type of this channel (currently either 'text' or 'voice').
Attributes included from IDObject
Instance Method Summary collapse
-
#await(key, attributes = {}, &block) ⇒ Object
Add an Await for a message in this channel.
-
#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.
-
#delete ⇒ Object
Permanently deletes this channel.
-
#history(amount, before_id = nil, after_id = nil) ⇒ Array<Message>
Retrieves some of this channel's message history.
-
#inspect ⇒ Object
The inspect method is overwritten to give more useful output.
-
#make_invite(max_age = 0, max_uses = 0, temporary = false, xkcd = false) ⇒ Invite
(also: #invite)
Creates a new invite to this channel.
-
#mention ⇒ String
A string that will mention the channel as a clickable link on Discord.
-
#private? ⇒ true, false
Whether or not this channel is a PM channel.
-
#prune(amount) ⇒ Object
Deletes the last N messages on this channel.
-
#send_file(file) ⇒ Object
Sends a file to this channel.
-
#send_message(content, tts = false) ⇒ Message
(also: #send, #message)
Sends a message to this channel.
-
#send_multiple(content) ⇒ Object
Sends multiple messages to a channel.
-
#split_send(content) ⇒ Object
Splits a message into chunks whose length is at most the Discord character limit, then sends them individually.
-
#start_typing ⇒ Object
Starts typing, which displays the typing indicator on the client for five seconds.
-
#text? ⇒ true, false
Whether or not this channel is a text channel.
-
#users ⇒ Array<Member>
The list of users currently in this channel.
-
#voice? ⇒ true, false
Whether or not this channel is a voice channel.
Methods included from IDObject
Instance Attribute Details
#name ⇒ String
Returns this channel's name.
669 670 671 |
# File 'lib/discordrb/data.rb', line 669 def name @name end |
#permission_overwrites ⇒ Hash<Integer => OpenStruct> (readonly)
This channel's permission overwrites, represented as a hash of role/user ID to an OpenStruct which has the
allow and deny properties which are Permissions objects respectively.
689 690 691 |
# File 'lib/discordrb/data.rb', line 689 def end |
#position ⇒ Integer
Returns the channel's position on the channel list.
684 685 686 |
# File 'lib/discordrb/data.rb', line 684 def position @position end |
#recipient ⇒ Recipient? (readonly)
Returns the recipient of the private messages, or nil if this is not a PM channel.
678 679 680 |
# File 'lib/discordrb/data.rb', line 678 def recipient @recipient end |
#server ⇒ Server? (readonly)
Returns the server this channel is on. If this channel is a PM channel, it will be nil.
672 673 674 |
# File 'lib/discordrb/data.rb', line 672 def server @server end |
#topic ⇒ String
Returns the channel's topic.
681 682 683 |
# File 'lib/discordrb/data.rb', line 681 def topic @topic end |
#type ⇒ String (readonly)
Returns the type of this channel (currently either 'text' or 'voice').
675 676 677 |
# File 'lib/discordrb/data.rb', line 675 def type @type end |
Instance Method Details
#await(key, attributes = {}, &block) ⇒ Object
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.
895 896 897 |
# File 'lib/discordrb/data.rb', line 895 def await(key, attributes = {}, &block) @bot.add_await(key, Discordrb::Events::MessageEvent, { in: @id }.merge(attributes), &block) end |
#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.
818 819 820 821 822 823 824 825 826 827 |
# File 'lib/discordrb/data.rb', line 818 def define_overwrite(thing, allow, deny) allow_bits = allow.respond_to?(:bits) ? allow.bits : allow deny_bits = deny.respond_to?(:bits) ? deny.bits : deny if thing.is_a? User API.update_user_overrides(@bot.token, @id, thing.id, allow_bits, deny_bits) elsif thing.is_a? Role API.update_role_overrides(@bot.token, @id, thing.id, allow_bits, deny_bits) end end |
#delete ⇒ Object
Permanently deletes this channel
777 778 779 |
# File 'lib/discordrb/data.rb', line 777 def delete API.delete_channel(@bot.token, @id) end |
#history(amount, before_id = nil, after_id = nil) ⇒ Array<Message>
Retrieves some of this channel's message history.
860 861 862 863 |
# File 'lib/discordrb/data.rb', line 860 def history(amount, before_id = nil, after_id = nil) logs = API.channel_log(@bot.token, @id, amount, before_id, after_id) JSON.parse(logs).map { || Message.new(, @bot) } end |
#inspect ⇒ Object
The inspect method is overwritten to give more useful output
922 923 924 |
# File 'lib/discordrb/data.rb', line 922 def inspect "<Channel name=#{@name} id=#{@id} topic=\"#{@topic}\" type=#{@type} position=#{@position} server=#{@server}>" end |
#make_invite(max_age = 0, max_uses = 0, temporary = false, xkcd = false) ⇒ Invite Also known as: invite
Creates a new invite to this channel.
905 906 907 908 |
# File 'lib/discordrb/data.rb', line 905 def make_invite(max_age = 0, max_uses = 0, temporary = false, xkcd = false) response = API.create_invite(@bot.token, @id, max_age, max_uses, temporary, xkcd) Invite.new(JSON.parse(response), @bot) end |
#mention ⇒ String
Returns a string that will mention the channel as a clickable link on Discord.
697 698 699 |
# File 'lib/discordrb/data.rb', line 697 def mention "<##{@id}>" end |
#private? ⇒ true, false
Returns whether or not this channel is a PM channel.
692 693 694 |
# File 'lib/discordrb/data.rb', line 692 def private? @server.nil? end |
#prune(amount) ⇒ Object
Each delete request is performed in a separate thread for performance reasons, so if a large number of messages are pruned, many threads will be created.
Deletes the last N messages on this channel.
870 871 872 873 874 875 876 877 878 879 880 881 882 883 |
# File 'lib/discordrb/data.rb', line 870 def prune(amount) raise ArgumentError, "Can't prune more than 100 messages!" if amount > 100 threads = [] history(amount).each do || threads << Thread.new { .delete } end # Make sure all requests have finished threads.each(&:join) # Delete the threads threads.map! { nil } end |
#send_file(file) ⇒ Object
Sends a file to this channel. If it is an image, it will be embedded.
772 773 774 |
# File 'lib/discordrb/data.rb', line 772 def send_file(file) @bot.send_file(@id, file) end |
#send_message(content, tts = false) ⇒ Message Also known as: send, message
Sends a message to this channel.
754 755 756 |
# File 'lib/discordrb/data.rb', line 754 def (content, tts = false) @bot.(@id, content, tts) end |
#send_multiple(content) ⇒ Object
Sends multiple messages to a channel
760 761 762 |
# File 'lib/discordrb/data.rb', line 760 def send_multiple(content) content.each { |e| (e) } 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!
766 767 768 |
# File 'lib/discordrb/data.rb', line 766 def split_send(content) send_multiple(Discordrb.(content)) 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)
913 914 915 |
# File 'lib/discordrb/data.rb', line 913 def start_typing API.start_typing(@bot.token, @id) end |
#text? ⇒ true, false
Returns whether or not this channel is a text channel.
741 742 743 |
# File 'lib/discordrb/data.rb', line 741 def text? @type == TEXT_TYPE end |
#users ⇒ Array<Member>
The list of users currently in this channel. This is mostly useful for a voice channel, for a text channel it will just return the users on the server that are online.
842 843 844 845 846 847 848 849 850 |
# File 'lib/discordrb/data.rb', line 842 def users if @type == 'text' @server.members.select { |u| u.status != :offline } else @server.members.select do |user| user.voice_channel.id == @id if user.voice_channel end end end |
#voice? ⇒ true, false
Returns whether or not this channel is a voice channel.
746 747 748 |
# File 'lib/discordrb/data.rb', line 746 def voice? @type == VOICE_TYPE end |