javascript - 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
并标记员工和助手,并说出用户连接的语音频道名称。
解决方案
message.member.voiceChannel
未定义,因为用户不在语音通道中。当您尝试读取其name
属性时,由于未定义语音通道而引发错误。
在使用之前message.member.voiceChannel
,请确保它已定义。下面的if
语句将在message.member.voiceChannel
为falsy时返回 true ,这意味着用户不在语音通道中。
if (!message.member.voiceChannel) return; // Or return an error message.
let room = message.member.voiceChannel.name;
还要确保在代码中捕获任何被拒绝的Promise,以防止出现 UnhandledPromiseRejectionWarning。使用try...catch
语句或catch()
方法这样做。
推荐阅读
- spring - spring data JPA中带有@Idclass的复合主键在调用Spring data JPA的save方法时插入新记录而不是更新
- javascript - Jquery获取客户端宽度的DOM值
- pytorch - 为什么使用 GPT2Tokenizer 将阿拉伯字符转换为符号?
- c++ - 调用 GLEW 函数时进程以退出代码 -1073741515 完成
- android - 使用 Post API 尝试 Retrofit2,响应显示成功但返回垃圾值
- c# - c# 按索引对列表进行分组
- django - 获取上传文件的Django模型名称
- c++ - cin.fail() 和 !(cin>>var) 在输入多个整数然后是字符串或字符时失败
- c# - 将十六进制转换为 unicode
- javascript - 在 ReactJs 中制作计时器/倒计时代码时遇到问题