Discord.js无法永久在服务器上启动带有process.env的机器人,但它可以通过Visual Studio在我的本地驱动器上运行,没有问题



我在服务器上启动bot永远有问题…我使用process.env,在那里我存储BOT令牌和Instagram登录凭据。但是在服务器上,由于这个错误,我无法使bot工作。

(node:30365) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
ig-bot/node_modules/discord.js/src/client/Client.js:228
if (!token || typeof token !== 'string') throw new Error('TOKEN_INVALID');
^
Error [TOKEN_INVALID]: An invalid token was provided.
at Client.login (ig-bot/node_modules/discord.js/src/client/Client.js:228:52)
at Object.<anonymous> (ig-bot/ig.js:64:8)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47 {
[Symbol(code)]: 'TOKEN_INVALID'
}
error: Forever detected script exited with code: 1
error: Script restart attempt #1
ig-bot/node_modules/discord.js/src/client/Client.js:228
if (!token || typeof token !== 'string') throw new Error('TOKEN_INVALID');
^
Error [TOKEN_INVALID]: An invalid token was provided.
at Client.login (ig-bot/node_modules/discord.js/src/client/Client.js:228:52)
at Object.<anonymous> (ig-bot/ig.js:64:8)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47 {
[Symbol(code)]: 'TOKEN_INVALID'
}
error: Forever detected script exited with code: 1

它说令牌是无效的,但它真的没有任何意义,因为它在我的PC上工作,我可以通过visual studio运行代码没有问题。同一文件夹的机器人有麻烦在我的服务器上运行(通常我使用配置。Json作为令牌,但这次我需要.env文件)

//处理ENV代码

const dotenv = require('dotenv');
dotenv.config({ path: './process.env' });
const username = process.env.IGUSERNAME
const password = process.env.IGPASSWORD
const TOKEN = process.env.TOKEN
client.login(TOKEN);

我试过ENV=production forever start yourApp.jsforever -c "node -r dotenv/config" --workingDir app-workdir-path start app.js,但这些都不适合我…

几个小时后,我终于找到了解决办法。

const path = require('path')
require('dotenv').config({ path: path.resolve(__dirname, '.env') })

这段代码将强制从脚本目录运行.env文件。console.log(__dirname)将返回path到.js文件。它解决了整个问题。

//Full code example
const path = require('path')
require('dotenv').config({ path: path.resolve(__dirname, '.env') })
const TOKEN = process.env.TOKEN

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.on('ready', () => {
client.user.setActivity("WE DID IT!", { type: 'PLAYING' });
client.guilds.cache.forEach(guild => {
let channel = guild.channels.cache.get('channelID')
channel.send(`I'm finally ready!`);
console.log('Wohoo! Bot is online!')
})
})
// Login to Discord with your client's token
client.login(TOKEN);

基于这个答案,我相信你应该做sudo IS_PROD=1 forever start yourapp.js

Node.js永远使用环境变量

最新更新