0

So i am trying to make my discord bot add roles to someone that dm-ed it, and i cant seem to make it work Here is the code:

guild.members.fetch(message.author)
  .then(member => {
    member.roles.set(['roleID']);
});

This is the error that i get: (node:5692) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'set' of undefined at D:\Documents\GitHub\InfinitBotPrototip\index.js:48:24 at processTicksAndRejections (internal/process/task_queues.js:97:5) (node:5692) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise w hich was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhand led_rejections_mode). (rejection id: 1) (node:5692) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero e xit code.

  • Hi buddy, welcome to stack overflow. Can you provide more code? This sound like your ```member.role``` object is not being set – Rod Ramírez Nov 03 '20 at 23:45
  • `guild.members.fetch()` requires a snowflake, not a user object. Maybe you're thinking of `guild.member()`? – Lioness100 Nov 04 '20 at 00:39
  • @Lioness100 could you give an example of how to use the guild.members.fetch() function properly? I can't seem to make it work – AlexDontu Nov 04 '20 at 19:21
  • [The docs have a few examples](https://discord.js.org/#/docs/main/stable/class/GuildMemberManager?scrollTo=fetch). If you still can't get it to work, it might be because you haven't enabled the correct [intents](https://stackoverflow.com/questions/64559390/none-of-my-discord-js-guildmember-events-are-emitting-my-user-caches-are-basica) – Lioness100 Nov 04 '20 at 20:13
  • Hi @RodRamírez, i have updated the code and found out what the problem is, but i still can't solve it. Here is the updated code: ` guild.members.cache.forEach((item, i) => {if (item.id === 'memberID') { item.roles.set([memberRole]) .then(console.log) .catch(console.error); } ` I am 99.9999% sure this code is correct, however it still tells me that the bot does not have the permission to execute it: DiscordAPIError: Missing Permissions The bot has all the permissions it needs: it is administrator, and everything else is checked as well – AlexDontu Nov 04 '20 at 23:09
  • I am not trying to change the role of the server owner(me) but the role of another member. What the problem seems to be: When i go to server settings -> roles -> (my bot's role) -> View server as role and i try to change someone's role, it let's me click the + button, but the list of roles is empty. It simply doesnt show me anything to pick from. So i see that indeed, my bot cant change anyone's role because when it tries to, it doesnt get the list of roles. I have enabled the correct intents as @Lioness100 said and i also gave the bot all permissions possible. Why is this happening? – AlexDontu Nov 04 '20 at 23:09
  • Your bot's role has to be [higher than the role you're trying to assign on the role hierarchy](https://support.discord.com/hc/en-us/articles/214836687-Role-Management-101) – Lioness100 Nov 04 '20 at 23:56

0 Answers0