【问题标题】:Discord Bot initialization FailureDiscord Bot 初始化失败
【发布时间】:2021-12-19 17:50:25
【问题描述】:

我在为我的 discord 机器人创建主文件时遇到问题。目前,我在尝试让我的机器人工作时遇到问题,但遗憾的是,此错误消息已从控制台输出。

PS C:\Users\Kelvin Wang\Documents\Projects\DiscordBot> node index.js
node:internal/modules/cjs/loader:1170
    throw err;
     ^

SyntaxError: C:\Users\Kelvin Wang\Documents\Projects\DiscordBot\config.json: Unexpected end of JSON input

    at parse (<anonymous>)
    at Object.Module._extensions..json (node:internal/modules/cjs/loader:1167:22)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Users\Kelvin Wang\Documents\Projects\DiscordBot\index.js:4:19)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)

我在 index.js 中的代码是这样的:

//initial discord initialization of client and library
// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
const dotenv = require('dotenv');

// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('Ferraribot is online!');
});

// Login to Discord with your client's token
client.login(token);

虽然我的 config.json 看起来像这样:

{
"token": "token"
}

任何帮助将不胜感激!这是我第一次使用堆栈溢出,所以对于我所做的任何错误格式,我深表歉意。提前致谢!

【问题讨论】:

  • 请编辑您的问题并删除您的机器人令牌以防止其他用户访问您的机器人,然后重新生成一个新令牌以确保安全:)
  • 哦,是的,我忘了隐藏我的机器人令牌,谢谢提醒 :)
  • @KelW 重新生成您的令牌,因为如果有人试图查看人们编辑的内容,它在历史记录中可用,例如您的令牌看起来像这样 - OTA2MTQxNDM0MjMzNjQzMDE5.YYUT-Q.rdlx5P4CxPH5ShKINNBrp3*****

标签: javascript node.js json discord discord.js


【解决方案1】:

您的代码看起来不错。

错误说,您的代码在读取 JSON 配置时存​​在一些问题。 (是在同一个目录吗?)

  • 首先使用 index.js 中的令牌尝试您的代码。 client.login(*****) // 只是不要上传,例如github ;)
  • 其次,我认为使用 .env 处理敏感信息会更好。 https://www.npmjs.com/package/dotenv

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-25
    • 2012-11-17
    • 2017-12-10
    • 2018-09-17
    • 1970-01-01
    • 2018-10-13
    • 2014-03-13
    • 1970-01-01
    相关资源
    最近更新 更多