【问题标题】:discord bot -- node index.js problem -- "client is not defined"不和谐机器人——节点 index.js 问题——“客户端未定义”
【发布时间】:2021-09-04 22:57:04
【问题描述】:

我一直在开发一个不和谐的机器人,但是当我运行 node main.js 时,它会回复以下回复: (我已将index.js 更改为main.js,因为这就是文件的名称)

/Users/Liam/Desktop/Discordbot/main.js:7
client.once('ready', () => {
^

ReferenceError: client is not defined
    at Object.<anonymous> (/Users/Liam/Desktop/Discordbot/main.js:7:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

这是我的完整代码:

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

const client = new Discord.Client();

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

client.login("TOKEN");

【问题讨论】:

  • 是否安装了discord.js?你试过用撇号吗?:const Discord = require('discord.js')
  • 完整的代码不是发生错误时使用的代码。 (错误说 client.on 行在第 7 行,但在您发布的代码中它在第 5 行)

标签: javascript node.js discord.js bots


【解决方案1】:

我认为您复制了错误的错误,因为当我尝试重新创建此错误时,它给了我不同的错误,它有一个简单的解决方案。

client.login 接受一个私人不和谐令牌,我猜您可能直接从指南粘贴了“TOKEN”字符串。

要创建机器人令牌,请关注https://discordjs.guide/ 的官方文档

创建令牌后,您可以这样做:

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

const client = new Discord.Client();

const token = 'ThePrivateTokenYouGenerated'

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

client.login(token);

【讨论】:

    猜你喜欢
    • 2020-06-23
    • 1970-01-01
    • 2017-04-26
    • 2020-11-10
    • 2022-11-10
    • 2021-08-08
    • 2022-01-08
    • 1970-01-01
    • 2019-02-26
    相关资源
    最近更新 更多