【问题标题】:ReferenceError: client isn't definedReferenceError:未定义客户端
【发布时间】:2021-04-16 06:02:55
【问题描述】:

我刚开始构建新的机器人,每当我尝试使用 node 时。在终端我得到这样的错误;

ReferenceError: client is not defined
    at Object.<anonymous> (C:\Users\Balkanski\Desktop\Bot discord\index.js:9:1)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

这是我的全部代码,我不知道问题出在哪里;

const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = '+';

const fs = require('fs');

client.command = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require(`./commands/${file}`);

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

client.once('ready', () =>{
    console.log('Bot is turned on');
}),

client.log('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.lenght).split(/ +/);
    const command = args.shift().toLowerCase
    
    if(command === 'bok'){
        client.commands.get('ping').execute(message, args);
    }
})      

client.login('token');

【问题讨论】:

  • 我可以看到所有的代码吗?
  • 错误不在您发布的代码部分。
  • 如果你只运行发布的代码,你会得到那个错误吗?
  • 我现在发布了整个代码,检查一下
  • @evolutionxbox 如果我只运行最初显示的代码,我仍然会收到错误

标签: javascript node.js discord.js


【解决方案1】:

确保重新安装discord.js 并尝试以下代码:

const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = '+';

const fs = require('fs');

client.command = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require(`./commands/${file}`);

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

client.on('ready', () =>{
    console.log('Bot is turned on');
}),

client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.lenght).split(/ +/);
    const command = args.shift().toLowerCase
    
    if(command === 'bok'){
        client.command.get('ping').execute(message, args);
    }
})      

client.login('token');

【讨论】:

    猜你喜欢
    • 2021-08-08
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 2020-10-08
    • 2013-10-04
    相关资源
    最近更新 更多