我在服務器上啟動機器人的時候遇到了問題......
我使用process.env來存盤BOT Token和Instagram登錄憑證。
但是在服務器上,由于這個錯誤,我無法使機器人作業。
(node:30365) Warning: 訪問不存在的屬性'padLevels' of module exports內部回圈依賴性
(使用 `node --trace-warnings ...`來顯示警告是在哪里產生的)
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.
在Client.login(ig-bot/node_modules/discord. js/src/client/Client.js:228:52)
在Object.<anonymous>(ig-bot/ig.js:64:8)。
在Module。 _compile (node:internal/modules/cjs/loader:1092:14)
在Object.Module._extensions。 js (node:internal/modules/cjs/loader:1121:10)
在Module。 load (node:internal/modules/cjs/loader: 972: 32)
在Function.Module。 _load (node:internal/modules/cjs/loader:813:14)
在Function。 executeUserEntryPoint [as runMain] (node: internal/modules/run_main:76: 12)
在node:internal/main/run_main_module:17:47 {
[Symbol(code)] 。'TOKEN_INVALID'/span>
}
error。Forever檢測到腳本退出with code。1: code.
error: Script 重啟嘗試 #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.
在Client.login(ig-bot/node_modules/discord. js/src/client/Client.js:228:52)
在Object.<anonymous>(ig-bot/ig.js:64:8)。
在Module。 _compile (node:internal/modules/cjs/loader:1092:14)
在Object.Module._extensions。 js (node:internal/modules/cjs/loader:1121:10)
在Module。 load (node:internal/modules/cjs/loader: 972: 32)
在Function.Module。 _load (node:internal/modules/cjs/loader:813:14)
在Function。 executeUserEntryPoint [as runMain] (node: internal/modules/run_main:76: 12)
在node:internal/main/run_main_module:17:47 {
[Symbol(code)] 。'TOKEN_INVALID'/span>
}
error。Forever檢測到腳本退出with code。1.
它說token是無效的,但這真的沒有任何意義,因為它在我的PC上作業,我可以通過visual studio運行代碼,沒有問題。在我的服務器上,機器人的同一檔案夾在運行時出現了問題(通常我使用config.json來處理令牌,但這次我需要.env檔案)
/處理EN檔案
//Process ENV code
const dotenv = require('dotenv'/span>) 。
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但是這些對我來說都沒有用。
uj5u.com熱心網友回復:
幾個小時后,我找到了一個解決方案。
const path = require('path')
require('dotenv').config({ path: path. resolve(__dirname, ' .env') })
這段代碼將強制運行你的腳本目錄中的.env檔案。console.log(__dirname)將回傳你的.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("我們做到了!", { 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! ')
})
})
//用客戶的token登錄到Discord。
client.login(TOKEN) 。
uj5u.com熱心網友回復:
基于這個答案,我相信你應該做的是sudo IS_PROD=1 forever start yourapp.js
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/329595.html
標籤:
