首页 > 解决方案 > UnhandledPromiseRejectionWarning: ReferenceError: name is not defined

问题描述

在我的机器人中,我收到错误:

UnhandledPromiseRejectionWarning: ReferenceError: name is not defined

当我不在语音频道中并键入命令时,就会出现问题!ph

该命令标记了助手角色 ( @helpers) 和员工角色 ( @staff),当我在语音频道中时,它可以工作。

错误在if(commandfile) commandfile.run(bot, message, args);主配置中。

命令的代码!ph

const Discord = require("discord.js");
const client = new Discord.Client();

module.exports.run = async (bot, message, args) => {

    let target = message.mentions.users.first() || message.author;
    let room = message.member.voiceChannel.name;

    if (!room) {
        return message.channel.send("<@&587662170548994076>" + " <@&594077199859187723> " + "**!צריך את עזרתכם** " + target + " ** :name_badge: המשתמש לא נמצא בשום חדר**\n");
    } else {

        return message.channel.send("<@&587662170548994076> <@&594077199859187723> " + target + " **!צריך את עזרתכם** \n" + " `` " + room + " `` " + "**המשתמש נמצא בחדר :bell:**");

    }

}

module.exports.help = {
    name: "ph"
}

机器人的代码:

const botconfig = require("./botconfig.json");
const Discord = require("discord.js");
const fs = require("fs");
const bot = new Discord.Client({disableEveryone: true});
bot.commands = new Discord.Collection();
let xp = require("./xp.json");
let purple = botconfig.purple;

fs.readdir("./commands/", (err, files) => {

    if (err) console.log(err);

    let jsfile = files.filter(f => f.split(".").pop() === "js")
    if (jsfile.length <= 0) {
        console.log("Couldn't find commands.");
        return;
    }

    jsfile.forEach((f, i) => {
        let props = require(`./commands/${f}`);
        console.log(`${f} loaded!`)
        bot.commands.set(props.help.name, props);
    });

})


bot.on("ready", async () => {
    console.log(`${bot.user.username} is online!`);
    bot.user.setGame("PeDiXOL Server!");
});

bot.on("message", async message => {
    if (message.author.bot) return;
    if (message.channel.type === "dm") return;

    let prefix = botconfig.prefix;
    let messageArray = message.content.split(" ");
    let cmd = messageArray[0];
    let args = messageArray.slice(1)

    let commandfile = bot.commands.get(cmd.slice(prefix.length));
    if (commandfile) commandfile.run(bot, message, args);

    let xpAdd = Math.floor(Math.random() * 7) + 8
    console.log(xpAdd)

    if (!xp[message.author.id]) {
        xp[message.author.id] = {
            xp: 0,
            level: 1
        };
    }

    let curxp = xp[message.author.id].xp;
    let curlvl = xp[message.author.id].level;
    let nxtLvl = xp[message.author.id].level * 300;
    xp[message.author.id].xp = curxp + xpAdd;
    if (nxtLvl <= xp[message.author.id].xp) {
        xp[message.author.id].level = curlvl + 1;
        let lvlup = new Discord.RichEmbed()
            .setTitle("Level Up!")
            .setColor(purple)
            .addField("New Level", curlvl + 1);

        message.channel.send(lvlup).then(msg => {
            msg.delete(5000)
        });

    }

    fs.writeFile("./xp.json", JSON.stringify(xp), (err) => {
        if (err) console.log(err)
    });

});

bot.login(botconfig.token);

当您不在语音频道中时,这需要输出标记助手和员工并说您不在语音频道中的内容。

当您在语音频道中时,此输出the username needs help并标记员工和助手,并说出用户连接的语音频道名称。

标签: javascriptdiscorddiscord.js

解决方案


message.member.voiceChannel未定义,因为用户不在语音通道中。当您尝试读取其name属性时,由于未定义语音通道而引发错误。

在使用之前message.member.voiceChannel,请确保它已定义。下面的if语句将在message.member.voiceChannelfalsy时返回 true ,这意味着用户不在语音通道中。

if (!message.member.voiceChannel) return; // Or return an error message.

let room = message.member.voiceChannel.name;

还要确保在代码中捕获任何被拒绝的Promise,以防止出现 UnhandledPromiseRejectionWarning。使用try...catch语句或catch()方法这样做。


推荐阅读