Discord.js - Continue loop for next users from Reaction.users.fetch().each((user) =gt; {...});(Discord.js - 为来自 Reaction.users.fetch().each((user) =gt; {...}) 的下一个用户继续循环;)
问题描述
这是 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.MyMessage,this.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) => {...}) 的下一个用户继续循环;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Discord.js - 为来自 Reaction.users.fetch().each((user) =&a
基础教程推荐
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
