开发者问题收集

未使用“messageReactionAdd”事件定义消息来执行操作

2020-08-29
1601

当“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.
3个回答

在此行:

message.member.send('You picked 🐍  '); 

message 未定义。请改用 reaction.message

Lioness100
2020-08-29

我确实添加了“reaction.message[...]”,但看不到消息。所以我使用了“nickname”方法,看看它是否有效。但我将昵称更改为他自己,而不是做出表情反应的用户。有什么帮助吗?

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);
      }
    }
  });
Foma Fomitch
2020-08-29

Message 未在您的代码中定义。而且最有可能的是,消息的作者是机器人,因此您会尝试向机器人发送消息。如您所见,您将获得一个 User 对象作为第二个参数。您可以使用 Usersend() 方法向用户发送消息。

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 🐍'); 
      }
    }
  });

注意:根据用户的隐私设置,您的代码可能会失败。使用 .catch 块来捕获任何错误并防止机器人崩溃。

Jakye
2020-08-29