Skip to content

Commit

Permalink
feat(Interactions): Add support for button interactions (#62)
Browse files Browse the repository at this point in the history
Exposes the "data" object which provides the custom_id of the clicked
button. Adds a convenience method for retrieving the selected button by
its custom_id, useful for obtaining the label of the clicked button.
Adds new buttony TYPES.
  • Loading branch information
mattantonelli authored Jun 12, 2021
1 parent 78109dc commit 8a61dc7
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/discordrb/data/interaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ class Interaction
# @see https://discord.com/developers/docs/interactions/slash-commands#interaction-interactiontype
TYPES = {
ping: 1,
command: 2
command: 2,
button: 3
}.freeze

# Component types.
# @see https://discord.com/developers/docs/interactions/message-components#component-types
COMPONENT_TYPES = {
action_row: 1,
button: 2
}.freeze

# Interaction response types.
Expand Down Expand Up @@ -44,13 +52,17 @@ class Interaction
# @see TYPES
attr_reader :type

# @return [String] The interaction data.
attr_reader :data

# @!visibility private
def initialize(data, bot)
@bot = bot

@id = data['id'].to_i
@application_id = data['application_id'].to_i
@type = data['type']
@message = data['message']
@data = data['data']
@server_id = data['guild_id']&.to_i
@channel_id = data['channel_id']&.to_i
Expand Down Expand Up @@ -175,6 +187,17 @@ def server
def channel
@bot.channel(@channel_id)
end

# @return [Hash, nil] Returns the button that triggered this interaction if applicable, otherwise nil
def button
return unless @type == TYPES[:button]

@message['components'].each do |row|
row['components'].each do |component|
return component if component['type'] == COMPONENT_TYPES[:button] && component['custom_id'] == @data['custom_id']
end
end
end
end

# An ApplicationCommand for slash commands.
Expand Down

0 comments on commit 8a61dc7

Please sign in to comment.