使用 Quick.db (Discord.js) 的自动角色问题

时间:2021-03-25 01:34:07

标签: javascript discord.js quick.db

我正在尝试使用 quick.db 制作自动角色代码,但它返回错误:UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes 我的“setautorole”命令:

      const role = message.mentions.roles.first() || message.guild.roles.cache.get(args[0])
      if(!role) return message.channel.send('I couldnt find the role')
db.set(`autorole`, role)
message.channel.send('The process worked fine!')

这是机器人的索引:

client.on("guildMemberAdd", (member) => {
  let few = db.get(`autorole_${member.guild.id}`)
  if(few === null) return;
  member.roles.add(few)
})

好吧,我不知道如何解决这个错误,我需要一点帮助

2 个答案:

答案 0 :(得分:0)

最好将角色 ID 保存在数据库中

顺便说一句,你做错了。应该是这样
setautorole.js

const role = message.mentions.roles.first() || message.guild.roles.cache.get(args[0]);
if(!role){
    return( message.channel.send('I couldnt find the role') );
}
db.set(`autorole_${message.guild.id}`, role.id);
message.channel.send('The process worked fine!');

index.js

client.on("guildMemberAdd", (member) => {
  let roleID = db.get(`autorole_${member.guild.id}`)
  if(!roleID) return;
  role = member.guild.roles.find(roleID);
  if(!role){
     console.log("That role dosen't exist");
     return (false);
  }
  member.roles.add(role)
})

答案 1 :(得分:0)

感谢 Akio 的想法,但是,我做了类似的事情:

client.on("guildMemberAdd", (member) => {
  let roleID = db.get(`autorole_${member.guild.id}`)
  if(!roleID) return;
 let role = member.guild.roles.cache.find(r => r.id === roleID);
  if(!role){
     console.log("That role dosen't exist");
     return (false);
  }
  member.roles.add(role)
})

成功了,感谢您的帮助:)