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

Node.js开发Discord经济机器人遇'is not a function'错误求助

修复Discord经济机器人/money命令的"is not a function"错误

错误根源

你在index.js里导入的data是命令文件中SlashCommandBuilder生成的命令定义对象,不是可执行函数,但你却把它当成函数调用data(client, interaction),这直接导致了类型错误。命令文件里真正处理业务逻辑的是导出的run方法。

修复步骤

1. 修改index.js的导入与调用逻辑

放弃只导入data,改为导入整个命令模块,然后调用模块内的run方法:

const Discord = require('discord.js');
const config = require('./config.json');
const client = new Discord.Client({
    intents: [
        Discord.GatewayIntentBits.Guilds,
        Discord.GatewayIntentBits.GuildMembers,
        Discord.GatewayIntentBits.GuildMessages,
    ]
})
// 导入整个money命令模块
const moneyCommand = require('./comandos/dinero');

client.on('interactionCreate', async interaction => {
    if (!interaction.isChatInputCommand()) return;

    if (interaction.commandName === 'money') {
        // 调用模块里的run方法执行命令逻辑
        await moneyCommand.run(client, interaction)
    }
});

2. 优化命令文件的run方法(可选)

命令文件里的return this.data没有实际作用,可以删掉,让代码更简洁:

const { SlashCommandBuilder } = require('discord.js');
const ecoSchema = require('../modelos/profileSchema');
const { saveCurrencyData } = require('../handlers/funcion');

module.exports = {
    data: new SlashCommandBuilder()
    .setName('money')
    .setDescription('Da dinero gratis!'),

    name: 'money',
    run: async(client, interaction) => {
        await saveCurrencyData(interaction.user.id)
        // 建议加上upsert: true,确保用户不存在时自动创建条目
        await ecoSchema.findOneAndUpdate(
            { UserID: interaction.user.id }, 
            { $inc: {banco: 100}},
            { upsert: true }
        );
        await interaction.reply({ content: `100$ han sido añadidos a tu cuenta`})
    }
}

额外提示

  • 确保saveCurrencyData函数能正常完成用户初始数据的创建逻辑,如果已经在findOneAndUpdate里加了upsert: true,可以根据实际情况调整两者的逻辑,避免重复操作。
  • 确认Discord机器人已完成登录,且/money命令已经成功注册到目标服务器(全局命令需等待约1小时生效,服务器命令通常几分钟内即可用)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 07:27:27