当这个成员对我的机器人发出的消息使用某种表情符号做出反应时,我希望我的机器人执行对“ message.member”的操作。但是,我遇到一个错误“消息未定义”,对此我无能为力。
我认为我需要使用“ messageReactionAdd”,因为我需要了解服务器上的每个响应,检查它们是否应用于我的机器人消息,然后将操作返回给执行响应的用户。这必须涉及我的机器人发出的所有消息,甚至是旧消息。我要使用的所有方法都是 GuildMember的方法:https://discord.js.org/#/docs/main/stable/class/GuildMember。
client.on('messageReactionAdd', (reaction, user) => {
console.log(`mess id : ${reaction.message.id} emoji user = ${reaction.message.member.id}`);
if ((reaction.message.author.id == client.user.id) && (user.id != client.user.id)) {
if (reaction.emoji.name == '?') {
message.member.send('You picked ? ');
}
}
});
UnhandledPromiseRejectionWarning: ReferenceError: message is not defined
at Client.client.on (/home/me/botdiscord/index.js:84:7)
at Client.emit (events.js:193:13)
at MessageReactionAdd.handle (/home/me/botdiscord/node_modules/discord.js/src/client/actions/MessageReactionAdd.js:46:17)
at Object.module.exports [as MESSAGE_REACTION_ADD] (/home/me/botdiscord/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_REACTION_ADD.js:4:37)
at WebSocketManager.handlePacket (/home/me/botdiscord/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/home/me/botdiscord/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/home/me/botdiscord/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/home/me/botdiscord/node_modules/ws/lib/event-target.js:125:16)
at WebSocket.emit (events.js:193:13)
at Receiver.receiverOnMessage (/home/me/botdiscord/node_modules/ws/lib/websocket.js:797:20)
(node:7306) 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(). (rejection id: 1)
(node:7306) [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.
答案 0 :(得分:0)
答案 1 :(得分:0)
Message
未在您的代码中定义。消息的作者很可能是机器人,因此您将尝试向机器人发送消息。如您所见,您将获得一个User
对象作为第二个参数。您可以使用send()
的{{1}}方法向用户发送消息。
User
注意:根据用户的隐私设置,您的代码可能会失败。使用client.on('messageReactionAdd', (reaction, user) => {
console.log(`mess id : ${reaction.message.id} emoji user = ${reaction.message.member.id}`);
if ((reaction.message.author.id == client.user.id) && (user.id != client.user.id)) {
if (reaction.emoji.name == '?') {
user.send('You picked ?');
}
}
});
块来捕获所有错误并防止漫游器崩溃。
答案 2 :(得分:0)
我确实添加了“ reaction.message [...]”,但看不到该消息。因此,我使用“ .nickname”方法来查看它是否有效。但是我的昵称是他自己,而不是对进行emohi反应的用户。请帮忙吗?
client.on('messageReactionAdd', (reaction, user) => {
console.log(`mess id : ${reaction.message.id} emoji user = ${reaction.message.member.id}`);
if ((reaction.message.author.id == client.user.id) && (user.id != client.user.id)) {
if (reaction.emoji.name == '?') {
reaction.message.member.setNickname('?')
.then(console.log)
.catch(console.error);
}
}
});