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

Call redundancy in the cache #2

@NathaelB

Description

@NathaelB

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions