Module: RubyCord::Internal::Messageable

Included in:
DMChannel, Guild::TextChannel, Guild::ThreadChannel, Guild::VoiceChannel, User
Defined in:
lib/rubycord/internal/messageable.rb

Overview

Module for sending and reading messages.

Instance Method Summary collapse

Instance Method Details

#delete_message(message_id, reason: nil) ⇒ Async::Task<void> Also known as: destroy_message

Delete a message.

Parameters:

  • message_id (#to_s)

    The message id.

  • reason (String) (defaults to: nil)

    The reason for deleting the message.

Returns:

  • (Async::Task<void>)

    The task.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rubycord/internal/messageable.rb', line 151

def delete_message(message_id, reason: nil)
  Async do
    @client
      .http
      .request(
        RubyCord::Internal::Route.new(
          "/channels/#{channel_id.wait}/messages/#{message_id}",
          "//channels/:channel_id/messages/:message_id",
          :delete
        ),
        {},
        audit_log_reason: reason
      )
      .wait
  end
end

#edit_message(message_id, content = RubyCord::Unset, embed: RubyCord::Unset, embeds: RubyCord::Unset, allowed_mentions: RubyCord::Unset, attachments: RubyCord::Unset, components: RubyCord::Unset, supress: RubyCord::Unset) ⇒ Async::Task<void>

Note:

The arguments of this method are defaultly set to RubyCord::Unset. Specify value to set the value, if not don't specify or specify RubyCord::Unset.

Edit a message.

Parameters:

  • message_id (#to_s)

    The message id.

  • content (String) (defaults to: RubyCord::Unset)

    The message content.

  • embed (RubyCord::Embed) (defaults to: RubyCord::Unset)

    The embed to send.

  • embeds (Array<RubyCord::Embed>) (defaults to: RubyCord::Unset)

    The embeds to send.

  • allowed_mentions (RubyCord::AllowedMentions) (defaults to: RubyCord::Unset)

    The allowed mentions.

  • attachments (Array<RubyCord::Attachment>) (defaults to: RubyCord::Unset)

    The new attachments.

  • components (Array<RubyCord::Component>, Array<Array<RubyCord::Component>>) (defaults to: RubyCord::Unset)

    The components to send.

  • supress (Boolean) (defaults to: RubyCord::Unset)

    Whether to supress embeds.

Returns:

  • (Async::Task<void>)

    The task.



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
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rubycord/internal/messageable.rb', line 94

def edit_message(
  message_id,
  content = RubyCord::Unset,
  embed: RubyCord::Unset,
  embeds: RubyCord::Unset,
  allowed_mentions: RubyCord::Unset,
  attachments: RubyCord::Unset,
  components: RubyCord::Unset,
  supress: RubyCord::Unset
)
  Async do
    payload = {}
    payload[:content] = content if content != RubyCord::Unset
    tmp_embed =
      if embed != RubyCord::Unset
        [embed]
      elsif embeds != RubyCord::Unset
        embeds
      end
    payload[:embeds] = tmp_embed.map(&:to_hash) if tmp_embed
    payload[:allowed_mentions] = if allowed_mentions == RubyCord::Unset
      @client.allowed_mentions.to_hash
    else
      allowed_mentions.to_hash(@client.allowed_mentions)
    end
    payload[:components] = Component.to_payload(components) if components !=
      RubyCord::Unset
    payload[:flags] = (supress ? 1 << 2 : 0) if supress != RubyCord::Unset
    if attachments != RubyCord::Unset
      payload[:attachments] = attachments.map.with_index do |a, i|
        { id: i, filename: a.filename, description: a.description }
      end
    end
    @client
      .http
      .multipart_request(
        RubyCord::Internal::Route.new(
          "/channels/#{channel_id.wait}/messages/#{message_id}",
          "//channels/:channel_id/messages/:message_id",
          :patch
        ),
        payload,
        attachments == RubyCord::Unset ? [] : attachments
      )
      .wait
  end
end

#fetch_message(id) ⇒ Async::Task<RubyCord::Message>

Fetch a message from ID.

Parameters:

Returns:

Raises:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/rubycord/internal/messageable.rb', line 179

def fetch_message(id)
  Async do
    _resp, data =
      @client
        .http
        .request(
          RubyCord::Internal::Route.new(
            "/channels/#{channel_id.wait}/messages/#{id}",
            "//channels/:channel_id/messages/:message_id",
            :get
          )
        )
        .wait
    RubyCord::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<RubyCord::Message>>

Fetch a message history.

Parameters:

  • limit (Integer) (defaults to: 50)

    The number of messages to fetch.

  • before (RubyCord::Snowflake) (defaults to: nil)

    The ID of the message to fetch before.

  • after (RubyCord::Snowflake) (defaults to: nil)

    The ID of the message to fetch after.

  • around (RubyCord::Snowflake) (defaults to: nil)

    The ID of the message to fetch around.

Returns:



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/rubycord/internal/messageable.rb', line 207

def fetch_messages(limit = 50, before: nil, after: nil, around: nil)
  Async do
    params =
      {
        limit:,
        before: RubyCord::Utils.try(after, :id),
        after: RubyCord::Utils.try(around, :id),
        around: RubyCord::Utils.try(before, :id)
      }.filter { |_k, v| !v.nil? }.to_h
    _resp, messages =
      @client
        .http
        .request(
          RubyCord::Internal::Route.new(
            "/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}",
            "//channels/:channel_id/messages",
            :get
          )
        )
        .wait
    messages.map do |m|
      RubyCord::Message.new(@client, m.merge({ guild_id: @guild_id.to_s }))
    end
  end
end

#fetch_pinsAsync::Task<Array<RubyCord::Message>>

Fetch the pinned messages in the channel.

Returns:



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rubycord/internal/messageable.rb', line 239

def fetch_pins
  Async do
    _resp, data =
      @client
        .http
        .request(
          RubyCord::Internal::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.

Parameters:

  • message (RubyCord::Message)

    The message to pin.

  • reason (String) (defaults to: nil)

    The reason of pinning the message.

Returns:

  • (Async::Task<void>)

    The task.



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/rubycord/internal/messageable.rb', line 265

def pin_message(message, reason: nil)
  Async do
    @client
      .http
      .request(
        RubyCord::Internal::Route.new(
          "/channels/#{channel_id.wait}/pins/#{message.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<RubyCord::Message> Also known as: send_message

Post a message to the channel.

Parameters:

Returns:



20
21
22
23
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
66
67
68
69
70
71
72
73
74
# File 'lib/rubycord/internal/messageable.rb', line 20

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
    tmp_embed =
      if embed
        [embed]
      elsif embeds
        embeds
      end
    payload[:embeds] = tmp_embed.map(&:to_hash) if tmp_embed
    payload[:allowed_mentions] = (
      if allowed_mentions
        allowed_mentions.to_hash(@client.allowed_mentions)
      else
        @client.allowed_mentions.to_hash
      end
    )
    payload[
      :message_reference
    ] = reference.to_reference.to_hash if reference
    payload[:components] = Component.to_payload(components) if components
    attachments ||= attachment ? [attachment] : []

    payload[:attachments] = attachments.map.with_index do |a, i|
      { id: i, filename: a.filename, description: a.description }
    end

    _resp, data =
      @client
        .http
        .multipart_request(
          RubyCord::Internal::Route.new(
            "/channels/#{channel_id.wait}/messages",
            "//channels/:channel_id/messages",
            :post
          ),
          payload,
          attachments
        )
        .wait
    RubyCord::Message.new(@client, data.merge({ guild_id: @guild_id.to_s }))
  end
end

#typingObject

Trigger the typing indicator in the channel. If block is given, trigger typing indicator during executing block.

Examples:

channel.typing do
  channel.post("Waiting for 60 seconds...")
  sleep 60
  channel.post("Done!")
end


320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/rubycord/internal/messageable.rb', line 320

def typing
  if block_given?
    begin
      post_task =
        Async do
          loop do
            @client.http.request(
              RubyCord::Internal::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(
        RubyCord::Internal::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.

Parameters:

  • message (RubyCord::Message)

    The message to unpin.

  • reason (String) (defaults to: nil)

    The reason of unpinning the message.

Returns:

  • (Async::Task<void>)

    The task.



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/rubycord/internal/messageable.rb', line 291

def unpin_message(message, reason: nil)
  Async do
    @client
      .http
      .request(
        RubyCord::Internal::Route.new(
          "/channels/#{channel_id.wait}/pins/#{message.id}",
          "//channels/:channel_id/pins/:message_id",
          :delete
        ),
        {},
        audit_log_reason: reason
      )
      .wait
  end
end