我一直关注this discord.js bot tutorial serie,但发现我无法解决的错误。当您给它一个id时,该命令有效,但是当您不给它任何东西时,它不显示错误行,它应该在控制台中显示给我一个错误。
这是没有一些不必要的行或有效行的代码:
const Discord = require("discord.js");
const botconfig = require("../botconfig.json");
const colours = require("../colours.json");
module.exports.run = async (bot, message, args) => {
if(!message.member.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"])) return message.channel.send("...")
let bannedMember = await bot.users.fetch(args[0]) //I believe the error is somewhere in this line maybe because of the promise
if(!bannedMember) return message.channel.send("I need an ID")
let reason = args.slice(1).join(" ")
if(!reason) reason = "..."
try {
message.guild.members.unban(bannedMember, {reason: reason})
message.channel.send(`${bannedMember.tag} ha sido readmitido.`)
} catch(e) {
console.log(e.message)
}
}
这是错误:
(node:19648) UnhandledPromiseRejectionWarning: DiscordAPIError: 404: Not Found
at RequestHandler.execute (C:\Users\Anton\Desktop\Bob\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:19648) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was
not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:19648) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
此外,当您给它提供的不是id的东西,例如!unban asdf 时,我会遇到此错误:
(node:17824) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body
user_id: Value "asdf" is not snowflake.
at RequestHandler.execute (C:\Users\Anton\Desktop\Bob\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:17824) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我不知道第一个错误出了什么问题,对于第二个错误,我想我只需要检查args[0]
是 id 还是雪花,但我不知道如何。
答案 0 :(得分:1)
我已经设法为我想做的事情提供了适当的解决方案,但首先我想评论几件事:
正如Zer0所说,如果bannedMember = await bot.users.fetch(args[0])
返回一个错误,然后我们用if(!bannedMember)
进行检查,就好像!!bannedMember
将其变成一个真实的语句一样,但是对于 if 条件语句:
Use if to specify a block of code to be executed, if a specified condition is true.
这就是为什么我们使用if(!condition)
检查条件是否为假的原因。
但是这里的问题不是那样。问题是 await 函数是异步函数的整体。这意味着,如果正在调用的 promise 并没有在调用时到达,它会伴随我所犯的错误而没有继续其余的代码。这是一个朋友给我的解决方案以及我最终使用的解决方案的出处,并且效果很好:
module.exports.run = async (bot, message, args) => {
if(!message.member.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"])) return message.channel.send("You can't do that.")
if(!args[0]) return message.channel.send("Give me a valid ID");
//This if() checks if we typed anything after "!unban"
let bannedMember;
//This try...catch solves the problem with the await
try{
bannedMember = await bot.users.fetch(args[0])
}catch(e){
if(!bannedMember) return message.channel.send("That's not a valid ID")
}
//Check if the user is not banned
try {
await message.guild.fetchBan(args[0])
} catch(e){
message.channel.send('This user is not banned.');
return;
}
let reason = args.slice(1).join(" ")
if(!reason) reason = "..."
if(!message.guild.me.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"])) return message.channel.send("I can't do that")
message.delete()
try {
message.guild.members.unban(bannedMember, {reason: reason})
message.channel.send(`${bannedMember.tag} was readmitted.`)
} catch(e) {
console.log(e.message)
}
}
我正在使用Zer0的建议if(!args[0]) return message.channel.send("Give me a valid ID");
检查在命令!unban 解决第一个错误之后是否键入了某些内容。
为了解决第二个错误并检查我们是否有一个有效的ID,我们只有第一个 try ... catch ,如果我们只有一个有效的ID,我们只能通过 try 因此:
如果 try 失败,则 catch 运行 if 来检查bannedMember
是否为 false 并返回消息错误。
答案 1 :(得分:0)
对于第一个错误,我只是检查是否给出了args [0]。我假设bot.users.fetch
返回一个错误对象,因此!!bannedMember
的评估结果为true。您正在使用Discord.js v12吗? v11和v12有所不同,因此我现在无法为您提供明确的答案。如果您想查看返回的内容,只需console.log被禁止的成员即可。
所以我的建议是:
if(!args[0]) return message.channel.send("please provide a valid ID");
另外,让下面的代码工作以捕获第二种错误类型也是一种完全有效的方法
try {
message.guild.members.unban(bannedMember, { reason });
message.channel.send(`${bannedMember.tag} ha sido readmitido.`);
} catch(e if e instanceof DiscordAPIError) {
message.channel.send("Are you sure this is a valid user ID?");
}