英文:
How to prevent execution if user no longer exists? Discord.js
问题
我正在开发一个Discord机器人,并遇到了一个小问题,我使用“messageReactionRemove”和“messageReactionAdd”来删除或添加特定的角色,问题出现在用户离开服务器时,因为当他离开时,使用“guildMemberRemove”来删除某些用户的反应,这又调用了“messageReactionRemove”,但是用户已经不在服务器上了(错误)。
client.on('messageReactionRemove', (reaction, user) => {
if (reaction.message.id == '1110918756189884496') {
let role = reaction.message.guild.roles.cache.find(role => role.name === "Verified");
reaction.message.guild.members.cache.get(user.id).roles.remove(role);
}
});
client.on("guildMemberRemove", (member) => {
const channel = member.guild.channels.cache.find((ch) => ch.id === '1109131318350053457');
channel.messages.fetch(1110918756189884496
).then(message => {
message.reactions.cache.find(reaction => reaction.emoji.name == "✅").users.remove(member.user.id);
})
});
如果我使用fetch()而不是cache,那么我会得到一个错误(未知成员)。
你有任何想法吗?感谢任何信息
英文:
I'm developing a discord bot and faced a small problem, I use "messageReactionRemove" and "messageReactionAdd" to remove or add certain roles, the problem happens when the user leaves the server, as when he leaves, certain user reactions are removed using "guildMemberRemove", which again calls "messageReactionRemove", but the user is no longer on the server (error).
client.on('messageReactionRemove', (reaction, user) => {
if (reaction.message.id == '1110918756189884496') {
let role = reaction.message.guild.roles.cache.find(role => role.name === "Verified");
reaction.message.guild.members.cache.get(user.id).roles.remove(role);
}
});
client.on("guildMemberRemove", (member) => {
const channel = member.guild.channels.cache.find((ch) => ch.id === '1109131318350053457');
channel.messages.fetch(`1110918756189884496`).then(message => {
message.reactions.cache.find(reaction => reaction.emoji.name == "✅").users.remove(member.user.id);
})
});
If I use fetch() instead of cache, then I get an error (Unknown member)
Do you have any ideas? Thanks for any information
答案1
得分: 0
I found another similar post, but I didn't find an answer there: https://stackoverflow.com/questions/71519803/how-to-check-the-existence-of-a-user-in-discord-js-without-errors?rq=2
However, I found a way around it!
Instead of deleting the user's reaction when he leaves the server, we delete it as soon as he connects to the server (in my case this logic works, since the server content is unlocked for the user only after agreeing to the rules => reacting)
client.on('guildMemberAdd', member => {
const channel = member.guild.channels.cache.find((ch) => ch.id === '1109131318350053457');
channel.messages.fetch('1110918756189884496').then(message => {
message.reactions.cache.find(reaction => reaction.emoji.name == "✅").users.remove(member.user.id);
})
});
英文:
I found another similar post, but I didn't find an answer there: https://stackoverflow.com/questions/71519803/how-to-check-the-existence-of-a-user-in-discord-js-without-errors?rq=2
However, I found a way around it!
Instead of deleting the user's reaction when he leaves the server, we delete it as soon as he connects to the server (in my case this logic works, since the server content is unlocked for the user only after agreeing to the rules => reacting)
client.on('guildMemberAdd', member => {
const channel = member.guild.channels.cache.find((ch) => ch.id === '1109131318350053457');
channel.messages.fetch(`1110918756189884496`).then(message => {
message.reactions.cache.find(reaction => reaction.emoji.name == "✅").users.remove(member.user.id);
})
});
答案2
得分: 0
抱歉首次误解了您的问题。
client.on('messageReactionRemove', (reaction, user) => {
if (reaction.message.id === '1110918756189884496') {
let role = reaction.message.guild.roles.cache.find(role => role.name === "Verified");
reaction.message.guild.members.fetch(user.id)
.then(member => {
member.roles.remove(role);
})
.catch(() => { });
}
});
英文:
Sorry for missunderstanding your question first.
client.on('messageReactionRemove', (reaction, user) => {
if (reaction.message.id == '1110918756189884496') {
let role = reaction.message.guild.roles.cache.find(role => role.name === "Verified");
reaction.message.guild.members.fetch(user.id)
.then(member => {
member.roles.remove(role);
})
.catch(() => { });
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论