我如何解决错误 ReferenceError: prefix is not defined?

时间:2021-06-19 01:13:58

标签: javascript node.js discord.js

我的代码:

const Discord = require('discord.js')
const config = require('./config.json');
const client = new Discord.Client();


client.once('ready', () => {
    console.log('Ready!')
})

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();
    if (message.content === '>ping') {
        message.channel.send('Pong.');
    } else if (message.content === '>beep') {
        message.channel.send('Boop.');
    } else if (message.content === '>serverinfo') {
        message.channel.send(`Server name: ${message.guild.name}\nTotal members: ${message.guild.memberCount}`);
    } else if (message.content === '>userinfo') {
        message.channel.send(`Your username: ${message.author.username}\nYour id: ${message.author.id}`);
    }
});

client.login('lmao no');

我的帖子很可能是代码,所以我可以发布它请帮助我需要这个机器人在一个小时内完成

1 个答案:

答案 0 :(得分:1)

要在触发消息事件时使用前缀检查,您需要先定义它。

示例:const prefix = ".";

const Discord = require('discord.js')
const config = require('./config.json');
const client = new Discord.Client();
const prefix = ".";

client.once('ready', () => {
    console.log('Ready!')
})

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();
    if (message.content === '>ping') {
        message.channel.send('Pong.');
    } else if (message.content === '>beep') {
        message.channel.send('Boop.');
    } else if (message.content === '>serverinfo') {
        message.channel.send(`Server name: ${message.guild.name}\nTotal members: ${message.guild.memberCount}`);
    } else if (message.content === '>userinfo') {
        message.channel.send(`Your username: ${message.author.username}\nYour id: ${message.author.id}`);
    }
});

client.login('lmao no');