Class: Discorb::Interaction

Inherits:
DiscordModel show all
Defined in:
lib/discorb/interaction/root.rb,
lib/discorb/interaction/response.rb

Overview

Represents an interaction of Discord.

Defined Under Namespace

Modules: ModalResponder, SourceResponder, UpdateResponder Classes: CallbackMessage

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DiscordModel

#==, #eql?

Instance Attribute Details

#app_permissions -> Discorb::Permission (readonly)

Returns The permissions of the bot.

Returns:



29
30
31
# File 'lib/discorb/interaction/root.rb', line 29

def app_permissions
  @app_permissions
end

#application_id -> Discorb::Snowflake (readonly)

Returns The ID of the application that created the interaction.

Returns:



11
12
13
# File 'lib/discorb/interaction/root.rb', line 11

def application_id
  @application_id
end

#guild_locale -> Symbol (readonly)

Note:

This modifies the language code, - will be replaced with _.

Returns The locale of the guild that created the interaction.

Returns:

  • (Symbol)

    The locale of the guild that created the interaction.



27
28
29
# File 'lib/discorb/interaction/root.rb', line 27

def guild_locale
  @guild_locale
end

#id -> Discorb::Snowflake (readonly)

Returns The ID of the interaction.

Returns:



9
10
11
# File 'lib/discorb/interaction/root.rb', line 9

def id
  @id
end

#locale -> Symbol (readonly)

Note:

This modifies the language code, - will be replaced with _.

Returns The locale of the user that created the interaction.

Returns:

  • (Symbol)

    The locale of the user that created the interaction.



24
25
26
# File 'lib/discorb/interaction/root.rb', line 24

def locale
  @locale
end

#token -> String (readonly)

Returns The token for the interaction.

Returns:

  • (String)

    The token for the interaction.



21
22
23
# File 'lib/discorb/interaction/root.rb', line 21

def token
  @token
end

#type -> Symbol (readonly)

Returns The type of interaction.

Returns:

  • (Symbol)

    The type of interaction.



13
14
15
# File 'lib/discorb/interaction/root.rb', line 13

def type
  @type
end

#user -> Discorb::User, Discorb::Member (readonly) Also known as: member

Returns The user or member that created the interaction.

Returns:



15
16
17
# File 'lib/discorb/interaction/root.rb', line 15

def user
  @user
end

#version -> Integer (readonly)

Note:

This is always 1 for now.

Returns The type of interaction.

Returns:

  • (Integer)

    The type of interaction.



19
20
21
# File 'lib/discorb/interaction/root.rb', line 19

def version
  @version
end

Instance Method Details

#channel -> Object



78
79
80
# File 'lib/discorb/interaction/root.rb', line 78

def channel
  @client.channels[@channel_id]
end

#delete_original_message -> Async::Task<void>

Delete the original response message. This method is low-level.

Returns:

  • (Async::Task<void>)

    The task.



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/discorb/interaction/root.rb', line 210

def delete_original_message
  Async do
    @client.http.request(
      Route.new(
        "/webhooks/#{@application_id}/#{@token}/messages/@original",
        "//webhooks/:webhook_id/:token/messages/@original",
        :delete
      )
    ).wait
  end
end

#edit_original_message(content = nil, embed: nil, embeds: nil, attachment: nil, attachments: nil, components: nil) -> Async::Task<void>

Edit the original response message. This method is low-level.

Parameters:

Returns:

  • (Async::Task<void>)

    The task.

See Also:



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/discorb/interaction/root.rb', line 166

def edit_original_message(
  content = nil,
  embed: nil,
  embeds: nil,
  attachment: nil,
  attachments: nil,
  components: nil
)
  Async do
    payload = {}
    payload[:content] = content if content
    payload[:embeds] = (embeds || [embed]).map { |e| e&.to_hash }.filter { _1 }.then { _1.empty? ? nil : _1 }
    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
    payload.compact!

    @client.http.multipart_request(
      Route.new(
        "/webhooks/#{@application_id}/#{@token}/messages/@original",
        "//webhooks/:webhook_id/:token/messages/@original",
        :patch
      ),
      payload,
      attachments
    ).wait
  end
end

#guild -> Object



74
75
76
# File 'lib/discorb/interaction/root.rb', line 74

def guild
  @client.guilds[@guild_id]
end

#inspect -> Object



82
83
84
# File 'lib/discorb/interaction/root.rb', line 82

def inspect
  "#<#{self.class} id=#{@id}>"
end

#post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, attachment: nil, attachments: nil, components: nil, ephemeral: false) -> Discorb::Webhook::Message

Send followup message.

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

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

    The attachment to send.

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

    The attachments to send. (max: 10)

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

    The components to send.

  • ephemeral (Boolean) (defaults to: false)

    Whether to make the response ephemeral.

Returns:



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
141
142
143
144
145
146
# File 'lib/discorb/interaction/root.rb', line 103

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

    _resp, data = @client.http.multipart_request(
      Route.new(
        "/webhooks/#{@application_id}/#{@token}",
        "//webhooks/:webhook_id/:token",
        :post
      ),
      payload,
      attachments
    ).wait
    webhook = Webhook::URLWebhook.new("/webhooks/#{@application_id}/#{@token}")
    Webhook::Message.new(webhook, data, @client)
    ret
  end
end