根据使用的邀请代码为新成员赋予角色

时间:2018-09-26 14:13:57

标签: javascript bots discord discord.js

我的代码需要帮助。

下面的代码应为从邀请(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"));
            }
        })
    })
});

问题:

它从任何邀请中赋予每个新成员这个角色。

2 个答案:

答案 0 :(得分:0)

获取邀请后,当前正在浏览它们以寻找邀请代码。由于该代码确实存在,它将添加用户,即使该密码不是用户使用的邀请代码也是如此。相反,您需要遍历邀请并检查使用了哪些邀请,然后对照其中一个代码进行检查。

来自https://github.com/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 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"));
        }
    });
});

来源警告:

  

这就是问题所在。每次获取邀请时,您都在访问Discord API并要求提供信息。尽管对于小型机器人而言这不是问题,但随着机器人的发展,它可能会出现问题。我并不是说使用此代码会让您被API所禁止-如果不滥用API,查询API不会存在固有问题。但是,特别是在性能方面存在一些技术问题。

     

您拥有的行会越多,每个行会中的邀请就越多,您从API接收到的数据就越多。 Rember,由于ready事件的工作方式,您需要稍等一会儿再运行fetchInvite方法,行会越多,等待的时间就越多。在此期间,成员联接将无法正确注册,因为缓存不存在。

     

此外,缓存本身的大小会增加,因此您正在使用更多的内存。最重要的是,花更多的时间对邀请进行分类以存在更多的邀请。可能会使该漫游器显示出对成员加入的响应较慢。

     

因此,可以得出结论,以上代码可以很好地运行,并且不会使您陷入Discord的麻烦,但是我不建议在大型bot上实现此功能,尤其是在您担心内存和性能的情况下。

答案 1 :(得分:0)

您无法通过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功能不会使您感到困惑。

相关问题