Module: Discorb::Messageable
- Included in:
- DMChannel, TextChannel, ThreadChannel, User, VoiceChannel
- Defined in:
- lib/discorb/modules.rb
Overview
Module for sending and reading messages.
Instance Method Summary collapse
-
#delete_message!(message_id, reason: nil) -> Async::Task<void>
(also: #destroy_message!)
Delete a message.
-
#edit_message(message_id, content = Discorb::Unset, embed: Discorb::Unset, embeds: Discorb::Unset, allowed_mentions: Discorb::Unset, attachments: Discorb::Unset, components: Discorb::Unset, supress: Discorb::Unset) -> Async::Task<void>
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) -> Async::Task<void>
Pin a message in the channel.
-
#post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, reference: nil, components: nil, attachment: nil, attachments: 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) -> Async::Task<void>
Unpin a message in the channel.
Instance Method Details
#delete_message!(message_id, reason: nil) -> Async::Task<void> Also known as: destroy_message!
Delete a message.
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/discorb/modules.rb', line 138 def (, reason: nil) Async do @client.http.request( Route.new( "/channels/#{channel_id.wait}/messages/#{}", "//channels/:channel_id/messages/:message_id", :delete ), {}, audit_log_reason: reason, ).wait end end |
#edit_message(message_id, content = Discorb::Unset, embed: Discorb::Unset, embeds: Discorb::Unset, allowed_mentions: Discorb::Unset, attachments: Discorb::Unset, components: Discorb::Unset, supress: Discorb::Unset) -> Async::Task<void>
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
.
Edit a message.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/discorb/modules.rb', line 85 def ( , content = Discorb::Unset, embed: Discorb::Unset, embeds: Discorb::Unset, allowed_mentions: Discorb::Unset, attachments: Discorb::Unset, components: Discorb::Unset, supress: Discorb::Unset ) Async do payload = {} payload[:content] = content if content != Discorb::Unset = if != Discorb::Unset [] elsif != Discorb::Unset end payload[:embeds] = .map(&:to_hash) if payload[:allowed_mentions] = if allowed_mentions == Discorb::Unset @client.allowed_mentions.to_hash else allowed_mentions.to_hash(@client.allowed_mentions) end payload[:components] = Component.to_payload(components) if components != Discorb::Unset payload[:flags] = (supress ? 1 << 2 : 0) if supress != Discorb::Unset if != Discorb::Unset payload[:attachments] = .map.with_index do |a, i| { id: i, filename: a.filename, description: a.description, } end end @client.http.multipart_request( Route.new("/channels/#{channel_id.wait}/messages/#{}", "//channels/:channel_id/messages/:message_id", :patch), payload, == Discorb::Unset ? [] : ).wait end end |
#fetch_message(id) -> Async::Task<Discorb::Message>
Fetch a message from ID.
160 161 162 163 164 165 166 |
# File 'lib/discorb/modules.rb', line 160 def (id) Async do _resp, data = @client.http.request(Route.new("/channels/#{channel_id.wait}/messages/#{id}", "//channels/:channel_id/messages/:message_id", :get)).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>>
Fetch a message history.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/discorb/modules.rb', line 179 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.request( Route.new( "/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}", "//channels/:channel_id/messages", :get ) ).wait .map { |m| Message.new(@client, m.merge({ guild_id: @guild_id.to_s })) } end end |
#fetch_pins -> Async::Task<Array<Discorb::Message>>
Fetch the pinned messages in the channel.
203 204 205 206 207 208 209 |
# File 'lib/discorb/modules.rb', line 203 def fetch_pins Async do _resp, data = @client.http.request(Route.new("/channels/#{channel_id.wait}/pins", "//channels/:channel_id/pins", :get)).wait data.map { |pin| Message.new(@client, pin) } end end |
#pin_message(message, reason: nil) -> Async::Task<void>
Pin a message in the channel.
220 221 222 223 224 225 226 227 |
# File 'lib/discorb/modules.rb', line 220 def (, reason: nil) Async do @client.http.request( Route.new("/channels/#{channel_id.wait}/pins/#{.id}", "//channels/:channel_id/pins/:message_id", :put), {}, audit_log_reason: reason, ).wait end end |
#post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, reference: nil, components: nil, attachment: nil, attachments: nil) -> Async::Task<Discorb::Message> Also known as: send_message
Post a message to the channel.
24 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/discorb/modules.rb', line 24 def post( content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, reference: nil, components: nil, attachment: nil, attachments: 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.to_hash if reference payload[:components] = Component.to_payload(components) if components ||= ? [] : [] payload[:attachments] = .map.with_index do |a, i| { id: i, filename: a.filename, description: a.description, } end _resp, data = @client.http.multipart_request( Route.new("/channels/#{channel_id.wait}/messages", "//channels/:channel_id/messages", :post), payload, ).wait Message.new(@client, data.merge({ guild_id: @guild_id.to_s })) end end |
#typing -> Object
Trigger the typing indicator in the channel. If block is given, trigger typing indicator during executing block.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/discorb/modules.rb', line 259 def typing if block_given? begin post_task = Async do loop do @client.http.request(Route.new("/channels/#{@id}/typing", "//channels/:channel_id/typing", :post), {}) sleep(5) end end ret = yield ensure post_task.stop end ret else Async do |_task| @client.http.request(Route.new("/channels/#{@id}/typing", "//channels/:channel_id/typing", :post), {}) end end end |
#unpin_message(message, reason: nil) -> Async::Task<void>
Unpin a message in the channel.
238 239 240 241 242 243 244 245 |
# File 'lib/discorb/modules.rb', line 238 def (, reason: nil) Async do @client.http.request( Route.new("/channels/#{channel_id.wait}/pins/#{.id}", "//channels/:channel_id/pins/:message_id", :delete), {}, audit_log_reason: reason, ).wait end end |