简体   繁体   English

类型错误:无法读取未定义的属性“添加”,Discord.JS

[英]TypeError: Cannot read property 'add' of undefined, Discord.JS

client.on('message', message => {
  if (message.content.startsWith(prefix + 'sanitar')) {
    if (message.member.hasPermission('ADMINISTRATOR')) {
      const user = message.mentions.users.first();
      if (user != undefined) {
        user.GuildMemberRoleManager.add(635602511964667907);
        message.channel.send(`<:mebel:632608605635608578> | Пользователь ${user} был отправлен в дурку ебать!`);
      } else {
        message.channel.send('<:mebel:632608605635608578> | Пингани кого-то, идиот.');
        message.channel.send('<:debil:691052950998482955>');
      }
    } else {
      message.channel.send('<:mebel:632608605635608578> | У вас недостаточно прав для этого действия!');
    }
  }
});

I'm trying to create an add-role command for my discord.js bot, but when it gets to user.GuildMemberRoleManager.add(635602511964667907);我正在尝试为我的 discord.js 机器人创建一个添加角色命令,但是当它到达user.GuildMemberRoleManager.add(635602511964667907);

i get:我得到:

TypeError: Cannot read property 'add' of undefined. TypeError:无法读取未定义的属性“添加”。

I also tried to use user.Addrole function, but i get:我也尝试使用user.Addrole function,但我得到:

user.Addrole is not a function. user.Addrole 不是 function。

Maybe addrole is outdated now, idk.也许 addrole 现在已经过时了,idk。 I am very tired of this, so can somebody help me to fix it pls...我对此感到非常厌倦,所以有人可以帮我解决它吗...

You need to use roles not GuildMemberRoleManager since roles returns GuildMemberRoleManager of the GuildMember .您需要使用roles而不是GuildMemberRoleManager ,因为roles返回GuildMemberRoleManagerGuildMember

Also User does not have roles only GuildMember does so you have to replace const user = message.mentions.users.first();此外, User没有roles ,只有GuildMember有,所以你必须替换const user = message.mentions.users.first(); with const user = message.mentions.members.first(); with const user = message.mentions.members.first();

You also need to pass the role ID as a string, not a number.您还需要将角色 ID 作为字符串而不是数字传递。

user.roles.add('635602511964667907');

Also, you are correct, addRole() is not a function and has deprecated since discord.js v12.另外,您是对的, addRole()不是 function 并且自 discord.js v12 以来已弃用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:无法读取未定义 Discord.js javascript 的属性“添加” - TypeError: Cannot read property 'add' of undefined Discord.js javascript (Discord.js)TypeError:无法读取未定义的属性“添加” - (Discord.js) TypeError: Cannot read property 'add' of undefined TypeError:无法读取未定义的discord.js的属性“ add” - TypeError: Cannot read property 'add' of undefined discord.js 类型错误:无法读取未定义的属性 &#39;has&#39; // Discord.js - TypeError: Cannot read property 'has' of undefined // Discord.js 遇到 TypeError:无法读取 Discord.JS 中未定义的属性“0” - Encountering TypeError: Cannot read property '0' of undefined in Discord.JS Discord.JS UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“startsWith” - Discord.JS UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'startsWith' of undefined TypeError:无法读取未定义的属性“数据”| discord.js - TypeError: Cannot read property 'data' of undefined | discord.js 类型错误:无法读取未定义的属性“角色”|| Discord.js - TypeError: Cannot read property 'roles' of undefined || Discord.js Discord.js:类型错误:无法读取未定义的属性“删除” - Discord.js : TypeError: Cannot read property 'remove' of undefined Discord.js“类型错误:无法读取未定义的属性‘有’” - Discord.js "TypeError: Cannot read property 'has' of undefined"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM