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

Instance Method Details

#delete_message!(message_id, reason: nil) -> Object Also known as: destroy_message!

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Delete a message.

Parameters:

  • message_id (#to_s)

    The message id.

  • reason (String) (defaults to: nil)

    The reason for deleting the message.

Raises:



95
96
97
98
99
# File 'lib/discorb/modules.rb', line 95

def delete_message!(message_id, reason: nil)
  Async do
    @client.http.delete("/channels/#{channel_id.wait}/messages/#{message_id}", reason: reason).wait
  end
end

#edit_message(message_id, content = nil, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, supress: nil) -> Object

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Edit a message.

Parameters:

  • message_id (#to_s)

    The message id.

  • content (String) (defaults to: nil)

    The message content.

  • embed (Discorb::Embed) (defaults to: nil)

    The embed to send.

  • embeds (Array<Discorb::Embed>) (defaults to: nil)

    The embeds to send.

  • allowed_mentions (Discorb::AllowedMentions) (defaults to: nil)

    The allowed mentions.

  • components (Array<Discorb::Component>, Array<Array<Discorb::Component>>) (defaults to: nil)

    The components to send.

  • supress (Boolean) (defaults to: nil)

    Whether to supress embeds.

Raises:



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 edit_message(message_id, content = nil, embed: nil, embeds: nil, allowed_mentions: nil,
                                            components: nil, supress: nil)
  Async do
    payload = {}
    payload[:content] = content if content
    tmp_embed = if embed
        [embed]
      elsif embeds
        embeds
      end
    payload[:embeds] = tmp_embed.map(&:to_hash) if tmp_embed
    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/#{message_id}", payload).wait
  end
end

#fetch_message(id) -> Async::Task<Discorb::Message>

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Fetch a message from ID.

Parameters:

Returns:

Raises:



113
114
115
116
117
118
# File 'lib/discorb/modules.rb', line 113

def fetch_message(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>>

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Fetch a message history.

Parameters:

  • limit (Integer) (defaults to: 50)

    The number of messages to fetch.

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

    The ID of the message to fetch before.

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

    The ID of the message to fetch after.

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

    The ID of the message to fetch around.

Returns:

Raises:



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/discorb/modules.rb', line 132

def fetch_messages(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, messages = @client.http.get("/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}").wait
    messages.map { |m| Message.new(@client, m.merge({ guild_id: @guild_id.to_s })) }
  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

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Post a message to the channel.

Parameters:

Returns:

Raises:



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
    tmp_embed = if embed
        [embed]
      elsif embeds
        embeds
      end
    payload[:embeds] = tmp_embed.map(&:to_hash) if tmp_embed
    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

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

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

Raises:



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/discorb/modules.rb', line 158

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