• If you need help or want to discuss things, you now can also join us on our Discord Server!
  • A first preview of the unlimited version of SinusBot can be found in the Upcoming Changes thread. A version for Windows will follow, but we don't have a release date, yet.

Solved weird/random occurence with "backend.getChannels()"

Everlike

Well-Known Member
Contributor
Hello,
a few months ago I wrote my script "unused channel deleter", as I think a very useful one, at least for me.
But it´s written really bad, even I don´t know what I did there when I look at the code, when I try to add or fix something.
So, I have a major bug, for some reason, and completely random, it does delete channels from my datebase, I already tried to fix that months ago, sometimes this bug does not appear for some days, sometimes every few hours. I tried to fix this in so many ways, but could never find the real reason.
So I decided to rewrite the script, becouse many users want new cool features I also want to implement.

Howsoever, today I worked on a new function which should get all channels existing on the teamspeak, and filter the blacklisted ones, no problem so far. As I wanted, if I blacklisted some channels via webinterface, they didn´t occure in the output, if I didn´t blacklisted any channels, they occured... at least, some times, and some times, multiple times. And here is the problem!
I (also sometimes called Sherock) made some further tests. I used this simple code:
JavaScript:
        event.on('chat', function(ev) {
        if (ev.text.indexOf("!printDebug") >= 0)    {
            ev.client.chat("+");
            engine.log(allChannelIds());
        }
        });

function allChannelIds() {
    var allChannelIds = [];
    for (i = 0; i < backend.getChannels().length; i++) {
        allChannelIds.push(backend.getChannels()[i].id());
    }
    return allChannelIds;
}

So as you can see, the code just gets all channels existing on the teamspeak right now (backend.getChannels()), creates an list (array) with all channel ids.
With the command !printDebug, I let the bot write this list into the log. and this is the output:
siBug44.JPG
As you can see, the channel-id 44 sometimes occures, zero times, somethimes 2, 3 or 1 time/s. Completely random

siBug128.JPG
Another example with th channel-id 128...
Iam not a 100% sure, but I guess this is/was the bug with my script. In the old version I tried to get all channels, and check if there is a channel in my datebas that does not even exist anymore on the teamspeak, and then delete it. So if the specific channel din´t occur in a list like the one above, my script just deleted it...

Iam not sure if I use the function wrong, if this is a "bug", only fixable by @flyth or maybe Iam just stupid. Howsoever, thanks for reading my story.
Have a great day, Greetings


Everlike
 

Everlike

Well-Known Member
Contributor
Oh my god, I now wrote this whole text, read it, and I just found the problem by myself.
I have to put backend.getChannels() into another variable before, like this:
JavaScript:
function allChannelIds() {
    var allChannelIds = [];
    var allChannels = backend.getChannels();
    for (i = 0; i < allChannels.length; i++) {
        allChannelIds.push(allChannels[i].id());
    }
    return allChannelIds;
}
I really hope someone has the same problem and this post will help him out. Three fk*n months to find the problem... Gosh.
 

flyth

is reticulating splines
Staff member
Developer
Contributor
Hehe, sorry :)
Calls to getChannels() are not guaranteed to return the channels in the same order. So if you're iterating over it and access the entries by their index in another fetched getChannels() object, this will happen.

Also, there is no guarantee that there haven't any channels been added or removed in between your calls, so it is always wise to fetch what you need once (and store it to a variable) and then work with the result.
 
Top