简体   繁体   中英

Problem with removing a role from a specific user using discord.js

So, I've been trying to finish my discord bot and one of the last things I wanted to add was to make it so when someone reacted to a certain message with a ✅, it would send them a DM and remove a specific role called "Newcomer". I fetched the message id and got the DM part working (using user.id to get the user's id), but, for some reason, I am not being able to do the same with removing a role.

I tried to use the "client.channels.cache.get()", but that's when I realized I'm working with async and the returning value is a Promise and not the actual guildmember I'm looking for. To solve that, I created an async function and passed the function I mentioned before with await, but it still returns the same god damn error:

node:events:491
      throw er; // Unhandled 'error' event
      ^

TypeError: Cannot read properties of undefined (reading 'remove')
    at ReactionCollector.<anonymous> (/home/runner/ATC24-Practice-Hub-BotJS/index.js:60:24)
    at ReactionCollector.emit (node:events:525:35)
    at ReactionCollector.emit (node:domain:489:12)
    at ReactionCollector.handleCollect (/home/runner/ATC24-Practice-Hub-BotJS/node_modules/discord.js/src/structures/interfaces/Collector.js:119:14)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:394:10)

What am I doing wrong? Here's my code:

const TOKEN = process.env['token'];
const GUILD_ID = process.env['guild_id'];
const Discord = require("discord.js");

const client = new Discord.Client({intents:[Discord.GatewayIntentBits.Guilds,
        Discord.GatewayIntentBits.GuildMessages,
        Discord.GatewayIntentBits.MessageContent,
        Discord.GatewayIntentBits.GuildMembers,
    Discord.GatewayIntentBits.GuildMessageReactions]})

async function get_react_user(id) {
  
  var newcomer = msg.guild.roles.cache.find(role => role.name == "Newcomer");
  const react_user = await guild.members.fetch(id);
  return react_user;
  
} 

client.on("ready", () => {
  
    client.channels.fetch("1057682369739489312").then(channel => {
  channel.messages.fetch("1057813574799609906").then(message => {

    const filter = (reaction, user) => {
       return ["✅", "❌"].includes(reaction.emoji.name);
    };
    
    const collector = message.createReactionCollector({ filter });

    const guild = client.guilds.cache.get(GUILD_ID);
    
    collector.on('collect', (reaction, user) => {

    if (reaction.emoji.name === "✅") {
      const role = guild.roles.cache.find(role => role.name === 'Newcomer');
      user.send("This is a test, if this works, I'm a legend");
      react_user = get_react_user(user.id);
      react_user.roles.remove(role);
      
    }
       
    });

  })
})                                                               
})

client.login(TOKEN);

I am not sure if you do get that the problem is you having a huge skill issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM