我需要有人帮忙处理我的密码。
下面的代码应该为从invite (HrTHHFf)中加入的新成员提供TRZ角色。
bot.on("guildMemberAdd", (member) => {
if (member.id == bot.user.id) {
return;
}
let guild = member.guild
guild.fetchInvites().then(invdat => {
invdat.forEach((invite, key, map) => {
console.log(invite.code)
if (invite.code === "HrTHHFf") {
return member.addRole(member.guild.roles.find(role => role.name === "TRZ"));
}
})
})
});这一问题:
它将角色赋予每一个新成员,从任何邀请。
发布于 2018-09-26 14:48:22
在获取了邀请之后,您目前正在遍历它们,寻找邀请代码。由于该代码确实存在,它将添加用户,即使这不是用户使用的邀请代码。相反,您需要循环访问邀请并检查使用了哪一个,然后对照该代码进行检查。
// Initialize the invite cache
const invites = {};
// A pretty useful method to create a delay without blocking the whole script.
const wait = require('util').promisify(setTimeout);
client.on('ready', () => {
// "ready" isn't really ready. We need to wait a spell.
wait(1000);
// Load all invites for all guilds and save them to the cache.
client.guilds.forEach(g => {
g.fetchInvites().then(guildInvites => {
invites[g.id] = guildInvites;
});
});
});
client.on('guildMemberAdd', member => {
// To compare, we need to load the current invite list.
member.guild.fetchInvites().then(guildInvites => {
// This is the *existing* invites for the guild.
const ei = invites[member.guild.id];
// Update the cached invites
invites[member.guild.id] = guildInvites;
// Look through the invites, find the one for which the uses went up.
const invite = guildInvites.find(i => ei.get(i.code).uses < i.uses);
console.log(invite.code)
if (invite.code === "HrTHHFf") {
return member.addRole(member.guild.roles.find(role => role.name === "TRZ"));
}
});
});来自源的警告:
这就是问题所在。每次获取邀请时,您都会使用请求信息来访问不和谐的API。虽然这并不是小型机器人的问题,但随着机器人的成长,这可能是个问题。我并不是说使用这段代码会使您从API中被禁止--如果不滥用API,那么查询API就没有固有的问题。但是,特别是性能方面存在一些技术问题。 你拥有的行会越多,每个行会的邀请就越多,你从API收到的数据就越多。记住,由于就绪事件的工作方式,您需要在运行fetchInvite方法之前稍等一下,而且您拥有的公会越多,您需要等待的时间就越多。在此期间,成员联接将无法正确注册,因为缓存不存在。 此外,缓存本身的大小也在增加,因此您需要使用更多的内存。最重要的是,对邀请进行排序需要更多的时间。这可能会使bot在响应成员加入时显得更慢。 因此,最后,上面的代码运行得非常好,不会给您带来不和谐的麻烦,但我不建议在较大的机器人上实现这一点,特别是当您担心内存和性能时。
发布于 2018-09-26 14:49:18
您不能从API中做到这一点,但是服务器为了作为邀请朋友的奖励所做的基本上是检查从新用户加入协会之前和之后对服务器发出的邀请数量的差异。
所以,当机器人加载时,你就有了一部分代码,
let uses; // this variable hold number of uses of invite with code HrTHHFf
guild.fetchInvites().then(invdat => {
let [invite] = invdat.filter(e=>e.code === "HrTHHFf")
uses = invite.uses
})当新用户加入行会时,检查一下这个号码是否有变化,
guild.fetchInvites().then(invdat => {
let [invite] = invdat.filter(e=>e.code === "HrTHHFf")
if(uses != invite.uses){
member.addRole(member.guild.roles.find(role => role.name === "TRZ"));
}
})希望es6特性不会让您感到困惑。
https://stackoverflow.com/questions/52519821
复制相似问题