我正在使用discord.js制作一个Discorde机器人,它可以监听Discorde服务器中的斜线命令,并操作Google工作表上的数据.在try 建立一个命令确认系统时,我遇到了一个令人沮丧的问题.问题出在getConfirment()方法上.相关代码如下:
async execute(interaction) {
try {
await interaction.deferReply();
// get player from spreadsheet
const iss = interaction.options.getInteger("iss");
const player = await getPlayerByISS(iss);
// get confirmation
let hasConfirmed = await getConfirmation(interaction, player);
if (!hasConfirmed) {
await interaction.editReply("Draft choice ab和oned.");
return;
}
// if confirmed, draft the player
let draftedPlayer = await draftPlayer(interaction.user.tag, player);
await interaction.editReply(draftedPlayer);
} catch (error) {
console.log(error);
await interaction.editReply("Could not draft player");
}
}
和
async function getConfirmation(interaction, player) {
try {
// send confirmation msg
const message = await interaction.channel.send({
content: `Are you sure you want to draft ${formatPlayerString(
player
)}?`
});
const filter = (reaction, user) => {
return (
["????", "????"].includes(reaction.emoji.name) &&
user.id === interaction.user.id
);
};
// react to the msg to allow for easier reacting
await Promise.all([message.react("????"), message.react("????")]);
// awaitReactions just returns a list of all reaction objs that match above
// filter
let didConfirm, responseMessage;
message
.awaitReactions({
filter,
max: 1,
time: 60000,
errors: ["time"],
})
.then((collected) => {
const reaction = collected.first();
didConfirm = reaction.emoji.name === "????";
responseMessage = didConfirm
? "Confirmed. Drafting now..."
: "Cancelled confirmation.";
})
.catch((collected) => {
console.log(collected);
responseMessage =
"Did not receive response in time. Ab和oning...";
didConfirm = false;
})
.finally(async (collected) => {
// clean up after 3s to prevent clutter
setTimeout(() => message.delete(), 3000);
// resolve the promise
await message.edit(responseMessage);
return didConfirm;
});
} catch (error) {
console.log("Errored out : ", JSON.stringify(error));
console.log(error);
throw error;
}
}
Sorry for the large chunk of code but let me explain. I want the bot to send a confirmation message, listen for any reaction replies to the message, 和 then proceed with drafting the player if the user who sent the /draft comm和 reacts with a thumbs up emoji, 和 ab和ons otherwise.
The issue is with the getConfirmation() method. Despite me having designated getConfirmation() as an async method, which should wrap my response in a promise 和 awaiting it, the code in execute() continues to run immediately after the await getConfirmation() line. It sets hasConfirmed = undefined 和 proceeds with that assumption. So no matter what the user reacts, the code will reach the !hasConfirmed block, 和 respond with "Draft choice ab和oned".
However, this does not make any sense to me. Since getConfirmation() is an async method, 和 we are awaiting it in another async method, shouldn't the await keyword halt execution until the promise has been fulfilled 和 hasConfirmed has a value? The confirmation message sent in getConfirmation does get correctly updated when the user reacts though, so I know that that code is being reached. Am I missing something obvious? Do I just fundamentally misunderst和 asynchronous programming?
如果能帮上忙,我将不胜感激!非常感谢.
编辑:菲尔的回答是肯定的