如何解决Discord.js 变量在定义时不存在?
我正在尝试使用 discord.js 和 minecraft-server-util 编写一个 bot 命令,但我的代码没有使用我定义的 const 并且说它不存在。如果您发现任何其他问题,您可以纠正我。
client.on('message',message => {
if (message.content === `${prefix}javaserverstatus`) {
const args = message.content.slice(prefix.length).trim().split(' '); // this is where args is defined.
if (!args.length) {
return message.channel.send(`You didn't provide a server,${message.author}!`); // checks if args exist
}
if (args.length > 1) {
return message.channel.send(`Wrong input! EG. play.hypixel.net,${message.author}`); // checks if there are only 1 arguments
}
}
const util = require('minecraft-server-util');
var serverinfo = null
util.status(args) // This code is giving an error saying args does not exist.
.then((response) => {
console.log(response);
serverinfo = response;
const embed = new discord.MessageEmbed()
.setTitle('play.hypixel.net Server Status')
.setColor(0xff0000)
.setDescription('IP: ' + response.host + '\n' + 'Port: ' + response.port + '\n' + 'Version: ' + response.version + '\n' + 'Online Players: ' + response.onlinePlayers.toString() + '\n' + 'Max Players: ' + response.maxPlayers.toString() + '\n');
message.channel.send(embed);
})
.catch((error) => {
console.error(error);
});
});
解决方法
错误是由于您在其中定义了 args 的范围。 args 是在您的第一个 if 语句中定义的,而它应该在上面的一行中定义。
const args = message.content.slice(prefix.length).trim().split(' ');
if (message.content === `${prefix}javaserverstatus`) {
// this is where args is defined.
if (!args.length) {
return message.channel.send(`You didn't provide a server,${message.author}!`); // checks if args exist
}
if (args.length > 1) {
return message.channel.send(`Wrong input! EG. play.hypixel.net,${message.author}`); // checks if there are only 1 arguments
}
}
,
您已经在 if 语句中定义了 args。你需要把它放在那里。
client.on('message',(message) => {
const args = message.content.slice(prefix.length).trim().split(' ') // this is where args is defined.
if (message.content === `${prefix}javaserverstatus`) {
if (!args.length) {
return message.channel.send(`You didn't provide a server,${message.author}!`) // checks if args exist
}
if (args.length > 1) {
return message.channel.send(`Wrong input! EG. play.hypixel.net,${message.author}`) // checks if there are only 1 arguments
}
}
const util = require('minecraft-server-util')
var serverinfo = null
util
.status(args) // This code is giving an error saying args does not exist.
.then((response) => {
console.log(response)
serverinfo = response
const embed = new Discord.MessageEmbed()
.setTitle('play.hypixel.net Server Status')
.setColor(0xff0000)
.setDescription('IP: ' + response.host + '\n' + 'Port: ' + response.port + '\n' + 'Version: ' + response.version + '\n' + 'Online Players: ' + response.onlinePlayers.toString() + '\n' + 'Max Players: ' + response.maxPlayers.toString() + '\n')
message.channel.send(embed)
})
.catch((error) => {
console.error(error)
})
})
如果这不起作用,请将您在不和谐聊天中发送的命令发送给我。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。