所以用户加入服务器并且必须 dm 机器人,然后机器人应该给他一个特定的角色(成员)。这是我的代码的一部分:
client.on('message', msg => {
if (msg.channel.type == "dm") {
user = msg.author.id;
if (msg.content.startsWith("-bind")) {
for (var i = 0; i < keys.length; i++) {
if (msg.content.includes(keys[i]) && msg.content.includes(keys_perma[i])) {
keys[i] = user;
let server = client.guilds.cache.get('792890734139015218')
var memberRole = server.roles.cache.find(role => role.name === "Member")
let member = server.members.cache.get(msg.author.id)
member.roles.add(memberRole)
temp = 1;
msg.author.send("Key bound!");
break;
}
但是 member.roles.add(memberRole) 给了我错误:无法读取未定义的属性“角色”
我希望任何人都可以帮助我:)
答案 0 :(得分:0)
member
返回 undefined,最好获取成员而不是依赖缓存。在本例中,我将使用 Promise#then()
来获取成员。
let member = server.members.fetch(msg.author.id).then(m => {
m.roles.add(memberRole);
});