Discord.js 永远无法在服务器上使用 process.env 启动机器人,但它可以通过 Visual Studio 在我的本地驱动器上正常运行
Discord.js can't launch bot with process.env on the server with forever, but it works on my local drive via Visual Studio without problems
我在服务器上启动机器人时遇到问题永远...
我正在使用 process.env
存储 BOT 令牌和 Instagram 登录凭据。
但是在服务器上,由于这个错误我无法让机器人工作..
(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
它说令牌无效,但它确实没有任何意义,因为它可以在我的电脑上运行,而且我可以 运行 通过 visual studio 的代码没有问题。机器人的同一文件夹在我的服务器上出现问题 运行ning(通常我使用 config.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.js
forever -c "node -r dotenv/config" --workingDir app-workdir-path start app.js
但是 none 这些对我有用...
基于这个答案,我相信你应该做 sudo IS_PROD=1 forever start yourapp.js
Node.js forever with environment variable
几个小时后我找到了解决方案。
const path = require('path')
require('dotenv').config({ path: path.resolve(__dirname, '.env') })
此代码将强制从您的脚本目录中 运行 您的 .env
文件。 console.log(__dirname)
将 return 你的 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);
我在服务器上启动机器人时遇到问题永远...
我正在使用 process.env
存储 BOT 令牌和 Instagram 登录凭据。
但是在服务器上,由于这个错误我无法让机器人工作..
(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
它说令牌无效,但它确实没有任何意义,因为它可以在我的电脑上运行,而且我可以 运行 通过 visual studio 的代码没有问题。机器人的同一文件夹在我的服务器上出现问题 运行ning(通常我使用 config.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.js
forever -c "node -r dotenv/config" --workingDir app-workdir-path start app.js
但是 none 这些对我有用...
基于这个答案,我相信你应该做 sudo IS_PROD=1 forever start yourapp.js
Node.js forever with environment variable
几个小时后我找到了解决方案。
const path = require('path')
require('dotenv').config({ path: path.resolve(__dirname, '.env') })
此代码将强制从您的脚本目录中 运行 您的 .env
文件。 console.log(__dirname)
将 return 你的 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);