catch 不工作 - promise 被拒绝,原因是“#<Collection>”
catch not working - The promise rejected with the reason "#<Collection>"
我正在尝试编写一个不和谐的机器人,现在它是一个结婚的命令。
除一件事外,一切正常。在 awaitReactions
函数中,我有 10 秒的时间,在这段时间之后我得到这个错误:
node:internal/process/promises:246
triggerUncaughtException(err, true /* fromPromise */);
[UnhandledPromiseRejection: This error originated either by throwing inside of a
n async function without a catch block, or by rejecting a promise which was not
handled with .catch(). The promise rejected with the reason "#".] {
code: 'ERR_UNHANDLED_REJECTION'
}
我不明白为什么会这样,我在函数末尾有 .catch()
,理论上一切都应该正常工作。
为什么 .catch()
对我的情况不起作用?可能是什么问题?
const { Command } = require('discord.js-commando');
const db = require("quick.db");
module.exports = class MarryCommand extends Command {
constructor(client) {
super(client, {
name: 'marry',
memberName: 'marry',
group: 'test',
description: 'Marry the mentioned user',
guildOnly: true,
args: [
{
key: 'userToMarry',
prompt: 'Please select the member you wish to marry.',
type: 'member'
}
]
});
}
run(message, { userToMarry }) {
const exists = db.get(`${message.author.id}.user`);
const married = db.get(`${userToMarry.id}.user`);
if (!userToMarry) {
return message.channel.send('Please try again with a valid user.')}
if (exists == message.author.id) {
return message.channel.send('You are already married!')}
if (married == userToMarry.id) {
return message.channel.send('This user is already married!')}
if (userToMarry.id == message.author.id) {
return message.channel.send('You cannot marry yourself!');
}
if (exists != message.author.id && married != userToMarry.id) {
message.channel.send(`**Important announcement!**
${message.author} makes a marriage proposal ${userToMarry}
Are you ready to get married?`).then(message => {
message.react('').then(() => message.react(''));
message.awaitReactions((reaction, user) => user.id == userToMarry.id && (reaction.emoji.name == '' || reaction.emoji.name == ''),
{ max: 1, time: 10000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '') {
return message.channel.send('I think **no**...')}
if (reaction.emoji.name === '') {
db.set(message.author.id, { user: message.author.id, partner: userToMarry.id });
db.set(userToMarry.id, { user: userToMarry.id, partner: message.author.id });
message.channel.send(`${message.author} and ${userToMarry} now married!!`)
.catch(() => {
message.reply('No reaction after 10 seconds, operation canceled');
});
}
});
});
}}};
这里的错误是每个 then 链都应该有一个 catch 块。你错过了两个 catch 块。解决方案是将 catch 块添加到所有 then 链,或者您可以将所有 then 链连接成一个大链,最后使用一个 catch 块
方法一
const { Command } = require("discord.js-commando");
const db = require("quick.db");
module.exports = class MarryCommand extends Command {
constructor(client) {
super(client, {
name: "marry",
memberName: "marry",
group: "test",
description: "Marry the mentioned user",
guildOnly: true,
args: [
{
key: "userToMarry",
prompt: "Please select the member you wish to marry.",
type: "member",
},
],
});
}
run(message, { userToMarry }) {
const exists = db.get(`${message.author.id}.user`);
const married = db.get(`${userToMarry.id}.user`);
if (!userToMarry) {
return message.channel.send("Please try again with a valid user.");
}
if (exists == message.author.id) {
return message.channel.send("You are already married!");
}
if (married == userToMarry.id) {
return message.channel.send("This user is already married!");
}
if (userToMarry.id == message.author.id) {
return message.channel.send("You cannot marry yourself!");
}
if (exists != message.author.id && married != userToMarry.id) {
message.channel
.send(
`**Important announcement!**
${message.author} makes a marriage proposal ${userToMarry}
Are you ready to get married?`
)
.then((message) => {
message.react("")
.then(() => message.react(""))
.catch(()=>{
//code
});
message.awaitReactions((reaction, user) =>
user.id == userToMarry.id && (reaction.emoji.name == "" || reaction.emoji.name == ""),
{ max: 1, time: 10000, errors: ["time"] }
).then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === "") {
return message.channel.send("I think **no**...");
}
if (reaction.emoji.name === "") {
db.set(message.author.id, {
user: message.author.id,
partner: userToMarry.id,
});
db.set(userToMarry.id, {
user: userToMarry.id,
partner: message.author.id,
});
message.channel
.send(`${message.author} and ${userToMarry} now married!!`)
.catch(() => {
message.reply(
"No reaction after 10 seconds, operation canceled"
);
});
}
}).catch(()=>{
//code
});
}).catch(()=>{
//code
});
}
}
};
方法二
const { Command } = require("discord.js-commando");
const db = require("quick.db");
module.exports = class MarryCommand extends Command {
constructor(client) {
super(client, {
name: "marry",
memberName: "marry",
group: "test",
description: "Marry the mentioned user",
guildOnly: true,
args: [
{
key: "userToMarry",
prompt: "Please select the member you wish to marry.",
type: "member",
},
],
});
}
run(message, { userToMarry }) {
const exists = db.get(`${message.author.id}.user`);
const married = db.get(`${userToMarry.id}.user`);
if (!userToMarry) {
return message.channel.send("Please try again with a valid user.");
}
if (exists == message.author.id) {
return message.channel.send("You are already married!");
}
if (married == userToMarry.id) {
return message.channel.send("This user is already married!");
}
if (userToMarry.id == message.author.id) {
return message.channel.send("You cannot marry yourself!");
}
if (exists != message.author.id && married != userToMarry.id) {
message.channel
.send(
`**Important announcement!**
${message.author} makes a marriage proposal ${userToMarry}
Are you ready to get married?`
)
.then((message) => {
message.react("")
.then(() => message.react(""))
.catch(()=>{
//code
});
return message.awaitReactions((reaction, user) =>
user.id == userToMarry.id && (reaction.emoji.name == "" || reaction.emoji.name == ""),
{ max: 1, time: 10000, errors: ["time"] }
)
})
.then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === "") {
return message.channel.send("I think **no**...");
}
if (reaction.emoji.name === "") {
db.set(message.author.id, {
user: message.author.id,
partner: userToMarry.id,
});
db.set(userToMarry.id, {
user: userToMarry.id,
partner: message.author.id,
});
return message.channel
.send(`${message.author} and ${userToMarry} now married!!`)
}
})
.catch(()=>{
message.reply(
"No reaction after 10 seconds, operation canceled"
);
});
}
}
};
我正在尝试编写一个不和谐的机器人,现在它是一个结婚的命令。
除一件事外,一切正常。在 awaitReactions
函数中,我有 10 秒的时间,在这段时间之后我得到这个错误:
node:internal/process/promises:246 triggerUncaughtException(err, true /* fromPromise */); [UnhandledPromiseRejection: This error originated either by throwing inside of a n async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "#".] { code: 'ERR_UNHANDLED_REJECTION' }
我不明白为什么会这样,我在函数末尾有 .catch()
,理论上一切都应该正常工作。
为什么 .catch()
对我的情况不起作用?可能是什么问题?
const { Command } = require('discord.js-commando');
const db = require("quick.db");
module.exports = class MarryCommand extends Command {
constructor(client) {
super(client, {
name: 'marry',
memberName: 'marry',
group: 'test',
description: 'Marry the mentioned user',
guildOnly: true,
args: [
{
key: 'userToMarry',
prompt: 'Please select the member you wish to marry.',
type: 'member'
}
]
});
}
run(message, { userToMarry }) {
const exists = db.get(`${message.author.id}.user`);
const married = db.get(`${userToMarry.id}.user`);
if (!userToMarry) {
return message.channel.send('Please try again with a valid user.')}
if (exists == message.author.id) {
return message.channel.send('You are already married!')}
if (married == userToMarry.id) {
return message.channel.send('This user is already married!')}
if (userToMarry.id == message.author.id) {
return message.channel.send('You cannot marry yourself!');
}
if (exists != message.author.id && married != userToMarry.id) {
message.channel.send(`**Important announcement!**
${message.author} makes a marriage proposal ${userToMarry}
Are you ready to get married?`).then(message => {
message.react('').then(() => message.react(''));
message.awaitReactions((reaction, user) => user.id == userToMarry.id && (reaction.emoji.name == '' || reaction.emoji.name == ''),
{ max: 1, time: 10000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '') {
return message.channel.send('I think **no**...')}
if (reaction.emoji.name === '') {
db.set(message.author.id, { user: message.author.id, partner: userToMarry.id });
db.set(userToMarry.id, { user: userToMarry.id, partner: message.author.id });
message.channel.send(`${message.author} and ${userToMarry} now married!!`)
.catch(() => {
message.reply('No reaction after 10 seconds, operation canceled');
});
}
});
});
}}};
这里的错误是每个 then 链都应该有一个 catch 块。你错过了两个 catch 块。解决方案是将 catch 块添加到所有 then 链,或者您可以将所有 then 链连接成一个大链,最后使用一个 catch 块
方法一
const { Command } = require("discord.js-commando");
const db = require("quick.db");
module.exports = class MarryCommand extends Command {
constructor(client) {
super(client, {
name: "marry",
memberName: "marry",
group: "test",
description: "Marry the mentioned user",
guildOnly: true,
args: [
{
key: "userToMarry",
prompt: "Please select the member you wish to marry.",
type: "member",
},
],
});
}
run(message, { userToMarry }) {
const exists = db.get(`${message.author.id}.user`);
const married = db.get(`${userToMarry.id}.user`);
if (!userToMarry) {
return message.channel.send("Please try again with a valid user.");
}
if (exists == message.author.id) {
return message.channel.send("You are already married!");
}
if (married == userToMarry.id) {
return message.channel.send("This user is already married!");
}
if (userToMarry.id == message.author.id) {
return message.channel.send("You cannot marry yourself!");
}
if (exists != message.author.id && married != userToMarry.id) {
message.channel
.send(
`**Important announcement!**
${message.author} makes a marriage proposal ${userToMarry}
Are you ready to get married?`
)
.then((message) => {
message.react("")
.then(() => message.react(""))
.catch(()=>{
//code
});
message.awaitReactions((reaction, user) =>
user.id == userToMarry.id && (reaction.emoji.name == "" || reaction.emoji.name == ""),
{ max: 1, time: 10000, errors: ["time"] }
).then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === "") {
return message.channel.send("I think **no**...");
}
if (reaction.emoji.name === "") {
db.set(message.author.id, {
user: message.author.id,
partner: userToMarry.id,
});
db.set(userToMarry.id, {
user: userToMarry.id,
partner: message.author.id,
});
message.channel
.send(`${message.author} and ${userToMarry} now married!!`)
.catch(() => {
message.reply(
"No reaction after 10 seconds, operation canceled"
);
});
}
}).catch(()=>{
//code
});
}).catch(()=>{
//code
});
}
}
};
方法二
const { Command } = require("discord.js-commando");
const db = require("quick.db");
module.exports = class MarryCommand extends Command {
constructor(client) {
super(client, {
name: "marry",
memberName: "marry",
group: "test",
description: "Marry the mentioned user",
guildOnly: true,
args: [
{
key: "userToMarry",
prompt: "Please select the member you wish to marry.",
type: "member",
},
],
});
}
run(message, { userToMarry }) {
const exists = db.get(`${message.author.id}.user`);
const married = db.get(`${userToMarry.id}.user`);
if (!userToMarry) {
return message.channel.send("Please try again with a valid user.");
}
if (exists == message.author.id) {
return message.channel.send("You are already married!");
}
if (married == userToMarry.id) {
return message.channel.send("This user is already married!");
}
if (userToMarry.id == message.author.id) {
return message.channel.send("You cannot marry yourself!");
}
if (exists != message.author.id && married != userToMarry.id) {
message.channel
.send(
`**Important announcement!**
${message.author} makes a marriage proposal ${userToMarry}
Are you ready to get married?`
)
.then((message) => {
message.react("")
.then(() => message.react(""))
.catch(()=>{
//code
});
return message.awaitReactions((reaction, user) =>
user.id == userToMarry.id && (reaction.emoji.name == "" || reaction.emoji.name == ""),
{ max: 1, time: 10000, errors: ["time"] }
)
})
.then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === "") {
return message.channel.send("I think **no**...");
}
if (reaction.emoji.name === "") {
db.set(message.author.id, {
user: message.author.id,
partner: userToMarry.id,
});
db.set(userToMarry.id, {
user: userToMarry.id,
partner: message.author.id,
});
return message.channel
.send(`${message.author} and ${userToMarry} now married!!`)
}
})
.catch(()=>{
message.reply(
"No reaction after 10 seconds, operation canceled"
);
});
}
}
};