javascript - 不断给我未处理的承诺拒绝警告,我无法发现错误
问题描述
这是我的代码,顺便说一句,我有一个命令处理程序。
const Discord = require('discord.js');
const bot = new Discord.Client();
const token = 'hidden';
const sqlite = require('sqlite3').verbose();
const dayno = '0';
const PREFIX = '$';
const fs = require('fs');
bot.on('message', (message) => {
let userid = message.author.id;
if (message.author.bot || !message.content.startsWith(PREFIX)) return;
if(message.author.bot)return;
let db = new sqlite.Database('./database.db', sqlite.OPEN_READWRITE );
bot.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
bot.commands.set(command.name, command);
if (message.author.bot || !message.content.startsWith(PREFIX))
return;
let args = message.content.substring(PREFIX.length).split(" ");
switch(args[0]){
case 'help':
bot.commands.get('help').execute(message, args);
break;
case 'getreports':
if(message.member.roles.cache.find(r => r.name
=== "Developers")) return message.channel('You are not authorized to use this command.')
bot.commands.get('getreports').execute(message, args);
break;
case 'getreportsof':
if(message.member.roles.cache.find(r => r.name
=== "Developer")){
bot.commands.get('getreportsof').execute(message, args);
}else{
message.reply('You are not authorized to use this command.')
}
break;
};
};
});
bot.on('ready', () =>{
console.log('Duncan Online');
bot.user.setActivity('$help', {type: "LISTENING"}).catch(console.error);
let db = new sqlite.Database('./database.db', sqlite.OPEN_READWRITE | sqlite.OPEN_CREATE)
db.run('CREATE TABLE IF NOT EXISTS data(userid INTEGER NOT NULL, reports INTEGER NOT NULL)')
});
bot.login(token);
这是错误:
(node:12456) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Message
at RequestHandler.execute (C:\Users\HP_Omen\Desktop\Testing\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:12456) 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:12456) [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.
解决方案
我在您提供的代码中看不到任何会直接导致错误的内容。这很可能来自您的命令。
对于每个 switch 案例,使用如下内容:
bot.commands.get('help').execute(message, args).catch(console.error)
catch
应该可以帮助您找到错误。如果没有,请添加.catch(console.error)
到您在命令中调用异步函数(返回承诺的函数)的任何位置。
async
我个人发现使用函数 andawait
代替then
and更容易和可读catch
,因此您也可以将代码重构为如下所示:
// commands/help.js
module.exports = {
name: 'help',
async execute(message, args) {
// note how I use await here
await message.channel.send('some useful help message');
}
};
// your main file (probably index.js or something)
bot.on('message', async (message) => {
try {
// rest of code...
switch (args[0]) {
case 'help':
// use await here as well so that the errors get caught
await bot.commands.get('help').execute(message, args);
break;
// rest of commands
}
} catch (error) {
// log all errors
console.error(error)
}
});
有关未处理的承诺拒绝的更多信息,请参阅此答案。
推荐阅读
- javascript - 在通过 graalvm 从 Java 程序调用的 javascript 中使用 import
- python - 如何通过单击键盘上的按钮来删除 pygame 中的精灵?
- google-cloud-platform - GCP API 网关:将参数设置为 false 会返回“架构不允许的属性”错误
- python - 如何找到给定总和的子数组
- python - 将十六进制值附加到列表/将字符串列表转换为十六进制
- javascript - 使用 Javascript 加载多个图像
- mysql - 如何在sql中创建一个派生属性列,它是其他两个现有列的总和
- laravel - 是否可以格式化不包含从数据库中获取的 html 标签的文本(laravel vue)
- c# - 我需要在按钮 Click 上检索动态创建的“DatePicker”的值
- office-js - office-js 加载项 API addAsync 在添加 50 多个联系人时挂起 Outlook