所以我想在机器人程序的消息中添加一个表情符号反应。但我知道该用什么代码。
我只知道对命令消息作出反应的代码。
else if(command === "guessage"){
message.channel.send({ embed: {
color: 16758465,
title: "Are you...",
description: Math.floor((Math.random() * 20) + 11) + " " + "years old?"
}
})
message.react('👍').then(() => message.react('👎'));
const filter = (reaction, user) => {
return ['👍', '👎'].includes(reaction.emoji.name) && user.id === message.author.id;
};
message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '👍') {
message.reply('you reacted with a thumbs up.');
} else {
message.reply('you reacted with a thumbs down.');
}
})
.catch(collected => {
message.reply('you reacted with neither a thumbs up, nor a thumbs down.');
});
}
处理每个Message#reply()
的承诺
使用回调的示例:
message.reply('you reacted with a thumbs up.').then(botsMessage => botsMessage.react('EMOJI-HERE'));
使用Async/Await的示例(建议用于维持反应顺序(:
// Inside an async function
const botsMessage = await message.reply('you reacted with a thumbs up.');
await botMessage.react('EMOJI-1');
await botMessage.react('EMOJI-2');
await botMessage.react('EMOJI-3');
理解承诺-Discord.JS
您需要等待消息的发送并使用它的消息对象。
例如:
else if (command === "guessage") {
(async () => {
let bmsg = await message.channel.send({
embed: {
color: 16758465,
title: "Are you...",
description: Math.floor((Math.random() * 20) + 11) + " " + "years old?"
}
})
await bmsg.react('👍');
await bmsg.react('👎');
const filter = (reaction, user) => {
return ['👍', '👎'].includes(reaction.emoji.name) && user.id === message.author.id;
};
bmsg.awaitReactions(filter, {
max: 1,
time: 60000,
errors: ['time']
})
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '👍') {
message.reply('you reacted with a thumbs up.');
} else {
message.reply('you reacted with a thumbs down.');
}
})
.catch(collected => {
message.reply('you reacted with neither a thumbs up, nor a thumbs down.');
});
})();
}
我正在使用异步IIFE来允许使用wait。还有其他地方应该使用等待,但我将由您决定。