【发布时间】: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
也许我的编译器或运行时环境或程序员使用的所有其他奇怪的词有问题?
【问题讨论】:
-
我们可以提供您的 Discord.js 版本吗?
-
@Leau 当然,它是 13.4.0。我用stackoverflow.com/questions/68734660/… 找到它。
标签: javascript node.js visual-studio-code discord.js