我试图用新的discord.js版本编写一个机器人,但是当我尝试登录时,它会提示我这个错误
/home/max/Schreibtisch/Discord Bots/Perplex/node_modules/discord.js/src/util/Util.js:279
if (!Object.hasOwn(given, key) || given[key] === undefined) {
^
TypeError: Object.hasOwn is not a function
at mergeDefault (/home/max/Schreibtisch/Discord Bots/Perplex/node_modules/discord.js/src/util/Util.js:279:17)
at new BaseClient (/home/max/Schreibtisch/Discord Bots/Perplex/node_modules/discord.js/src/client/BaseClient.js:25:20)
at new Client (/home/max/Schreibtisch/Discord Bots/Perplex/node_modules/discord.js/src/client/Client.js:43:5)
at Object.<anonymous> (/home/max/Schreibtisch/Discord Bots/Perplex/src/bot.js:26:16)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:988:32)
at Function.Module._load (node:internal/modules/cjs/loader:828:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47
这是我的机器人代码
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'ping') {
await interaction.reply('Pong!');
}
});
client.login('token');
发布于 2022-07-23 17:20:32
我不认为这个代码片段中有错误。如果这是您的src/bot.js
文件,那么它似乎没有问题。
但是,错误说"Object.hasOwn不是一个函数“,Object.hasOwn
只在node.js v16.9.0之后才可用。
Discord.js v14需要node.js 16.9或更高版本,所以请确保您是最新的。若要检查节点版本,请在终端或命令提示符中使用node -v
,如果它比此更旧,则使用update it!
要了解更多的突破性更改,您可以查看以下答案:Discord.js v13 code breaks when upgrading to v14
https://stackoverflow.com/questions/73092593
复制相似问题