Skip to content

Commit

Permalink
bugfix: implement more fixes for the interaction class
Browse files Browse the repository at this point in the history
  • Loading branch information
4x8Matrix committed Oct 22, 2024
1 parent c14f6e8 commit e186248
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 8 deletions.
41 changes: 36 additions & 5 deletions development.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local botObject = require("@core/bot")

local messageBuilder = require("@builders/message/message")
local actionRowBuilder = require("@builders/message/components/actionRow")
local buttonBuilder = require("@builders/message/components/button")
local intentsBuilder = require("@builders/intents")
local interactionBuilder = require("@builders/interaction/interaction")

Expand Down Expand Up @@ -37,14 +39,43 @@ discordBot.onAllShardsReady:listen(function()
interaction:pongAsync()
end)

discordBot.onComponentInteraction:listen(function(interaction)
-- warn(interaction)

assert(interaction.data, `e`)

interaction:messageAsync({
content = `You pressed the {interaction.data.customId} button!`,
})
end)

discordBot.onCommandInteraction:listen(function(interaction)
local message = interaction
:messageAsync({
local button = buttonBuilder
.new({
customId = "custom-id",
label = "label",
style = "Blurple",
})
:build()

local buttonList = actionRowBuilder
.new({
components = {
button,
},
})
:build()

local message = messageBuilder
.new({
content = "abc",
}, true)
:expect(`Something bad happened..`)
components = {
buttonList,
},
})
:build()

print(message)
local response = interaction:messageAsync(message):expect(`Something bad happened..`)
end)
end)

Expand Down
8 changes: 7 additions & 1 deletion packages/api-types/src/apiTypes.luau
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,13 @@ export type InteractionDataObject = {
resolved: ResolvedDataStructure?, -- converted users + roles + channels + attachments
options: { ApplicationCommandInteractionDataOptionObject }?, -- the params + values from the user
guild_id: Snowflake?, -- the id of the guild the command is registered to
target_id: Snowflake?, -- id of the user or message targeted by a user or message command
target_id: Snowflake?, -- id of the user or message targeted by a user or message command,

custom_id: string?, -- custom_id of the component
component_type: number?, -- type of the component
values: { SelectOptionObject }?, -- Values the user selected in a select menu component

components: { ComponentObjects }?, -- Resolved entities from selected options
}

-- https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-structure
Expand Down
22 changes: 22 additions & 0 deletions packages/api-types/src/interaction.luau
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,34 @@ local ApplicationCommandType = table.freeze(reflect({
PrimaryEntryPoiant = 4,
}))

local ComponentTypes = table.freeze(reflect({
ActionRow = 1,
Button = 2,
StringSelect = 3,
TextInput = 4,
UserSelect = 5,
RoleSelect = 6,
MentionableSelect = 7,
ChannelSelect = 8,
}))

export type InteractionType =
"Ping"
| "ApplicationCommand"
| "MessageComponent"
| "ApplicationCommandAutocomplete"
| "ModalSubmit"

export type ComponentTypes =
"ActionRow"
| "Button"
| "StringSelect"
| "TextInput"
| "UserSelect"
| "RoleSelect"
| "MentionableSelect"
| "ChannelSelect"

export type ApplicationCommandOptionType =
"SubCommand"
| "SubCommandGroup"
Expand All @@ -64,4 +85,5 @@ return {
ApplicationCommandOptionType = ApplicationCommandOptionType,
InteractionContextType = InteractionContextType,
ApplicationCommandType = ApplicationCommandType,
ComponentTypes = ComponentTypes,
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function Interaction.Interface.inheritProperties(class: any, interactionData: ap
class.id = interactionData.id
class.applicationId = interactionData.application_id
class.data = interactionData.data and data.new(class.state, interactionData.data)
class.guild = interactionData.guild and unavailableGuild.new(class.state, interactionData.guild.id)
class.guild = interactionData.guild and unavailableGuild.new(class.state, interactionData.guild.id :: string)
class.guildId = interactionData.guild_id
-- class.channel = interactionData.channel and message.new(class.state, interactionData.channel)
class.channelId = interactionData.channel_id
Expand Down
34 changes: 34 additions & 0 deletions packages/classes/src/interaction/data.luau
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ local interactionTypes = require("@api-types/interaction")
local resolved = require("@classes/resolved")
local state = require("@classes/state")

local button = require("@classes/message/components/button")
local textInput = require("@classes/message/components/textInput")
local selectMenu = require("@classes/message/components/selectMenu/selectMenu")
local actionRow = require("@classes/message/components/actionRow")

local dataOption = require("@classes/interaction/dataOption")

local Data = {}
Expand All @@ -19,11 +24,34 @@ Data.Prototype = {}

function Data.Prototype.sync(self: Data, dataObject: apiTypes.InteractionDataObject)
local optionArray = {}
local componentsArray: { button.Button | textInput.TextInput | selectMenu.SelectMenu | actionRow.ActionRow } = {}

for _, option in next, dataObject.options or {} do
table.insert(optionArray, dataOption.new(option))
end

if dataObject.components then
for _, component in dataObject.components do
if component.type == 1 then
local actionRowComponent = component :: apiTypes.ActionRowComponentObject

table.insert(componentsArray, actionRow.new(actionRowComponent))
elseif component.type == 2 then
local buttonComponent = component :: apiTypes.ButtonComponentObject

table.insert(componentsArray, button.new(buttonComponent))
elseif component.type == 4 then
local textInputComponent = component :: apiTypes.TextInputComponentObject

table.insert(componentsArray, textInput.new(textInputComponent))
else
local selectMenuComponent = component :: apiTypes.SelectMenuComponentObject

table.insert(componentsArray, selectMenu.new(selectMenuComponent))
end
end
end

self.type = interactionTypes.InteractionType[dataObject.type]

self.id = dataObject.id
Expand All @@ -32,6 +60,9 @@ function Data.Prototype.sync(self: Data, dataObject: apiTypes.InteractionDataObj
self.resolved = dataObject.resolved and resolved.new(self.state, dataObject.resolved)
self.guildId = dataObject.guild_id
self.targetId = dataObject.target_id
self.customId = dataObject.custom_id
self.componentType = interactionTypes.ComponentTypes[dataObject.component_type]
self.components = componentsArray
end

function Data.Interface.new(state: state.State, dataObject: apiTypes.InteractionDataObject): Data
Expand All @@ -58,6 +89,9 @@ export type Data = typeof(Data.Prototype) & {
options: { dataOption.DataOption<unknown> },
guildId: apiTypes.Snowflake?,
targetId: apiTypes.Snowflake?,
customId: string?,
componentType: interactionTypes.ComponentTypes?,
components: { button.Button | textInput.TextInput | selectMenu.SelectMenu | actionRow.ActionRow }?,
}

return Data.Interface
Loading

0 comments on commit e186248

Please sign in to comment.