【问题标题】:V12 Discord.js retrieve user.id from a message reactionsV12 Discord.js 从消息反应中检索 user.id
【发布时间】:2021-11-13 05:15:28
【问题描述】:

我正在努力在 discord.js 的 V12 中检索 user.id 和 user.name 以获取他们也做出反应的消息。 这个想法是一条消息被固定,用户每天在 1-7 之间选择,它会在他们的总数中增加一个点。如果他们取消选择任何反应,它也会从他们的总数中减去。但我还没有写出来。 目前它因 TypeError 失败:尝试获取 user.id 时无法读取 const Character 上未定义的属性 'guild'

config.json

{
"prefix":"!",
"channel": "xxx",
"channelID": "xxx",
}

SQLite

client.getdayCount = DB.prepare("SELECT dayCount from Table WHERE id = ?;");
client.setdayCount = DB.prepare("INSERT OR REPLACE INTO Table (id, dayCount, guild) VALUES (@id, @dayCount @guild);");

主代码

client.on('messageReactionAdd', addRole);
client.on('messageReactionRemove', removeRole);

async function addRole(message, reaction, user) {

if (message.partial) {
   try {
       await message.fetch();
   } catch (err) {
  console.error('Error fetching message', err);
  return;
   }
}


client.channels.cache.get(config.channelID).messages.fetch({
limit: 1
}).then(messages => {
const lastMessage = messages.first()
const character = reaction.message.guild.member(user.id);
//Filter the reaction
const addition = +1;
if (lastMessage.reactions.cache.emojis === "1️⃣" || "2️⃣" || "3️⃣" || "4️⃣" || "5️⃣" || "6️⃣" || "7️⃣") {
  // Define the emoji user add
   //const character = lastMessage.guild.member(user.id);
  //reaction.message.guild.member(user.id);
  if (character) //if author has sent before
  {
      character.setdayCount += addition;
      client.setdayCount.run(character);
      return;
    
  } else //else add new author to collection
  {
    character = {
      id: user.id,
      setdayCount: addition,
      guild: reaction.guild.id,
    }

  }
} else {

};
}).catch(err => {
console.error(err)
})
}

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    您在函数中使用了不正确的参数,在 discord js v12 中 messageReactionAdd 只有两个参数,messageReactionuser。因此,您称为reaction 的参数实际上是一个User 对象,它没有名为message 的属性并导致您的错误。

    另外

    if (lastMessage.reactions.cache.emojis === "1️⃣" || "2️⃣" || "3️⃣" || "4️⃣" || "5️⃣" || "6️⃣" || "7️⃣") {
    

    永远都是真的,我想你打算做的是

    if (["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣"].includes(lastMessage.reactions.cache.emojis)) {
    

    【讨论】:

    • 谢谢!你是对的。我的错!
    猜你喜欢
    • 2021-01-03
    • 2020-11-30
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 2021-06-21
    • 2022-01-21
    • 1970-01-01
    • 2020-10-25
    相关资源
    最近更新 更多