Discord.js机器人-在我的情况下如何处理UnhandledPromiseRejectionWarning [英] Discord.js Bot - How to handle UnhandledPromiseRejectionWarning in my case
问题描述
即使代码起作用(部分地有时会中断),我仍会收到UnhandledPromiseRejectionWarning。
那么,如何应对承诺拒绝呢?我昨天开始使用node.js,这真是令人困惑。
I keep getting UnhandledPromiseRejectionWarning even though the code functions (partially, sometimes it breaks). So, how to catch the promise rejection thingy? I started using node.js yesterday and this is confusing as hell.
bot.on('message', async message => {
let inGame = message.guild.roles.cache.find(role => role.name == "In-Game");
let memberArray = message.guild.members.cache.filter(member => member.roles.cache.find(role => role == inGame)).array();
if (message.content.startsWith(".mute2") && message.channel.name == 'admin-only') {
memberArray.forEach(member => member.edit({mute: true}));
}
});
推荐答案
UnhandledPromiseRejectionWarning是对您遇到的其他错误的包装尚未使用 try / catch
处理的基于代码的代码。这是一个示例(不是代码中的错误)
UnhandledPromiseRejectionWarning is a wrapper for other errors you get in promise-based code that you haven't handled with try/catch
or something. Here's an example (not an error from your code)
(node:16189) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Access
at RequestHandler.execute (/home/*/NODESTUFF/node_modules/discord.js/src/rest/RequestHandler.js:170:25)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:16189) 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:16189) [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.
注意,在UnhandledPromiseRejectionWarning之后,还有另一个错误:DiscordAPIError:缺少访问权限。
Notice that right after UnhandledPromiseRejectionWarning, there's another error: DiscordAPIError: Missing Access.
然后还有另一行您看不到何时在控制台中进行了处理,但是以SO代码格式非常可读:
And then there's another line you can't see when it's crunched up in your console but is very readable in the SO code formatting:
UnhandledPromiseRejectionWarning:未处理的承诺拒绝。该错误是由于在没有catch块的情况下抛出异步函数而引起的,或者是由于拒绝了未使用.catch()处理的诺言而引起的。
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().
如果您遇到类似的API错误,则您的漫游器缺少某些内容的权限(我怀疑)。不幸的是,代码没有告诉您缺少的 权限。我建议您为要尝试执行的每件事添加权限。
If there's an APIerror like that for you, your bot is missing permissions for something (which I suspect). Unfortunately, the code doesn't tell you what permission you're missing. I recommend you add perms for each thing you're trying to do.
您的代码对我来说还不错,因为我已经给我的机器人授予了使用户静音,查看角色等的权限。真的很惊讶您不需要等待 member.edit()
。如果有人知道为什么,请发表评论。
Your code ran fine for me because I already gave my bot permissions to mute users, see roles, etc. I was actually surprised you didn't need to await member.edit()
. If anyone knows why, please leave a comment.
这篇关于Discord.js机器人-在我的情况下如何处理UnhandledPromiseRejectionWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!