Discord.js - 为来自 Reaction.users.fetch().each((user) =&a

时间:2023-02-10
本文介绍了Discord.js - 为来自 Reaction.users.fetch().each((user) => {...}) 的下一个用户继续循环;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 client.on('ready', async() => {...}); 下的代码片段;:

this.MyServer = await client.guilds.fetch('MyServer ID here').catch(error => { console.log(error) });
this.MyRole = await this.Server.roles.cache.find((role) => role.name == `MyRole name here`);
this.MyChannel = await client.channels.fetch('MyChannel ID here').catch(error => { console.log(error) });
this.MyMessage = await this.RoleChannel.messages.fetch('MyMessage ID here').catch(error => { console.log(error) });
this.MyEmojiReaction = await this.MyMessage.reactions.cache.get('MyEmoji ID here');
this.ReactingUsers = await this.MyEmojiReaction.users.fetch();

this.ReactingUsers.each(async (user) => {
 if (!this.MyServer.members.cache.get(user.id)) return;
 if (!user.bot) {
  try {
   const member = await this.MyMessage.guild.members.fetch(user.id);
   member.roles.add(this.MyRole);
  } catch (error) {
   console.log(error);
   return;
  }
 }
});

当机器人启动时,它应该检查反应,消息中的 this.MyEmojiReaction,频道中的 this.MyMessagethis.MyChannel,并将 this.ReactingUsers 中存储的响应用户添加到角色 this.MyRole.除了一个问题外,它基本上工作正常.

As the bot starts, it should check for the reaction, this.MyEmojiReaction in the message, this.MyMessage in the channel, this.MyChannel, and add the reacting users stored in this.ReactingUsers to the role, this.MyRole. It is working mostly fine except one problem.

第一行if (!this.MyServer.members.cache.get(user.id)) return; in this.ReactingMembers.each(async (user) =>如果来自 this.ReactingMembers 的用户不存在于 this.MyServer 中,{..}负责从函数返回, this.ReactingMembers 中的下一个用户永远不会执行该函数,可能是因为它从整个循环中返回.我想为下一个用户继续循环.

The first line if (!this.MyServer.members.cache.get(user.id)) return; in this.ReactingMembers.each(async (user) => {..}is responsible to return from the function if the user from this.ReactingMembers is not present in this.MyServer and it does but as it returns, the function never executes for the next users in this.ReactingMembers, maybe because it returns from the entire loop. I want to continue the loop for the next users.

推荐答案

如果我理解正确,你应该使用 继续.

If i understand it correctly, you should use continue.

它将转而进入下一次迭代,以及数组中的下一个用户.

It will instead go to the next iteration, and the next User in the array.

在你的情况下,而不是这个:

In your case, instead of this:

if (!this.MyServer.members.cache.get(user.id)) return;

你应该这样做:

if (!this.MyServer.members.cache.has(user.id))  continue;

这篇关于Discord.js - 为来自 Reaction.users.fetch().each((user) => {...}) 的下一个用户继续循环;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何在 Discord.js 中添加错误消息? 下一篇:如何让不和谐机器人编辑 dm 消息?

相关文章

最新文章