当我尝试使用 node.js 运行我的 discord 机器人时,我总是收到错误消息
所以我一直收到这个特定的错误,无论我尝试什么,错误每次都保持不变,我显示我的索引文件的唯一原因是因为它在我更新索引以采用不和谐斜杠命令时开始,索引文件用于node.js 是一个无权访问的文件,我对所有这些都使用 replit,所以除非我显示隐藏文件,否则我无法直接访问该文件。
错误:
/home/runner/blah-blah/node_modules/@discordjs/rest/dist/index.js:64
var DefaultUserAgentAppendix = import_node_process.default.release?.name === "node" ? Node.js/${import_node_process.default.version} : "";
^
SyntaxError: Unexpected token '.'
这是我的索引文件:
const express = require('express');
const { Client, Collection, Intents } = require('discord.js');
const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
require('dotenv').config();
const app = express();
app.get('/', (req, res) => {
res.send('Hello Express app!');
});
app.listen(3000, () => {
console.log('Server started');
});
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
clientmands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
clientmands.set(command.data.name, command);
}
const rest = new REST({ version: '9' }).setToken(process.env.token);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationCommands(process.env.clientId),
{ body: clientmands.map(command => command.data.toJSON()) }
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
client.once('ready', () => {
console.log('Bot is online');
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = clientmands.get(interactionmandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'An error occurred while executing this command.', ephemeral: true });
}
});
const nodeProcess = require('process');
const releaseName = nodeProcess.release ? nodeProcess.release.name : null;
const nodeVersion = nodeProcess.version;
const DefaultUserAgentAppendix = releaseName && releaseName === 'node' ? `Node.js/${nodeVersion}` : '';
// Manually set the user agent
rest.userAgent = `DiscordBot (${process.env.clientId}, ${client.user.username}) ${DefaultUserAgentAppendix}`;
client.login(process.env.token).catch(() => {
console.log('Invalid token');
});
这是我的 package.json
{
"name": "Bucket-Giveaways-Bot",
"version": "1.3.0",
"description": "A simple discord giveaway bot",
"main": "index.js",
"dependencies": {
"@discordjs/builders": "^1.6.3",
"@discordjs/rest": "^1.7.1",
"discord-api-types": "^0.37.42",
"discord-giveaways": "^4.5.1",
"discord.js": "^12.5.3",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"ms": "^2.1.3"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Bucket",
"license": "ISC"
}
我从来没有真正使用过 node.js,我通常用 python 制作我的机器人,但我想挑战自己,我尝试使用 chat gpt,maily bc 我没有任何编码朋友,甚至 chat gpt 都无法修复它
回答如下: