我启动了基于按钮的RPG机器人,目标是无论我按下哪个按钮来滚动d20,都能获得菜单。稍后我将为每个按钮处理功能。首先按钮工作得很好,然后我开始出错。我使用以下代码生成操作行:
//Remember to run " node deploy-commands.js " to register your commands!
const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageActionRow, MessageButton, MessageEmbed } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('menu')
.setDescription('Brings up the Main Menu'),
async execute(interaction, client) {
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('dice-button')
.setLabel('Dice bag')
.setStyle("DANGER"),
new MessageButton()
.setCustomId('spells-button')
.setLabel('Spells')
.setStyle("PRIMARY"),
new MessageButton()
.setCustomId('inventory-button')
.setLabel('Inventory')
.setStyle("SUCCESS"),
);
await interaction.reply({content: 'Main menu', ephemeral: true, components: [row]})
//button handler
client.on('interactionCreate', interaction => {
if (!interaction.isButton()) return;
console.log(interaction);
interaction.reply(`${interaction.user} rolled ${Math.round(Math.random() * 20)}`);
});
},
};
几次按压后,我得到了错误:
TypeError:无法读取未定义的属性(读取"on")在Object.execute(C:\Users\levan\Desktop\Discordbot\Test bots\5 Buttons\commands\Menu.js:32)在processTicksAndRejections(节点:internal/process/task_queues:96:5)异步客户端。(C:\Users\levan\Desktop\Discordbot\Test bots\5 Buttons\index.js:40:3)C: \Users\levan\Desktop\Discordbot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:89如果(this.deferred||this.replied)抛出新错误('INTERRACTION_ARREADY_replied');^
错误[INTERACTION_ALREADY_REPLIED]:对此交互的回复已发送或推迟。在CommandInteraction.reply(C:\Users\levan\Desktop\Discordbot\node_modules\discord.js\src\structures\interfaces\InteractionResponse.js:89:46)在客户处。(C:\Users\levan\Desktop\Discordbot\Test bots\5 Buttons\index.js:43:22)在processTicksAndRejections(节点:internal/process/task_queues:96:5){[Symbol(code)]:"INTERACTION_ALREADY_REPLIED">
我曾尝试删除和重做代码,但这是我最接近上面代码的一次。
我还看到了以下错误
C:\Users\levan\Desktop\Discordbot\Test bots\5 Buttons\index.js:10等待交互。回复,组件:[行]})
SyntaxError:await仅在异步函数和顶层中有效模块主体在Object.compileFunction(节点:vm:352:18)在wrapSafe(节点:internal/modules/cjs/loader:1031:15)在模块中_compile(节点:internal/modules/cjs/loader:1065:27)位于Object.Module_extensions..js(节点:internal/modules/cjs/loader:1153:10)在Module.load(节点:internal/modules/cjs/loader:981:32)位于Function.Module_load(节点:internal/modules/cjs/loader:822:12)在Function.executeUserEntryPoint[作为runMain](节点:internal/modules/run_main:79:12)在节点:internal/main/run_main_module:17:47
在尝试修改上面的代码后,但我不确定为什么要移动:
await interaction.reply({content: 'Main menu', ephemeral: true, components: [row]})
//button handler
client.on('interactionCreate', interaction => {
if (!interaction.isButton()) return;
console.log(interaction);
interaction.reply(`${interaction.user} rolled ${Math.round(Math.random() * 20)}`);
});
在我的文件中超过一两个,让我更容易在脑海中把它拼凑起来,我猜这就是导致它死亡的原因,但即使我试图把它移回来,它也会出现同样的错误。
我使用的主要资源是https://discordjs.guide/interactions/buttons.html#responding-到按钮
。(C:\Users\levan\Desktop\Discordbot\Test bots\5 Buttons\index.js:40:3)
我删除了一个不应该放在索引中的命令。
我试图删除menu.js中的等待,结果得到:
类型错误:交互。回复不是函数在Object.execute(C:\Users\levan\Desktop\Discordbot\Test bots\5 Buttons\commands\Menu.js:32:43)在客户处。(C:\Users\levan\Desktop\Discordbot\Test bots\5 Buttons\index.js:41:17)
在Client.emit(节点:事件:402:35)位于InteractionCreateAction.handle(C:\Users\levan\Desktop\Discordbot\node_modules\discord.js\src\client\actions\InteractionCreate.js:66:12)在Object.module.exports[作为INTERACTION_CREATE](C:\Users\levan\Desktop\Discordbot\node_modules\discord.js\src\client\websocket\handlers \INTERACTION_CREATE.js:4:36)位于WebSocketManager.handlePack(C:\Users\levan\Desktop\Discordbot\node_modules\discord.js\src\client\websocket\WebocketManager.js:350:31)位于WebSocketHard.onPacket(C:\Users\levan\Desktop\Discordbot\node_modules\discord.js\src\client\websocket\WebocketHard.js:443:22)位于WebSocketHard.oMessage(C:\Users\levan\Desktop\Discordbot\node_modules\discord.js\src\client\websocket\WebocketHard.js:30:10)位于WebSocket.onMessage(C:\Users\levan\Desktop\Discordbot\node_modules\ws\lib\event-target.js:199:18)在WebSocket.emit(节点:事件:390:28)
这允许菜单弹出,但按钮仍在抛出错误:
TypeError:无法读取未定义的属性(读取"on")在Object.execute(C:\Users\levan\Desktop\Discordbot\Test bots\5 Buttons\commands\Menu.js:32)在processTicksAndRejections(节点:internal/process/task_queues:96:5)异步客户端。(C:\Users\levan\Desktop\Discordbot\Test bots\5 Buttons\index.js:41:3)C: \Users\levan\Desktop\Discordbot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:89如果(this.deferred||this.replied)抛出新错误('INTERRACTION_ARREADY_replied');^
错误[INTERACTION_ALREADY_REPLIED]:对此交互的回复已发送或推迟。在CommandInteraction.reply(C:\Users\levan\Desktop\Discordbot\node_modules\discord.js\src\structures\interfaces\InteractionResponse.js:89:46)在客户处。(C:\Users\levan\Desktop\Discordbot\Test bots\5 Buttons\index.js:44:22)在processTicksAndRejections(节点:internal/process/task_queues:96:5){[Symbol(code)]:"INTERACTION_ALREADY_REPLIED">
尝试节点之后我得到了错误:
C:\Users\levan\Desktop\Discordbot\node_modules\discord.js\src\structures\BaseCommandInteraction.js:193applyToClass(BaseCommandInteraction,['deferUpdate','update']);^
TypeError:InteractionResponses.applyToClass不是函数在对象处。(C:\Users\levan\Desktop\Discordbot\node_modules\discord.js\src\structures\BaseCommandInteraction.js:1193:22)在模块中_compile(节点:internal/modules/cjs/loader:1101:14)位于Object.Module_extensions..js(节点:internal/modules/cjs/loader:1153:10)在Module.load(节点:internal/modules/cjs/loader:981:32)位于Function.Module_load(节点:internal/modules/cjs/loader:822:12)在Module.require(节点:internal/modules/cjs/loader:1005:19)at require(节点:internal/modules/cjs/helpers:102:18)在对象处。(C:\Users\levan\Desktop\Discordbot\node_modules\discord.js\src\index.js:74:27)在模块中_compile(节点:internal/modules/cjs/loader:1101:14)在Object.Module_extensions..js(节点:internal/modules/cjs/loader:1153:10)
在查看了错误中引用的代码后,我得出了以下结论:我安装的API之一一定不能使用我的代码。
我撕掉了所有我没有亲自编写的代码,并一次重新加载API的代码。同样的错误。
原来以下代码不应该在命令文件中。。。
//button handler
client.on('interactionCreate', interaction => {
if (!interaction.isButton()) return;
console.log(interaction);
interaction.reply(`You rolled ${Math.round(Math.random() * 20)}`);
});
将其移动到索引和离开时的问题