我目前是Discord.JS的新手,我想知道如何才能通过机器人获得这个"ECHO"命令的选项来回复

我当前的所有代码:

package.json

{
  "name": "bot",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

index.js

const fs = require('node:fs');
const path = require('node:path');
const {Client,GatewayIntentBits, Collection} = require('discord.js');
const {token} = require('./config.json');
const { execute } = require('./commands/ping');
const ping = require('./commands/ping');

const client = new Client({intents: [GatewayIntentBits.Guilds]});

client.commands = new Collection();
const commandPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandPath).filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const filePath = path.join(commandPath, file);
    const command = require(filePath);

    client.commands.set(command.data.name, command);
}

client.once('ready',()=> {
    console.log("ready");
});

client.on('interactionCreate', async interaction => {
    if(!interaction.isChatInputCommand()) return;
    
    const command = client.commands.get(interaction.commandName)

    if(!command) return;
    try {
        await command.execute(interaction);
    } catch(error) {
        console.error(error);
        await interaction.reply({content: "there was an error while executing this command", ephemeral: true});
    }
});

client.login(token);

echo.js

const {SlashCommandBuilder} = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('echo')
        .setDescription('returns input')
        .addStringOption(option =>
            option.setName('input')
                .setDescription("the option to echo back")
                .setRequired(true)),
            
    async execute(interaction) {
        await interaction.reply();
    }
}

ping.js

const {SlashCommandBuilder} = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('replies with pong'),
    async execute(interaction) {
        await interaction.reply("pong!")
    }
}

deploy-commands.js

const {REST} = require("@discordjs/rest");
const {Routes} = require("discord.js");
const {clientId, guildId, token} = require("./config.json");
const path = require("node:path");
const fs = require("node:fs");

const commands = [];
const commandPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandPath).filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const filePath = path.join(commandPath, file);
    const command = require(filePath);
    commands.push(command.data.toJSON());
}

const rest = new REST({version: 10}).setToken(token);

rest.put(Routes.applicationGuildCommands(clientId, guildId), {body:commands})
    .then(() => console.log("registered commands"))
    .catch(console.error);

config.json

{
    "token": "hidden",
    "clientId": "1009394510117212162",
    "guildId": "1009401102929768479"
}

error log

C:\Users\Perry\Desktop\bot> node .就绪类型错误:无法读取 未定义的属性(读数为‘临时性’) 在ChatInputCommandInteraction.Reply(C:\Users\perry\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:102:30) 在对象.执行(C:\Users\perry\Desktop\bot\commands\echo.js:14:27) 在客户处.(C:\Users\Perry\Desktop\bot\index.js:32:23) 在Client.emit( node :事件:527:28) 在InteractionCreateAction.Handle(C:\Users\perry\node_modules\discord.js\src\client\actions\InteractionCreate.js:81:12) 在对象模块.exports[as Interaction_Create](C:\Users\perry\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36) 在WebSocketManager.handlePacket(C:\Users\perry\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31) 在WebSocketShard.onPacket(C:\Users\perry\node_modules\discord.js\src\client\websocket\WebSocketShard.js:481:22) OnMessage(C:\Users\perry\node_modules\discord.js\src\client\websocket\WebSocketShard.js:321:10) OnMessage(C:\Users\perry\node_modules\ws\lib\event-target.js:199:18)

推荐答案

您可以简单地使用您从使用getString()的用户那里获得的<CommandInteraction>#option,并像这样发送它:

const {
    SlashCommandBuilder
} = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('echo')
        .setDescription('returns input')
        .addStringOption(option =>
            option.setName('input')
            .setDescription("the option to echo back")
            .setRequired(true)),


    async execute(interaction) {
        let input = interaction.options.getString("input")
        await interaction.reply({
            content: input
        });
    }
}

Javascript相关问答推荐

如何解决(不忽略)reaction详尽的linter规则,而不会在获取数据时导致无限的reender循环

Mongodb拥有5亿个文档,我想根据JavaScript驱动程序中的两个字段使用regEx进行搜索,而不是模式

如果没有尾随斜线,托管在收件箱中的React/Vite将无法工作

获取表格的左滚动位置

使用redux-toolet DelivercThunks刷新访问令牌

使用AJX发送表单后,$_Post看起来为空

被CSS优先级所迷惑

我们如何从一个行动中分派行动

空的结果抓取网站与Fetch和Cheerio

将本机导航路由react 到导航栏中未列出的屏幕?

当使用';字母而不是与';var#39;一起使用时,访问窗口为什么返回未定义的?

try 将Redux工具包与MUI ToggleButtonGroup组件一起使用时出错

顶点图使用组标签更新列之间的条宽

如何在每隔2分钟刷新OKTA令牌后停止页面刷新

如何在Java脚本中并行运行for或任意循环的每次迭代

连续添加promise 时,如何在所有promise 都已结算时解除加载覆盖

设置复选框根据选中状态输入选中值

将延迟加载的模块转换为Eager 加载的模块

在点击链接后重定向至url之前暂停

每隔一行文本段落进行镜像(Boustrophedon)