admin管理员组

文章数量:1391975

I need some help with my code.

The code below should give new members that joined from the the invite (HrTHHFf) the TRZ role.

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"));
            }
        })
    })
});

The issue:

It gives the role to every new member, from any invite.

I need some help with my code.

The code below should give new members that joined from the the invite (HrTHHFf) the TRZ role.

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"));
            }
        })
    })
});

The issue:

It gives the role to every new member, from any invite.

Share Improve this question edited Sep 27, 2018 at 14:30 frobinsonj 1,16710 silver badges22 bronze badges asked Sep 26, 2018 at 14:13 mr. hmr. h 331 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

After you have fetched the invites, you are currently looping through them looking for an invite code. As that code does exist, it will add the user, even if that was not the invite code that the user used. Instead, you need to loop through the invites and check which one has been used, then check against that one code.

An example from https://github./AnIdiotsGuide/discordjs-bot-guide/blob/master/coding-guides/tracking-used-invites.md:

// 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 pare, 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"));
        }
    });
});

Warning from source:

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 remend implementing this on larger bots, especially if you're worried about memory and performance.

You can't do that from API, but basically what servers do in order to give role as a reward for inviting friend is checking for differences in numbers of invitation to server from the moment before new user joins guild and moment after that.

So, you'll have part of code when bot loads,

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
})

And when new users join the guild, check for change of that number,

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")); 
    }
})

Hope that es6 features doesn't confuse you.

本文标签: javascriptGive new members a role based on the invite code usedStack Overflow