如何防止在用户不存在时执行? Discord.js

huangapple go评论61阅读模式
英文:

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(() => { });
    }
});

huangapple
  • 本文由 发表于 2023年5月24日 23:26:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定