UnhandledPromiseRejectionWarning:ReferenceError:名称未定义

时间:2019-06-28 09:03:44

标签: javascript discord discord.js

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

  

UnhandledPromiseRejectionWarning:ReferenceError:名称未定义

当我不在语音通道中并键入命令!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并标记员工和助手,并说出用户连接的语音通道名称。

2 个答案:

答案 0 :(得分:0)

message.member.voiceChannel未定义,因此message.member.voiceChannel.name将引发错误。您可以这样修复它:


let room = message.member.voiceChannel;

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

答案 1 :(得分:0)

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

在使用message.member.voiceChannel之前,请确保已定义。当iffalsy时,下面的message.member.voiceChannel语句将返回true,这表示用户在语音通道中不是

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

let room = message.member.voiceChannel.name;

还请确保捕获代码中所有被拒绝的Promises,以防止发生UnhandledPromiseRejectionWarning。使用try...catch语句或catch()方法。