【问题标题】:Discord.js: TypeError: Cannot read properties of undefined (reading 'add')Discord.js:TypeError:无法读取未定义的属性(读取“添加”)
【发布时间】:2022-01-27 07:44:17
【问题描述】:

我搜索了所有 Stack Overflow 和互联网,但没有发现任何有用的信息。我尝试了一切,它告诉我同样的事情。我正在制作一个 Discord 机器人并尝试向用户添加角色,但它一直告诉我我有一个错误。我曾尝试完全复制 YouTube 和博客上的教程,但它告诉我同样的错误。我被逼疯了;我已经尝试调试这个错误一个多星期了。这是我的代码:

const targetUser = message.mentions.users.first()
    if(!targetUser) {
        message.reply('ya gotta specify someone to give the role to ya idiot')
        return
    }
//gets the user's username. For example: someone#0001



    args.shift()
//takes user out of the arguments array


    const roleName = args[0]

//finds the name of the role. For example: Mod.


    const { guild } = message
//not sure what this does. I copied it from a tutorial on YouTube. I think it just makes saying guild the same thing as saying message
    const rool = guild.roles.cache.find((role)=>{
        return role.name === roleName
    })
    const role = rool.toString().replace("<", "").replace(">", "").replace("@", "").replace("&", "")
//this gets the role id: for example: 912345678901234567



    if(!role) {
    message.reply(`There is no role with the name ${roleName}. btw, you might want to put the person you're adding the role to first, *and then* the role name.`)
//checks if role exists
    }



    const member = targetUser.id
//gets user id. For example: 812345678901234567 
    try{
        member.role.add(role)//THIS IS THE ONE I HAVE TROUBLE WITH. THE ONE LINE! ALL THE OTHERS WORK PERFECTLY FINE. I HAVE NO IDEA WHAT'S WRONG.
    } catch(bad){
    message.channel.send("i have bad bad error: " + bad + ". Please try again later. Much, much later.")
    message.channel.send(`btw, member = ${member} and the role is ${role}`)
    message.channel.send(typeof role)
    message.channel.send(typeof member)

//the role is a string. the member is a string. I checked, and they are right. I've seen other people do the exact same thing as me and it DOESN'T WORK.
    }

如果需要,请随时索取更多代码。 每当我运行代码时,因为我有一个问题,它会告诉我:

i have bad bad error: TypeError: Cannot read properties of undefined (reading 'add'). Please try again later. Much, much later.
btw, member = 768522095071985746 and the role is 925077346170585138
string
string

也许我的编译器或运行时环境或程序员使用的所有其他奇怪的词有问题?

【问题讨论】:

标签: javascript node.js visual-studio-code discord.js


【解决方案1】:

您正在尝试访问 id 上的 role 属性,这是不可能的,因为它不是对象,访问 GuildMember 实例角色的正确方法是 &lt;GuildMember&gt;.roles, role 一个在&lt;User&gt; 上不存在,甚至更少。

const member = message.mentions.members.first();

const roleName = args[0];
const role = message.guild.roles.cache.find(role => role.name === roleName);

try {
  member.roles.add(role);
} catch(error) {
  message.channel.send('Error');
}

【讨论】:

  • 嘿@Leau。感谢您的回复,但我试过了,因为最后我有一个 try-catch,它告诉我变量中的用户和角色。机器人说它有一个错误,和以前一样。但是,最后的代码告诉我,角色变量现在是 而用户变量现在是我的不和谐用户名,所以它会 ping 我。机器人还说它们都是对象,这就是你的建议,对吧?无论如何,它没有工作。随意提出其他建议或要求更多代码。谢谢!
  • 你是说member.roles返回未定义?
  • 是的。它是未定义的。
  • 你在消息中提到过某人吗?
  • 哦,是的,当然,将message.mentions.users.first() 替换为message.mentions.members.first(),用户没有角色,但成员有!
猜你喜欢
  • 2020-09-04
  • 2020-09-03
  • 2020-12-14
  • 2021-11-02
  • 2020-02-16
  • 2021-10-05
  • 2021-07-24
  • 2018-02-06
  • 1970-01-01
相关资源
最近更新 更多