問(wèn)題描述
這是 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;
}
}
});
當(dāng)機(jī)器人啟動(dòng)時(shí),它應(yīng)該檢查反應(yīng),消息中的 this.MyEmojiReaction
,頻道中的 this.MyMessage
,this.MyChannel
,并將 this.ReactingUsers
中存儲(chǔ)的響應(yīng)用戶添加到角色 this.MyRole
.除了一個(gè)問(wèn)題外,它基本上工作正常.
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) =>如果來(lái)自
負(fù)責(zé)從函數(shù)返回, this.ReactingMembers
的用戶不存在于 this.MyServer
中,{..}this.ReactingMembers
中的下一個(gè)用戶永遠(yuǎn)不會(huì)執(zhí)行該函數(shù),可能是因?yàn)樗鼜恼麄€(gè)循環(huán)中返回.我想為下一個(gè)用戶繼續(xù)循環(huán).
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.
推薦答案
如果我理解正確,你應(yīng)該使用 繼續(xù)
.
If i understand it correctly, you should use continue
.
它將轉(zhuǎn)而進(jìn)入下一次迭代,以及數(shù)組中的下一個(gè)用戶.
It will instead go to the next iteration, and the next User in the array.
在你的情況下,而不是這個(gè):
In your case, instead of this:
if (!this.MyServer.members.cache.get(user.id)) return;
你應(yīng)該這樣做:
if (!this.MyServer.members.cache.has(user.id)) continue;
這篇關(guān)于Discord.js - 為來(lái)自 Reaction.users.fetch().each((user) => {...}) 的下一個(gè)用戶繼續(xù)循環(huán);的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!