【问题标题】:UnhandledPromiseRejectionWarning: ReferenceError: name is not definedUnhandledPromiseRejectionWarning: ReferenceError: name is not defined
【发布时间】:2019-11-10 05:43:24
【问题描述】:

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

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 并标记员工和助手并说出用户连接的语音通道名称。

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    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:**");
    }
    

    【讨论】:

    • 如果 message.member.voiceChannel 未定义,room 将未定义。 room.name 将读取未定义的属性name,返回相同的错误。
    【解决方案2】:

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

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

    if (!message.member.voiceChannel) return; // Or return an error message.
    
    let room = message.member.voiceChannel.name;
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-03
      • 2018-12-26
      相关资源
      最近更新 更多