Ad

How To Create Multiple Discord Bot Statuses With Javascript

- 1 answer

I want to add something so after it plays the block from the API, I want it to be playing another status. For example, if it plays the block for some mins then it changes to playing something like "hello world", then it changes back to playing block after few mins. Below is the code I currently have:

function updateBlock() {
    let response3 = axios.get(`https://google/api/getblockcount`)
    .then((response3) => {
        return Promise.resolve(response3);
    }).catch((error) => {
        console.log("Can not connect to API");
        console.log(error);
        return Promise.resolve({
            failed: true
        })
    });

    response3.then((response3)=> {
        if (response3.failed) {
            console.log("API Response Failed");
            return response3;
        }
        let b = response3.data;
        try {
            Client.user.setActivity("B: " + b, { type: 'WATCHING' })
            .then()
            .catch(console.error);
        } catch(err) {
            console.log("This is an API error catch");
            console.log(err);
        }
    });
}

Client.on("ready", () => {
    console.log(botName + " online!");
    updateBlock();
    setInterval(() => {
        updateBlock();
    }, 10000);
});
Ad

Answer

Let's assume you are using discord.js library, this is how to display a random item from array textList in which is also block height received from API, changing every 1min.

client.on('ready', () => {
    setInterval(async ()=>{
        let {data: blocks} = await axios.get('https://chain.ragnaproject.io/api/getblockcount').catch(console.log)
        let textList = ['Hello World','Lorem Ipsum','Discord Bots', 'Blocks: ' + blocks]
        var text = textList[Math.floor(Math.random() * textList.length)];
        client.user.setActivity(text , { type: 'WATCHING' })
    },60000) // milliseconds
});
Ad
source: stackoverflow.com
Ad