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

#fetch_message(id) -> Discorb::Message

Fetch a message from ID.

Parameters:

Returns:

Raises:



77
78
79
80
81
82
# File 'lib/discorb/modules.rb', line 77

def fetch_message(id)
  Async do
    _resp, data = @client.http.get("#{base_url.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) -> Array<Discorb::Message>

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:



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/discorb/modules.rb', line 94

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("#{base_url.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) -> Discorb::Message

Post a message to the channel.

Parameters:

  • content (String) (defaults to: nil)

    The message content.

  • tts (Boolean) (defaults to: false)

    Whether the message is tts.

  • 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.

  • reference (Discorb::Message, Discorb::Message::Reference) (defaults to: nil)

    The message to reply to.

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

    The components to send.

  • file (Discorb::File) (defaults to: nil)

    The file to send.

  • files (Array<Discorb::File>) (defaults to: nil)

    The files to send.

Returns:



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
# File 'lib/discorb/modules.rb', line 23

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
    if components
      tmp_components = []
      tmp_row = []
      components.each do |c|
        case c
        when Array
          tmp_components << tmp_row
          tmp_row = []
          tmp_components << c
        when SelectMenu
          tmp_components << tmp_row
          tmp_row = []
          tmp_components << [c]
        else
          tmp_row << c
        end
      end
      tmp_components << tmp_row
      payload[:components] = tmp_components.filter { |c| c.length.positive? }.map { |c| { type: 1, components: c.map(&:to_hash) } }
    end
    files = [file] if file
    if files
      headers, payload = HTTP.multipart(payload, files)
    else
      headers = {}
    end
    _resp, data = @client.http.post("#{base_url.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:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/discorb/modules.rb', line 120

def typing
  Async do |task|
    if block_given?
      begin
        post_task = task.async do
          @client.http.post("/channels/#{@id}/typing", {})
          sleep(5)
        end
        yield
      ensure
        post_task.stop
      end
    else
      @client.http.post("/channels/#{@id}/typing", {})
    end
  end
end