【发布时间】:2022-01-13 23:48:03
【问题描述】:
在我的 discord 机器人中,我试图在启动脚本中运行 2 个文件:index.js 和 deploycommands.js。它正在正确运行index.js,但没有运行deploycommands.js。
当我使用 node deploycommands.js 手动运行 deploycommands.js 时,它会引发错误,我在下面提供了该错误。
deploycommands.js(文件在根目录下)
//Imports
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const fs = require('fs');
const rest = new REST({ version: '9' }).setToken(process.env.token);
const commands = [];
const guilds = [
'833289549341523998',
'804731876950147122',
'793019059691192370',
'853617621547352104',
'872713876284792892',
'882096964295082004',
'851070792196030524',
'904751148404203522',
]
//Pushing command files to 'commands' array
fs.readdirSync("./commands").forEach(dir => {
fs.readdir(`./commands/${dir}`, (err, files) => {
if (err) {
throw err
return
}
const commandFiles = files.filter(file => file.endsWith(".js"));
if (commandFiles.length <= 0) return
commandFiles.forEach(file => {
const command = require(`./commands/${dir}/${file}`);
try {
commands.push(command)
} catch (err) {
return console.log(`[ERROR] - ${error}`);
}
});
});
});
//Pushing the array to guild commands
for (const guild of guilds) {
(async () => {
try {
console.log('Started refreshing guild commands.');
await rest.put(
Routes.applicationGuildCommands(process.env.clientId, guild),
{ body: commands },
);
console.log('Successfully reloaded guild commands.');
} catch (error) {
console.error(error);
}
})();
}
//Pushing the array to global commands
(async () => {
try {
console.log('Started refreshing global commands.');
await rest.put(
Routes.applicationCommands(process.env.clientId),
{ body: commands },
);
console.log('Successfully reloaded global commands.');
} catch (error) {
console.error(error);
}
})();
我用来运行机器人的脚本:
{
"scripts": {
"index": "node .",
"deploycommands": "node deploycommands.js",
"start": "npm run index && npm run deploycommands",
}
}
我正在使用npm start 运行机器人
当我尝试手动运行deploycommands.js 时抛出的错误
/home/runner/MultiBot/node_modules/@discordjs/rest/dist/lib/RequestManager.js:66
const hash = this.hashes.get(`${request.method}:${routeID.bucketRoute}`) ?? `Global(${request.method}:${routeID.bucketRoute})`;
^
SyntaxError: Unexpected token '?'
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/home/runner/MultiBot/node_modules/@discordjs/rest/dist/index.js:7:22)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
编辑:
错误是由deploycommands.js中的第一次导入引起的:const { REST } = require('@discordjs/rest');。
当我注释掉该行时,它会在第 6 行引发错误,提示 REST is not defined,这是预期的
【问题讨论】:
-
@MrMythical 我收到一个错误gist.github.com/TechPro424/7daa6121d52d50c1bd441f164f8433b5
标签: javascript node.js discord.js