Node.js 的 Discord 机器人问题

问题描述 投票:0回答:1

我正在尝试制作我的第一个不和谐机器人。这是为了记录我班在学校的奖学金积分。我和一个朋友在 ChatGPT 的帮助下编写了一些代码。为了测试代码,我正在执行“node bot.js”命令并收到此问题。

`name@name-machine discord % node bot.js
/Applications/XAMPP/xamppfiles/htdocs/discord/bot.js:5
        Intents.FLAGS.GUILDS,
                ^

TypeError: Cannot read properties of undefined (reading 'FLAGS')
    at Object.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/discord/bot.js:5:17)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49

Node.js v20.11.1`

代码如下。 “TOKEN”是为我提供的不和谐机器人令牌。

    const { Client, Intents } = require('discord.js');

const client = new Client({ 
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES
    ]
});

client.login('TOKEN');

const points = {};

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', message => {
    if (message.author.bot) {
    return;
    } // Ignore messages from bots

    const args = message.content.trim().split(/ +/);
    const command = args.shift().toLowerCase();

    if (command.startsWith('/')) {
        const name = command.substring(1);
        if (!points.hasOwnProperty(name)) {
            points[name] = 0;
            message.channel.send(`Added ${name} to the scholarship points list.`);
        } else {
            const amount = parseInt(args[0]);
            if (!isNaN(amount)) {
                points[name] += amount;
                message.channel.send(`Added ${amount} points to ${name}. Total points: ${points[name]}`);
            } else {
                message.channel.send('Invalid points value. Please provide a valid number.');
            }
        }
    } else if (command === '/points') {
        let leaderboard = 'Scholarship Points Leaderboard:\n';
        Object.keys(points).forEach(name => {
            leaderboard += `${name}: ${points[name]} points\n`;
        });
        message.channel.send(leaderboard);
    }
});
node.js discord
1个回答
0
投票

在 v14 中,

INTENTS
枚举已移至
GatewayIntentBits
(有关详细信息,请参阅 https://discordjs.guide/additional-info/changes-in-v14.html#enum-values):

const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({ 
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages
    ]
});
© www.soinside.com 2019 - 2024. All rights reserved.