Search code examples
javascriptnode.jsdynamic-variables

i = 1 How to access channel_number_i?


I'm creating TS3 Query Bot with Node JS library

It should register automatically after joining x channel. I am trying to make possible to let user of bot creating x-number of channels.

I am trying to make a for(;;) loop that contains something like this: (variable is local loop variable that increases once every iteration)

if(channel_i.channelId == event.channel.cid) {...}

Here is code:

register.js:

module.exports = {
    registerUser: function(event, client) 
    {
        var rc = require("../../configs/register_config.js")

        for(i = 1; i<= rc.iloscKanalow; i++){
            console.log("zaczynam loop: " + i)

            if(event.channel.cid == rc.channel_1.channelId) {

                if(!event.client.servergroups.includes(rc.channel_1.groupId)) {
                    var date = new Date(),
                        g = date.getHours(),
                        m = date.getMinutes(),
                        s = date.getSeconds()
                    try{
                        event.client.addGroups(rc.channel_1.groupId)
                    }catch(e){
                        console.log(" [ " + g + " : " + m + " : " + s + " ] " + "register.js: error: " + e.message)
                        event.client.kickFromChannel("ERROR - ZOBACZ LOGI")
                        event.client.poke("Wystąpił błąd, proszę zgłoś go administracji.")
                    }
                    event.client.poke("You have been successfullyn registerned!")
                    event.client.kickFromChannel()
                    console.log(" [ " + g + " : " + m + " : " + s + " ] " +'Zarejestronwalem: ' + client.nickname)
                } else {
                    event.client.poke("You arne already registerned!")
                    event.client.kickFromChannel()
                }

            //koniec ifa
            }

        //koniec fora
        }
    }
}




register_config.js:

var register_config = {

    "iloscKanalow": 1, // <--- it means "number of channels"

    channel_1: {
        "groupId" : 11,
        "channelId": 4
    }

};

module.exports = register_config;

The exact question: How to dynamically access variable that is named

var channel_1 = 5

with variable i = 1 ?

Example: client.addGroups(channel_i.groupId)


Solution

  • Updated

    rc["channel_"+i] where i is the index