运行此代码时不断出现此错误

时间:2020-06-14 02:35:38

标签: discord.js roles

我正在尝试建立验证渠道,以便用户使用反应来验证自己。 我尝试从验证渠道获取消息并尝试执行以下代码:

 const mReaction = new Discord.MessageReaction();
 const message = mReaction.message;
 const verified = 'verifiedID';
 const unverified = 'unverifiedID';
 if(message.reactions.cache.find(r => r.emoji === '✅') || message.channel.id == 'channelID'){
     message.member.roles.add(verified);
     message.member.roles.remove(unverified);
 }

运行代码时,我不断得到:

TypeError: Cannot read property 'me' of undefined
    at new MessageReaction (D:\Users\Rastik\Desktop\discord bot\node_modules\discord.js\src\structures\MessageReaction.js:35:20)
    at Object.<anonymous> (D:\Users\Rastik\Desktop\discord bot\main.js:98:20)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

因此,我想请大家帮助我,如果有人愿意帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

错误是您凭空创建MessageReaction

const mReaction = new Discord.MessageReaction();

如果您查看文档,它表明您将需要客户端,数据和消息。

https://discord.js.org/#/docs/main/stable/class/MessageReaction

const message = mReaction.message如何知道反应是指哪条消息?你什么都没定义

所有这些代码毫无意义。

如果您要执行验证消息响应系统,则需要侦听messageReactionAdd事件,但是由于该事件仅侦听缓存的消息,因此您将需要使用raw事件或局部事件

我将展示部分方法。

主文件:

const client = new Client({ partials: ["REACTION", "MESSAGE"]});

然后您将需要以某种方式处理messageReactionAdd事件,

client.on("messageReactionAdd", async (reaction, user) => {
  if(reaction.partial) await reaction.fetch();
  //you should have one message with the checkmark emoji, that users need to press
  //to join, insert the msg id here
  if(reaction.message.id !== "id") return;


  //now you validate the user, if they have the role already return if not add.
});