Discord机器人无法添加很多角色...抱怨事件监听器 [英] Discord bot having trouble adding lots of roles... complains about event listeners

查看:40
本文介绍了Discord机器人无法添加很多角色...抱怨事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Discord机器人,可以更新我们的Discord上的页首横幅频道.每次将游戏记录到数据库中时,机器人都会获取排行榜数据,然后针对遇到的每个memberId删除所有技能标记(技能越高等于声望更高的标记),然后根据其当前技能添加正确的标记.出于某种原因,连续添加很多成员角色导致该机器人仅从前100名成员中脱颖而出.大约15名成员.我对js很陌生,但我认为每个addRole命令都会触发GuildMemberUpdate事件,而且处理起来实在太多了.这是我收到的警告消息.其次是大量的角色超时错误(我将添加一个).

I have a discord bot that updates a leaderboard channel on our discord. Each time a game is recorded in the database, the bot gets the leaderboard data, then for each memberId it comes across, it removes all the skill badges (higher skill equals more prestigious badge), then adds the correct badge based on their current skill. For some reason, adding a lot of member roles in succession is causing the bot to only get through about 15 or so members out of the top 100. I'm pretty new to js, but I think each addRole command is triggering a GuildMemberUpdate event, and it's just too much to handle. Here is the warning message I get. It's followed by a ton of adding role timeout errors (I'll include one).

(node:612) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 guildMemberUpdate listeners added. Use emitter.setMaxListeners() to increase limit
    at _addListener (events.js:280:19)
    at Client.addListener (events.js:297:10)
    at Promise (/home/ubuntu/bots/node_modules/discord.js/src/client/rest/RESTMethods.js:511:19)
    at new Promise (<anonymous>)
    at RESTMethods.addMemberRole (/home/ubuntu/bots/node_modules/discord.js/src/client/rest/RESTMethods.js:501:12)
    at GuildMember.addRole (/home/ubuntu/bots/node_modules/discord.js/src/structures/GuildMember.js:453:37)
    at C6Leaderboard.applyTags (/home/ubuntu/bots/modules/leaderboard.js:139:17)
    at C6Leaderboard.createLeaderboard (/home/ubuntu/bots/modules/leaderboard.js:110:18)
    at C6Leaderboard.publishLeaderboard (/home/ubuntu/bots/modules/leaderboard.js:67:29)
    at <anonymous>
Error: Adding the role timed out.
    at timeout.client.setTimeout (/home/ubuntu/bots/node_modules/discord.js/src/client/rest/RESTMethods.js:514:16)
    at Timeout.setTimeout [as _onTimeout] (/home/ubuntu/bots/node_modules/discord.js/src/client/Client.js:433:7)
    at ontimeout (timers.js:475:11)
    at tryOnTimeout (timers.js:310:5)
    at Timer.listOnTimeout (timers.js:270:5)

这是我模块代码的要点:

Here is the gist of my module's code:

class C6Leaderboard
{
    constructor() {
        util.client.on('ready', () =>
        {
            guild = util.client.guilds.get("291751672106188800");
            this.generate(); //generate an up to date leaderboard
        });
    }

    async generate()
    {
        try
        {
            // Grab data
            this.leaderboard = await util.makeRGRequest('leaderboard.php', {});

            // Publish new Leaderboard
            this.publishLeaderboard();
        }
        catch(err)
        {
            console.log('[leaderboard_err]' + err);
        }
    }

    async publishLeaderboard()
    {
        const channel = util.getChannel(483346000233365526);
        const msg = await channel.fetchMessage(channel.lastMessageID());
        const content_new = this.createLeaderboard();

        if( msg.content != content_new) msg.edit( content_new );
    }

    createLeaderboard(i, max_top)
    {
        //loop through leaderboard data
            //print line of leaderboard message
            this.applyTags(this.leaderboard[i].id, this.leaderboard[i].skill);
    }

    async applytags(id, skill)
    {
         if (!guild)
            return;
        let mem = guild.members.get(id);
        if (!mem)
            return;
        mem.removeRoles(difficulties);
        if (skill < 1500)
            mem.addRole(settler).catch(console.error);
        else if (skill >= 1500 && skill < 1600)
            mem.addRole(chieftain).catch(console.error);
        else if (skill >= 1600 && skill < 1700)
            mem.addRole(warlord).catch(console.error);
        else if (skill >= 1700 && skill < 1800)
            mem.addRole(prince).catch(console.error);
        else if (skill >= 1800 && skill < 1900)
            mem.addRole(king).catch(console.error);
        else if (skill >= 1900 && skill < 2000)
            mem.addRole(emperor).catch(console.error);
        else if (skill >= 2000 && skill < 2100)
            mem.addRole(immortal).catch(console.error);
        else if (skill >= 2100)
            mem.addRole(deity).catch(console.error);
    }
}

推荐答案

返回Promise的方法需要使用await调用,并且不能嵌套.

Methods that return a Promise need to be called using await and cannot be nested.

async function applytags(id, skill)
{
     if (!guild)
        return;
    let mem = await guild.members.get(id);
    if (!mem)
        return;
    await mem.removeRoles(difficulties);
    if (skill < 1500)
        await mem.addRole(settler).catch(console.error);
    else if (skill >= 1500 && skill < 1600)
        await mem.addRole(chieftain).catch(console.error);
    else if (skill >= 1600 && skill < 1700)
        await mem.addRole(warlord).catch(console.error);
    else if (skill >= 1700 && skill < 1800)
        await mem.addRole(prince).catch(console.error);
    else if (skill >= 1800 && skill < 1900)
        await mem.addRole(king).catch(console.error);
    else if (skill >= 1900 && skill < 2000)
        await mem.addRole(emperor).catch(console.error);
    else if (skill >= 2000 && skill < 2100)
        await mem.addRole(immortal).catch(console.error);
    else if (skill >= 2100)
        await mem.addRole(deity).catch(console.error);
}

  1. https://anidiots.guide/other-guides/async-await
  2. https://hackernoon.com/javascript-promises-and-why-async-await-wins-the-battle-4fc9d15d509f
  3. https://medium.com/@tkssharma/writing-neat-asynchronous-node-js-code-with-promises-async-await-fa8d8b0bcd7c
  4. https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
  5. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

这篇关于Discord机器人无法添加很多角色...抱怨事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆