【问题标题】:Discord bot post multiple resultsDiscord 机器人发布多个结果
【发布时间】:2021-04-26 15:21:24
【问题描述】:

第一次使用stackoverflow。每当在 nyaa.si 上添加搜索列表中的新剧集时,它都会发布结果。我希望机器人每集只发布一次结果,但机器人在不同的时间范围内多次发布同一集。在我重新启动机器人后它会得到修复。 将节目添加到搜索列表的代码。

async addShow(msg) {
        const regex = /"(.+?)" "(.+?)"(?: "(.+?)")?/g;
        const found = regex.exec(msg.content);
        if (found === null) {
            await msg.channel.send(`Invalid new syntax:\n${COMMAND_CHARACTER} new \"show search phrase\" \"MALURL\" \"attribute regex\" (optional last)`);
            return;
        }

        let [f, search, url, reg] = found;
        let count = await this.db.get('search').last().value();
        if (_.isUndefined(count)) {
            count = 0;
        }
        else {
            count = count.id;
        }
        await this.db.get('search').push({id: count + 1, search, url, regex: reg}).write();
        logger.info(`New show has been added to the searchlist - ${search} - ${url} for server ${this.guildID}`);
        await msg.channel.send("Saved!");
    }

要搜索的代码

async searchShow(id, query, channel = null, OG = null) {
        const results = await this.nyaa.getResults(query);
        if (!results.length) {
            return;
        }

        logger.info(`Results found for ${query}: ${results.length}`);
        const embedFunction = this.getRichEmbed.bind(this);

        for (let i of results) {
            const item = await this.db.get('rss').find({guid: i.guid}).value();
            if (!_.isUndefined(item)) {
                continue;
            }

            if (await this.postShow(embedFunction, i, channel, OG)) {
                await this.db.get('rss').push({...i, searchID: id}).write();
            }
        }
    }

新剧集到来时发布结果的代码。

async postShow(embedFunc, item, channel = null, og = null, channelType = NYAA_UPDATES) {
        if (channel === null) {
            channel = await this.getGuildChannel(channelType);

            if (!channel) {
                return false;
            }
        }

        return new Promise(async (resolve) => {
            const title = (og !== null ? og.title ?? item.title : item.title);
            const embed = await embedFunc(item, title);

            if (og !== null) {
                const img = og.image ?? null;
                if (img) {
                    embed.setThumbnail(img);
                }

                const url = og.url ?? null;
                if (url) {
                    embed.setURL(url);
                }
            }

            let retryCounter = 0;

            logger.info(`Posting new result for ${title} with guid ${item.guid} for server ${this.guildID}`);
            while (true) {
                try {
                    await channel.send(embed);
                    setTimeout(() => {
                        resolve(true);
                    }, 2000);
                    break;
                }
                catch (e) {
                    logger.warn(`An error has occured while posting: ${e.toString()}, retrying (${++retryCounter} in 5 seconds`);
                    await new Promise((res) => {
                        setTimeout(() => {
                            res();
                        }, 5000);
                    });
                    if (retryCounter > 10) {
                        resolve(false);
                    }
                }
            }
        });
    }

另外,编写大部分代码的人是不同的人,我只是在这里和那里添加了一些额外的功能,这对代码没有太大影响。编写了大部分核心代码的人不得不离开 discord,所以我只能托管我在 repl.it 托管的机器人。知道问题是否出在代码上会很有帮助。

【问题讨论】:

  • 我也使用 json 作为保存搜索列表的数据库。此外,当添加搜索列表中的任何节目的新剧集时,会推送该剧集,然后将其嵌入并作为结果发布到 discord 上。
  • 如果错误不在代码中,那么可能有两个活动的不和谐钩子

标签: javascript typescript discord.js


【解决方案1】:

正如 Julian Kleine 上面提到的,机器人多次发布的最常见原因是您正在运行主机的多个实例。关闭所有命令提示符实例,并检查任务管理器以查看是否有其他隐藏实例在后台运行。

【讨论】:

  • 当我在 repll.it 中托管一个机器人时,我不知道如何检查多个实例是否正在运行。
  • 啊,我没有这方面的经验,抱歉 - 请尝试查看文档
猜你喜欢
  • 1970-01-01
  • 2019-09-12
  • 2020-01-25
  • 1970-01-01
  • 2021-08-19
  • 2021-06-18
  • 2021-07-14
  • 2021-04-01
  • 2021-02-02
相关资源
最近更新 更多