Module: RubyCord::Interaction::SourceResponder

Included in:
Command, Component, Modal
Defined in:
lib/rubycord/interaction/response.rb

Instance Method Summary collapse

Instance Method Details

#defer_source(ephemeral: false) ⇒ Async::Task<void>

Response with DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE(5).

Parameters:

  • ephemeral (Boolean) (defaults to: false)

    Whether to make the response ephemeral.

Returns:

  • (Async::Task<void>)

    The task.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubycord/interaction/response.rb', line 19

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

#post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, attachment: nil, attachments: nil, components: nil, ephemeral: false) ⇒ RubyCord::Interaction::SourceResponder::CallbackMessage, RubyCord::Webhook::Message

Response with CHANNEL_MESSAGE_WITH_SOURCE(4).

Parameters:

  • content (String) (defaults to: nil)

    The content of the response.

  • tts (Boolean) (defaults to: false)

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

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

    The embed to send.

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

    The embeds to send. (max: 10)

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

    The allowed mentions to send.

  • attachment (RubyCord::Attachment) (defaults to: nil)

    The attachment to send.

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

    The attachments to send. (max: 10)

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

    The components to send.

  • ephemeral (Boolean) (defaults to: false)

    Whether to make the response ephemeral.

Returns:

  • (RubyCord::Interaction::SourceResponder::CallbackMessage, RubyCord::Webhook::Message)

    The callback message.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
128
129
130
131
132
133
134
135
# File 'lib/rubycord/interaction/response.rb', line 54

def post(
  content = nil,
  tts: false,
  embed: nil,
  embeds: nil,
  allowed_mentions: nil,
  attachment: nil,
  attachments: nil,
  components: nil,
  ephemeral: false
)
  Async do
    payload = {}
    payload[:content] = content if content
    payload[:tts] = tts
    payload[:embeds] = (embeds || [embed])
      .map { |e| e&.to_hash }
      .filter { _1 }
    payload[:allowed_mentions] = allowed_mentions&.to_hash(
      @client.allowed_mentions
    ) || @client.allowed_mentions.to_hash
    payload[:components] = Component.to_payload(components) if components
    payload[:flags] = (ephemeral ? 1 << 6 : 0)
    attachments ||= attachment ? [attachment] : []

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

    ret =
      if @responded
        _resp, data =
          @client
            .http
            .multipart_request(
              RubyCord::Internal::Route.new(
                "/webhooks/#{@application_id}/#{@token}",
                "//webhooks/:webhook_id/:token",
                :post
              ),
              payload,
              attachments
            )
            .wait
        webhook =
          Guild::Webhook::URLWebhook.new(
            "/webhooks/#{@application_id}/#{@token}"
          )
        Guild::Webhook::Message.new(webhook, data, @client)
      elsif @defered
        @client
          .http
          .multipart_request(
            RubyCord::Internal::Route.new(
              "/webhooks/#{@application_id}/#{@token}/messages/@original",
              "//webhooks/:webhook_id/:token/messages/@original",
              :patch
            ),
            payload,
            attachments
          )
          .wait
        CallbackMessage.new(@client, payload, @application_id, @token)
      else
        @client
          .http
          .multipart_request(
            RubyCord::Internal::Route.new(
              "/interactions/#{@id}/#{@token}/callback",
              "//interactions/:interaction_id/:token/callback",
              :post
            ),
            { type: 4, data: payload },
            attachments
          )
          .wait
        CallbackMessage.new(@client, payload, @application_id, @token)
      end
    @responded = true
    ret
  end
end