You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js中interaction.reply非函数的TypeScript斜杠命令问题求助

问题:Discord.js斜杠命令/ping触发TypeError: interaction.reply is not a function

错误信息

TypeError: interaction.reply is not a function
    at Object.<anonymous> (C:\Users\timda\code\discord bot\bot-data\commands\ping.ts:8:15)
    at Generator.next (<anonymous>)
    at C:\Users\timda\code\discord bot\bot-data\commands\ping.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\timda\code\discord bot\bot-data\commands\ping.ts:4:12)
    at Object.execute (C:\Users\timda\code\discord bot\bot-data\commands\ping.ts:18:16)   
    at C:\Users\timda\code\discord bot\main.ts:43:18
    at Generator.next (<anonymous>)
    at C:\Users\timda\code\discord bot\main.ts:8:71
    at new Promise (<anonymous>)

相关代码

/ping命令代码

import { SlashCommandBuilder } from 'discord.js';

module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Replies with Pong!'),
    async execute(interaction: { reply: (arg0: string) => any }) {
        console.log(interaction);
        interaction.reply('Pong!');
    },
};

主文件加载命令代码

import fs from 'node:fs';
import path from 'node:path';
import { Client, Collection, GatewayIntentBits, Partials, REST, Routes } from 'discord.js';
import { clientId, token } from './config.json';

const client = new Client({ 
    intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages, GatewayIntentBits.MessageContent],
    partials: [Partials.Message, Partials.Channel],
    presence: { status: 'online', activities: [{ name: 'you 👀', type: 3 }] }
});

(client as any).commands = new Collection();
const commands: any[] = [];

for (const file of (fs.readdirSync(path.join('./bot-data/commands')).filter(file => file.endsWith('.ts')))) {
    const command = require(`./bot-data/commands/${file}`);
    (client as any).commands.set(command.data.name, command);
    commands.push(command.data.toJSON());
};

client.on('ready', async (client: { user: { tag: any; }; }) => {
    const rest = new REST({ version: '10' }).setToken(token);

    try {
        await rest.put(Routes.applicationCommands(clientId), { body: commands});
    } catch (error) {
        console.error(error);
    }

    console.log(`Succesfully logged in as ${client.user.tag}!`);
});

client.on('interactionCreate', async interaction => {
    if(interaction.isCommand()) {
        const command = (client as any).commands.get(interaction.commandName);

        if (!command) {
            await interaction.reply({content: `Something went wrong while executing ${interaction.commandName}.`, ephemeral: true});
            return;
        }

        try {
            await command.execute(client, interaction);
        } catch (error) {
            await interaction.reply({content: `Something went wrong while executing ${interaction.commandName}., ephemeral: true});
            console.error(error);
        }
    }
});

问题根源

  1. 参数顺序不匹配:主文件调用command.execute(client, interaction)时,将client作为第一个参数传入,但/ping命令的execute函数期望第一个参数是interaction。此时函数内的interaction变量实际指向client对象,而client没有reply方法,直接触发报错。
  2. 自定义类型不规范:手动定义的interaction: { reply: (arg0: string) => any }类型不准确,未使用Discord.js官方提供的类型,既丢失了TypeScript的类型校验能力,也无法正确识别交互对象的完整结构。
  3. 主文件语法错误:catch块中的interaction.reply配置对象缺少闭合引号,会引发额外的语法问题。

修复方案

1. 修正execute调用的参数顺序

在主文件的interactionCreate事件中,调整调用参数顺序:

// 若命令不需要client,修改为
await command.execute(interaction);

// 若需要传递client,同步修改命令的execute参数定义(见下文)

2. 使用Discord.js官方类型定义

修改/ping命令代码,导入ChatInputCommandInteraction类型并正确定义参数:

import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';

module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Replies with Pong!'),
    async execute(interaction: ChatInputCommandInteraction) {
        console.log(interaction);
        await interaction.reply('Pong!'); // 添加await确保异步操作完成
    },
};

如果需要传递client,则调整为:

import { SlashCommandBuilder, ChatInputCommandInteraction, Client } from 'discord.js';

module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Replies with Pong!'),
    async execute(client: Client, interaction: ChatInputCommandInteraction) {
        console.log(interaction);
        await interaction.reply('Pong!');
    },
};

此时主文件的await command.execute(client, interaction)调用就符合参数顺序要求了。

3. 修复主文件的语法错误

修正catch块中的引号缺失问题:

// 原错误代码
await interaction.reply({content: `Something went wrong while executing ${interaction.commandName}., ephemeral: true});

// 修改后
await interaction.reply({content: `Something went wrong while executing ${interaction.commandName}.`, ephemeral: true});

内容的提问来源于stack exchange,提问作者user20471569

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 05:50:40