Sharded Discord.js 机器人重置为初始存在状态

问题描述 投票:0回答:1

我在 NodeJS 中编写了一个 Discord.js 机器人,它使用 discord-hybrid-sharding 来生成我的

bot.js

在下面的
bot.js
代码中,您可以看到我的初始状态为“正在开始...”,然后在
ClientReady
中我设置了实际状态。

然而,看似随机,我的机器人只是返回到最初的“开始...”状态。但它运行完全正常。我认为可能是某个碎片死亡或其他什么原因,但为什么这会破坏我的间隔呢?遗憾的是,我在间隔期间尝试进行的检查不起作用。

status reset

当我重新启动机器人的 pm2 进程时,状态会再次正确设置一段时间,但随后会随机返回到“正在开始...”。


分片管理器 (

app.js
) 启动
bot.js
:

import { ClusterManager } from "discord-hybrid-sharding";
import { config } from "../config/config.js";

const manager = new ClusterManager("./src/bot.js", {
    totalShards: "auto",
    shardsPerClusters: 2,
    token: config.discord.bot_token,
});

manager.on("clusterCreate", shard => console.log(`Launched shard ${shard.id}`));

manager.spawn({ timeout: -1 });

bot.js
设置状态:

import { ClusterClient, getInfo } from "discord-hybrid-sharding";
import DiscordClient from "discord.js";

const client = new DiscordClient({
    presence: {
        status: "dnd",
        activities: [{ name: "Starting...", type: ActivityType.Playing }],
    },
    shards: getInfo().SHARD_LIST,
    shardCount: getInfo().TOTAL_SHARDS,
});

client.cluster = new ClusterClient(client);

client.on(Events.ClientReady, async() => {
    const guildCount = await client.guilds.fetch().then(guilds => guilds.size);

    client.user?.setActivity({ name: `Counting on ${guildCount} servers!`, type: ActivityType.Playing });

    let lastGuildCount = guildCount;
    setInterval(async() => {
        const newGuildCount = await client.guilds.fetch().then(guilds => guilds.size);
        const statusHasReset = client.user?.presence.activities[0].name === "Starting...";

        if (newGuildCount !== lastGuildCount || statusHasReset){
            lastGuildCount = newGuildCount;
            client.user?.setActivity({ name: `Counting on ${newGuildCount} servers!`, type: ActivityType.Playing });
        }
    }, 45 * 60 * 1000);

    client.user?.setStatus("online");
});

(代码中不相关的部分已被省略。如果需要,完整代码位于https://github.com/NullDev/Arithmetica-Bot

为什么会发生这种情况以及如何解决?

javascript node.js discord discord.js sharding
1个回答
0
投票

Discord.js Discord 上的某人帮助了我:

将我的

setInterval
逻辑移至
shardReady
事件就成功了。
一旦机器人启动,以及每次分片重新启动时,都会调用此事件。
ClientReady
不同,它仅在机器人启动时调用一次。

const setStatus = async function(client, count){
    client.user?.setActivity({ name: `Counting on ${count} servers!`, type: ActivityType.Playing });
    client.user?.setStatus("online");
};

client.on(Events.ClientReady, async() => {
    const guildCount = await client.guilds.fetch().then(guilds => guilds.size);
    await setStatus(client, guildCount);
});

client.on(Events.ShardReady, async shard => {
    const guildCount = await client.guilds.fetch().then(guilds => guilds.size);
    await setStatus(client, guildCount);

    let lastGuildCount = guildCount;
    setInterval(async() => {
        const newGuildCount = await client.guilds.fetch().then(guilds => guilds.size);

        if (newGuildCount !== lastGuildCount){
            lastGuildCount = newGuildCount;
            await setStatus(client, newGuildCount);
            Log.info("Guild count changed to " + newGuildCount + ". Updated activity.");
        }
    }, 10 * 60 * 1000);
});
© www.soinside.com 2019 - 2024. All rights reserved.