繁体   English   中英

如何修复错误 ReferenceError: prefix is not defined?

[英]How do i fix the error ReferenceError: prefix is not defined?

我的代码:

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');

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

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

例子: 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');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM