类型错误:对象没有 hasOwn 方法

13

我尝试使用新的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');
1个回答

18

我认为这段代码没有错误。如果这是您的 src/bot.js 文件,则看起来没有问题。

然而,错误信息显示"Object.hasOwn is not a function",并且Object.hasOwn 仅在 node.js v16.9.0 及以上版本中可用。

Discord.js v14 需要 node.js 16.9 或更高版本,请确保您已经升级到最新版本。使用node -v命令在终端或命令提示符中检查您的 node 版本,如果版本旧于此,请更新它

有关更多重大更改,请查看此答案:Discord.js v13 代码升级到 v14 后出现错误


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接