Discord bot发布多个结果



首次使用stackoverflow。这是一个机器人,每当在搜索列表中添加新一集的节目时,都会发布结果。我想机器人张贴结果只有一次每集,但机器人张贴同一集多次在不同的时间框架。它得到修复一段时间后,我重新启动机器人。将show添加到搜索列表的代码。

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,所以我被留下来主持我在replit上主持的机器人。这将是很大的帮助,了解问题是否与代码或不是。

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

最新更新