Module: Discorb::Interaction::SourceResponse

Included in:
MessageComponentInteraction
Defined in:
lib/discorb/interaction.rb

Overview

A module for response with source.

Instance Method Summary collapse

Instance Method Details

#defer_source(hide: false) -> Object

Response with DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE(5).

Parameters:

  • hide (Boolean) (defaults to: false)

    Whether to hide the response (ephemeral).



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/discorb/interaction.rb', line 100

def defer_source(hide: false)
  Async do
    @client.http.post("/interactions/#{@id}/#{@token}/callback", {
      type: 5,
      data: {
        flags: (hide ? 1 << 6 : 0),
      },
    }).wait
    @defered = true
  end
end

#post(content, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, hide: false) -> Object

Response with CHANNEL_MESSAGE_WITH_SOURCE(4).

Parameters:

  • content (String)

    The content of the response.

  • tts (Boolean) (defaults to: false)

    Whether to send the message as text-to-speech.

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

    The embed to send.

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

    The embeds to send. (max: 10)

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

    The allowed mentions to send.

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

    The components to send.

  • hide (Boolean) (defaults to: false)

    Whether to hide the response (ephemeral).



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/discorb/interaction.rb', line 123

def post(content, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, hide: false)
  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
  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
  payload[:flags] = (hide ? 1 << 6 : 0)
  if @responded
    @client.http.post("/webhooks/#{@id}/#{@token}", { type: 4, data: payload }).wait
  elsif @defered
    @client.http.post("/interactions/#{@id}/#{@token}/@original/edit", { type: 4, data: payload }).wait
  else
    @client.http.post("/interactions/#{@id}/#{@token}/callback", { type: 4, data: payload }).wait
  end
  @responded = true
end