Error when trying to add buttons to Discord JS interaction.reply

huangapple go评论66阅读模式
英文:

Error when trying to add buttons to Discord JS interaction.reply

问题

return interaction.reply({
    content: `运行频道命令时出现错误。请稍后再试。 (${error})`,
    ephemeral: true,
    components: [new ActionRowBuilder({
        components: [
            new ButtonBuilder({
                customId: '1',
                label: '是',
                style: ButtonStyle.Success
            })
        ]
    })]
});
英文:

I am currently having an issue with adding buttons to my Discord JS project, I am using Typescript and Discord JS v14 to make the bot.

My current Issue is with adding buttons, it gives me a long error that I am unable to find a solution on myself and I haven't found one online.

And yes everything has been included and everything is working on the bot, I am just receiving errors when trying to add buttons.

Here is the code snippet in question:

return interaction.reply({
    content: `There was an error while running the channel command. Please try again later. (${error})`,
    ephemeral: true,
    components: [new ActionRowBuilder({
        components: [
            new ButtonBuilder({
                customId: '1',
                label: 'yes',
                style: ButtonStyle.Success
            })
        ]
    })]
});

Here is the error I am getting:

[{
	"resource": "/c:/Users/Coby/Documents/GitHub/franksbot/src/commands/chanell.ts",
	"owner": "typescript",
	"code": "2769",
	"severity": 8,
	"message": "No overload matches this call.\n  Overload 1 of 2, '(options: InteractionReplyOptions & { fetchReply: true; }): Promise<Message<boolean>>', gave the following error.\n    Type 'ActionRowBuilder<AnyComponentBuilder>' is not assignable to type 'APIActionRowComponent<APIMessageActionRowComponent> | JSONEncodable<APIActionRowComponent<APIMessageActionRowComponent>> | ActionRowData<...>'.\n      Property 'type' is missing in type 'ActionRowBuilder<AnyComponentBuilder>' but required in type 'ActionRowData<MessageActionRowComponentData | MessageActionRowComponentBuilder>'.\n  Overload 2 of 2, '(options: string | InteractionReplyOptions | MessagePayload): Promise<InteractionResponse<boolean>>', gave the following error.\n    Type 'ActionRowBuilder<AnyComponentBuilder>' is not assignable to type 'APIActionRowComponent<APIMessageActionRowComponent> | JSONEncodable<APIActionRowComponent<APIMessageActionRowComponent>> | ActionRowData<...>'.",
	"source": "ts",
	"startLineNumber": 79,
	"startColumn": 26,
	"endLineNumber": 87,
	"endColumn": 15,
	"relatedInformation": [
		{
			"startLineNumber": 267,
			"startColumn": 3,
			"endLineNumber": 267,
			"endColumn": 7,
			"message": "'type' is declared here.",
			"resource": "/c:/Users/Coby/Documents/GitHub/franksbot/node_modules/discord.js/typings/index.d.ts"
		}
	]
}]

I have tried many different approaches to this such as storing them in a variable first, I've tried different formats such as using .setStyle etc.

I expected it to not return an error and allow me to add a button to my message reply on discord.

答案1

得分: 3

I found the solution for anyone else having this issue. In TypeScript, you need to add this to it as well <ButtonBuilder> to the ActionRowBuilder class.

So it would be

return interaction.reply({
    content: `在运行通道命令时出错了。请稍后再试。 (${error})`,
    ephemeral: true,
    components: [new ActionRowBuilder<ButtonBuilder>({
        components: [
            new ButtonBuilder({
                customId: '1',
                label: '是',
                style: ButtonStyle.Success
            })
        ]
    })]
});
英文:

Found the solution for anyone else having this issue. In TypeScript you need to add this to it aswell <ButtonBuilder> to the ActionRowBuilder class.

So it would be
return interaction.reply({
content:
There was an error while running the channel command. Please try again later. (${error}),
ephemeral: true,
components: [new ActionRowBuilder<ButtonBuilder>({
components: [
new ButtonBuilder({
customId: '1',
label: 'yes',
style: ButtonStyle.Success
})
]
})]
});

huangapple
  • 本文由 发表于 2023年5月22日 01:11:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定