根据使用的邀请码为新成员分配角色
Give new members a role based on the invite code used
我的代码需要一些帮助。
下面的代码应该为通过邀请 (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"));
}
})
})
});
问题:
它将角色分配给来自任何邀请的每个新成员。
获取邀请后,您目前正在循环查找邀请码。由于该代码确实存在,它会添加用户,即使那不是用户使用的邀请代码。相反,您需要遍历邀请并检查使用了哪一个,然后检查那个代码。
// 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"));
}
});
});
来源警告:
So here's the problem. Each time you fetch invites, you're hitting the Discord API with a request for information. While that's not an issue for small bots, it might as the bot grows. I'm not saying that using this code would get you banned from the API - there is no inherent problem with querying the API if it's not abuse. However, there are a few technical issues with performance especially.
The more guilds you have, the more invites are in each guild, the more data you're receiving from the API. Rember, because of the way the ready event works, you need to wait a bit before running the fetchInvite method, and the more guilds you have, the more time you need to wait. During this time, member joins won't correctly register because the cache doesn't exist.
Furthermore, the cache itself grows in size, so you're using more memory. On top of which, it takes more time to sort through the invites the more invites exist. It might make the bot appear as being slower to respond to members joining.
So to conclude, the above code works perfectly well and it will not get you in trouble with Discord, but I wouldn't recommend implementing this on larger bots, especially if you're worried about memory and performance.
你不能从 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 特性不会让您感到困惑。
我的代码需要一些帮助。
下面的代码应该为通过邀请 (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"));
}
})
})
});
问题:
它将角色分配给来自任何邀请的每个新成员。
获取邀请后,您目前正在循环查找邀请码。由于该代码确实存在,它会添加用户,即使那不是用户使用的邀请代码。相反,您需要遍历邀请并检查使用了哪一个,然后检查那个代码。
// 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"));
}
});
});
来源警告:
So here's the problem. Each time you fetch invites, you're hitting the Discord API with a request for information. While that's not an issue for small bots, it might as the bot grows. I'm not saying that using this code would get you banned from the API - there is no inherent problem with querying the API if it's not abuse. However, there are a few technical issues with performance especially.
The more guilds you have, the more invites are in each guild, the more data you're receiving from the API. Rember, because of the way the ready event works, you need to wait a bit before running the fetchInvite method, and the more guilds you have, the more time you need to wait. During this time, member joins won't correctly register because the cache doesn't exist.
Furthermore, the cache itself grows in size, so you're using more memory. On top of which, it takes more time to sort through the invites the more invites exist. It might make the bot appear as being slower to respond to members joining.
So to conclude, the above code works perfectly well and it will not get you in trouble with Discord, but I wouldn't recommend implementing this on larger bots, especially if you're worried about memory and performance.
你不能从 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 特性不会让您感到困惑。