[英]Problem with removing a role from a specific user using discord.js
所以,我一直在努力完成我的 discord 机器人,我想添加的最后一件事是让它在有人用 ✅ 对特定消息做出反应时,它会向他们发送 DM 并删除一个名为“新人”。 我获取了消息 ID 并使 DM 部分正常工作(使用 user.id 获取用户 ID),但是由于某种原因,我无法通过删除角色来执行相同的操作。
我尝试使用“client.channels.cache.get()”,但那时我意识到我正在使用异步并且返回值是 Promise 而不是我正在寻找的实际公会成员。 为了解决这个问题,我创建了一个异步 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)
我究竟做错了什么? 这是我的代码:
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);
我不确定你是否明白问题是你有一个巨大的技能问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.