Module: Discorb::Messageable
- Included in:
- DMChannel, NewsChannel, TextChannel, ThreadChannel, User
- Defined in:
- lib/discorb/modules.rb
Overview
Module for sending and reading messages.
Instance Method Summary collapse
-
#delete_message!(message_id, reason: nil) -> Object
(also: #destroy_message!)
Delete a message.
-
#edit_message(message_id, content = nil, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, supress: nil) -> Object
Edit a message.
-
#fetch_message(id) -> Async::Task<Discorb::Message>
Fetch a message from ID.
-
#fetch_messages(limit = 50, before: nil, after: nil, around: nil) -> Async::Task<Array<Discorb::Message>>
Fetch a message history.
-
#fetch_pins -> Async::Task<Array<Discorb::Message>>
Fetch the pinned messages in the channel.
-
#pin_message(message, reason: nil) -> Object
Pin a message in the channel.
-
#post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, reference: nil, components: nil, file: nil, files: nil) -> Async::Task<Discorb::Message>
(also: #send_message)
Post a message to the channel.
-
#typing -> Object
Trigger the typing indicator in the channel.
-
#unpin_message(message, reason: nil) -> Object
Unpin a message in the channel.
Instance Method Details
#delete_message!(message_id, reason: nil) -> Object Also known as: destroy_message!
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Delete a message.
95 96 97 98 99 |
# File 'lib/discorb/modules.rb', line 95 def (, reason: nil) Async do @client.http.delete("/channels/#{channel_id.wait}/messages/#{}", audit_log_reason: reason).wait end end |
#edit_message(message_id, content = nil, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, supress: nil) -> Object
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Edit a message.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/discorb/modules.rb', line 68 def (, content = nil, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, supress: nil) Async do payload = {} payload[:content] = content if content = if [] elsif end payload[:embeds] = .map(&:to_hash) if payload[:allowed_mentions] = allowed_mentions ? allowed_mentions.to_hash(@client.allowed_mentions) : @client.allowed_mentions.to_hash payload[:components] = Component.to_payload(components) if components payload[:flags] = (supress ? 1 << 2 : 0) unless supress.nil? @client.http.patch("/channels/#{channel_id.wait}/messages/#{}", payload).wait end end |
#fetch_message(id) -> Async::Task<Discorb::Message>
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Fetch a message from ID.
113 114 115 116 117 118 |
# File 'lib/discorb/modules.rb', line 113 def (id) Async do _resp, data = @client.http.get("/channels/#{channel_id.wait}/messages/#{id}").wait Message.new(@client, data.merge({ guild_id: @guild_id.to_s })) end end |
#fetch_messages(limit = 50, before: nil, after: nil, around: nil) -> Async::Task<Array<Discorb::Message>>
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Fetch a message history.
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/discorb/modules.rb', line 132 def (limit = 50, before: nil, after: nil, around: nil) Async do params = { limit: limit, before: Discorb::Utils.try(after, :id), after: Discorb::Utils.try(around, :id), around: Discorb::Utils.try(before, :id), }.filter { |_k, v| !v.nil? }.to_h _resp, = @client.http.get("/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}").wait .map { |m| Message.new(@client, m.merge({ guild_id: @guild_id.to_s })) } end end |
#fetch_pins -> Async::Task<Array<Discorb::Message>>
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Fetch the pinned messages in the channel.
152 153 154 155 156 157 |
# File 'lib/discorb/modules.rb', line 152 def fetch_pins Async do _resp, data = @client.http.get("/channels/#{channel_id.wait}/pins").wait data.map { |pin| Message.new(@client, pin) } end end |
#pin_message(message, reason: nil) -> Object
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Pin a message in the channel.
167 168 169 170 171 |
# File 'lib/discorb/modules.rb', line 167 def (, reason: nil) Async do @client.http.put("/channels/#{channel_id.wait}/pins/#{.id}", {}, audit_log_reason: reason).wait end end |
#post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, reference: nil, components: nil, file: nil, files: nil) -> Async::Task<Discorb::Message> Also known as: send_message
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Post a message to the channel.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/discorb/modules.rb', line 25 def post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, reference: nil, components: nil, file: nil, files: nil) Async do payload = {} payload[:content] = content if content payload[:tts] = tts = if [] elsif end payload[:embeds] = .map(&:to_hash) if payload[:allowed_mentions] = allowed_mentions ? allowed_mentions.to_hash(@client.allowed_mentions) : @client.allowed_mentions.to_hash payload[:message_reference] = reference.to_reference if reference payload[:components] = Component.to_payload(components) if components files = [file] if file if files seperator, payload = HTTP.multipart(payload, files) headers = { "content-type" => "multipart/form-data; boundary=#{seperator}" } else headers = {} end _resp, data = @client.http.post("/channels/#{channel_id.wait}/messages", payload, headers: headers).wait Message.new(@client, data.merge({ guild_id: @guild_id.to_s })) end end |
#typing -> Object
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Trigger the typing indicator in the channel. If block is given, trigger typing indicator during executing block.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/discorb/modules.rb', line 200 def typing if block_given? begin post_task = Async do loop do @client.http.post("/channels/#{@id}/typing", {}) sleep(5) end end yield ensure post_task.stop end else Async do |task| @client.http.post("/channels/#{@id}/typing", {}) end end end |
#unpin_message(message, reason: nil) -> Object
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Unpin a message in the channel.
181 182 183 184 185 |
# File 'lib/discorb/modules.rb', line 181 def (, reason: nil) Async do @client.http.delete("/channels/#{channel_id.wait}/pins/#{.id}", audit_log_reason: reason).wait end end |