如何回复 discord.js 中按下的按钮
How do I reply button pressed in discord.js
我正在使用按钮制作一个不和谐的机器人,一切运行良好,没有错误,但是当我点击按钮时我卡住了,没有任何反应。由于 Discord.js 在最近的更新中进行了更改,我无法找到合适的解决方案。下面是代码-
const { MessageActionRow, MessageButton, MessageEmbed } = require("discord.js")
module.exports ={
name:'ping',
execute(message, arg){
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('id1')
.setLabel('LoL')
.setStyle('DANGER')
)
const ping = new MessageEmbed()
.setDescription('First Button event!')
.setTimestamp()
.setImage('https://images2.alphacoders.com/927/thumbbig-927608.webp')
.setColor('RANDOM')
const lol = new MessageActionRow()
.addComponents(
new MessageButton()
.setStyle('LINK')
.setLabel('Link to Image!')
.setURL('https://images2.alphacoders.com/927/thumbbig-927608.webp')
)
message.channel.send({content :'Hello! This is my first Button event by using command handeler!! ',embeds:[ping], components :[row , lol]})
}
}
下面是主文件的代码
client.on('message', message => {
if(!message.content.startsWith(PREFIX)|| message.author.bot) return
const arg = message.content.slice(PREFIX.length).split(/ +/)
const command = arg.shift().toLowerCase()
if (command === 'ping'){
client.commands.get('ping').execute(message, arg)
}
})
client.on('clickButton', async (button) =>{
} )
没有 clickButton
事件。处理任何交互(包括按钮点击)的事件是 interactionCreate
。从那里,你会检查交互是一个按钮(因为它也可以是一个斜杠命令、select 菜单、上下文菜单、模式等),然后用它做你想做的事。或许在使用之前还要检查按钮的 ID。
client.on('interactionCreate', interaction => {
if (!interaction.isButton()) return;
if (interaction.customId == "id1") {
// Do what you want with button 'id1'.
}
});
如果您想了解可以在 interaction
上使用的方法和属性,按钮交互的文档是 here。
我正在使用按钮制作一个不和谐的机器人,一切运行良好,没有错误,但是当我点击按钮时我卡住了,没有任何反应。由于 Discord.js 在最近的更新中进行了更改,我无法找到合适的解决方案。下面是代码-
const { MessageActionRow, MessageButton, MessageEmbed } = require("discord.js")
module.exports ={
name:'ping',
execute(message, arg){
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('id1')
.setLabel('LoL')
.setStyle('DANGER')
)
const ping = new MessageEmbed()
.setDescription('First Button event!')
.setTimestamp()
.setImage('https://images2.alphacoders.com/927/thumbbig-927608.webp')
.setColor('RANDOM')
const lol = new MessageActionRow()
.addComponents(
new MessageButton()
.setStyle('LINK')
.setLabel('Link to Image!')
.setURL('https://images2.alphacoders.com/927/thumbbig-927608.webp')
)
message.channel.send({content :'Hello! This is my first Button event by using command handeler!! ',embeds:[ping], components :[row , lol]})
}
}
下面是主文件的代码
client.on('message', message => {
if(!message.content.startsWith(PREFIX)|| message.author.bot) return
const arg = message.content.slice(PREFIX.length).split(/ +/)
const command = arg.shift().toLowerCase()
if (command === 'ping'){
client.commands.get('ping').execute(message, arg)
}
})
client.on('clickButton', async (button) =>{
} )
没有 clickButton
事件。处理任何交互(包括按钮点击)的事件是 interactionCreate
。从那里,你会检查交互是一个按钮(因为它也可以是一个斜杠命令、select 菜单、上下文菜单、模式等),然后用它做你想做的事。或许在使用之前还要检查按钮的 ID。
client.on('interactionCreate', interaction => {
if (!interaction.isButton()) return;
if (interaction.customId == "id1") {
// Do what you want with button 'id1'.
}
});
如果您想了解可以在 interaction
上使用的方法和属性,按钮交互的文档是 here。