简体   繁体   English

使用 discord.js 从特定用户删除角色时出现问题

[英]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".所以,我一直在努力完成我的 discord 机器人,我想添加的最后一件事是让它在有人用 ✅ 对特定消息做出反应时,它会向他们发送 DM 并删除一个名为“新人”。 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.我获取了消息 ID 并使 DM 部分正常工作(使用 user.id 获取用户 ID),但是由于某种原因,我无法通过删除角色来执行相同的操作。

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.我尝试使用“client.channels.cache.get()”,但那时我意识到我正在使用异步并且返回值是 Promise 而不是我正在寻找的实际公会成员。 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:为了解决这个问题,我创建了一个异步 function 并通过等待传递了我之前提到的 function,但它仍然返回相同的该死的错误:

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.我不确定你是否明白问题是你有一个巨大的技能问题。

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

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