Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Call redundancy in the cache #2

Open
NathaelB opened this issue Feb 26, 2024 · 0 comments
Open

Call redundancy in the cache #2

NathaelB opened this issue Feb 26, 2024 · 0 comments

Comments

@NathaelB
Copy link

A call to the cache to retrieve a channel is repeated every minute in a setInterval.
The latter could be exported outside the callback to avoid this redundancy.

const client = require('../../../index');
const axios = require('axios');

const CONNECTED_USERS_CHANNEL_ID = process.env.CONNECTED_USERS_CHANNEL_ID;
const MEMBERS_CHANNEL_ID = process.env.MEMBERS_CHANNEL_ID;
const IP = process.env.IP;

client.on("ready", () => {
    const channel = client.channels.cache.get(CONNECTED_USERS_CHANNEL_ID)
    async function updateStatus() {
        try {
            const response = await axios.get(`https://api.mcsrvstat.us/2/${IP}`);
            const json = response.data;

            await channel.setName(`🚀 Connectés: ${json.players.online}`);

            const guild = client.guilds.cache.first();
            client.channels.cache.get(MEMBERS_CHANNEL_ID).setName(`🌏 Membres: ${guild.memberCount}`);
        } catch (error) {
            console.error("Une erreur s'est produite:", error);
        }
    }

    setInterval(updateStatus, 60000); // 1 minute
});

The action can of course be repeated on the guild and the other channel recovered from the cache.

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant