• 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.

Feature Request | Client Online

Kamikaze

Well-Known Member
Contributor
I'm not sure but I think I couldn't find any similar.
Would be nice if there is a function like Client.isOnline() which returns a boolean.
Yes you can do loopy-loops through the Server and compare client with all online clients, but just a simple function would be nice.

Because as example Client.removeFromServerGroup(StreamerGroup.id()); will throw an error when the Client is offline. With the .isOnline() Function I can loop it and retry removing the group when .isOnline returns "true".
 

DrWarpMan

Well-Known Member
Contributor
Insider
Can you explain where do you need such a thing like this? Like.. some code example or something.
 

Kamikaze

Well-Known Member
Contributor
I'm using this for my Twitch Status Script.
If the Streamer starts streaming, the Bot will give him a Server Group.
When the Stream went offline, the Bot removes the Group from the User. But Twitch API doesnt update frequently, so sometimes it happens that the Streamer disconnect from Teamspeak before the script refreshed or Twitch API refreshed. Then he can't find the client which he want the Srver Group remove from.

So I would do a function which removes/add the server group only when the Client is online, if not it will retry in X minutes and the function get triggered when stream goes online/offline.
 

Multivitamin

Well-Known Member
Tier III
is awesome!
V.I.P.
is uber awesome!
Contributor
Insider
as quick and dirty solution you could just try to do

Code:
if (backend.getClientById(client.id())) {
  //client online
}
 

DrWarpMan

Well-Known Member
Contributor
Insider
as quick and dirty solution you could just try to do

Code:
if (backend.getClientById(client.id())) {
  //client online
}
That is what I was thinking, but if the client is offline, won't client.id() return error like .. can not find property of undefined in the console?
 

DrWarpMan

Well-Known Member
Contributor
Insider
Can you show me the whole code, please?

I tried:

Code:
    event.on("clientMove", ev => {
        if (!ev.toChannel) {
            setTimeout(() => { console.log(ev.client.getIPAddress()) }, 15 * 1000);
        }
    });

and after I disconnect it somehow saved the "client" somewhere and it wrote my IP in the log, even though I was not connected :D
 

Kamikaze

Well-Known Member
Contributor
See above the "TODO"-comments
JavaScript:
    function refreshStatus() {
        engine.log("refreshStatus() starting..")
        TwitchArray.forEach(function(Twitch1) {
            
            var TwitchUser = Twitch1.TwitchUser
            var TsUser = Twitch1.TsUser;
            var Channel = Twitch1.Channel;
            Channel = backend.getChannelByID(Channel);
            var ChannelName = Twitch1.ChannelName;
            var URL = "https://api.twitch.tv/kraken/streams/" + TwitchUser + "?stream_type=live"

            if (TsUser.includes("=")) { // UID
                TsClient = backend.getClientByUID(TsUser);
            } else {
                engine.log("UID not found");
                return;
            }

            debugLog("Getting Data for " + TwitchUser)
            
            http.simpleRequest({
                    method: "GET",
                    url: URL,
                    timeout: 60000,
                    headers: {"Client-ID": "09yqfi2m1ggvcumavhvql1j7mcadql"}
                }, function (error, response) {
                    
                    var data = response.data
                    data = JSON.parse(data)
                    if (!data.stream) {
                        debugLog(TwitchUser + " Status is Offline")
                        if (config.f_SuffixOrPrefix == 0) {
                            Channel.setName(ChannelName + config.g_StatusFormatOffline)
                        } else {
                            ChannelName = ChannelName.split("]");
                            Channel.setName(ChannelName[0] + "]" + config.g_StatusFormatOffline + ChannelName[1])
                        }
                        TsClient.removeFromServerGroup(StreamerGroup.id());
                        //TODO: Check if Client online

                    } else {
                        debugLog(TwitchUser + " Status is Online")
                        if (config.f_SuffixOrPrefix == 0) {
                            Channel.setName(ChannelName + config.h_StatusFormatOnline)
                        } else {
                            ChannelName = ChannelName.split("]");
                            Channel.setName(ChannelName[0] + "]" + config.h_StatusFormatOnline + ChannelName[1])
                        }
                        StreamerGroup.addClientByDatabaseId(TsClient);
                        //TODO: Check if Client online
                    }
                }
            )
        })
    }
 

DrWarpMan

Well-Known Member
Contributor
Insider
Then I would just do something like

store.set(`removeLater ${uid}`, groupID);

Where uid = UID of the client, which in your case is TsUser I guess
And groupID = StreamerGroup.id()

And then I would do an event, clientMove, and upon client connection or something, do:

JavaScript:
----------

let keyNames = store.getKeys();

keyNames.forEach(keyName => {
let removeGroupID = store.get(`removeLater  ${ev.client.uid()}`);

if(removeGroup!= undefined)
{
  ev.client.removeFromServerGroup(removeGroupID);
  store.unset(`removeLater  ${ev.client.uid()}`);
}

});

----------
 

DrWarpMan

Well-Known Member
Contributor
Insider
But I just realized, you have to know whether the client is or is not connected, I would try this:

JavaScript:
try {
  TsClient.removeFromServerGroup(StreamerGroup.id());
} catch
{
  store.set(`removeLater ${uid}`, groupID);
}
 

Kamikaze

Well-Known Member
Contributor
Then I would just do something like

store.set(`removeLater ${uid}`, groupID);

Where uid = UID of the client, which in your case is TsUser I guess
And groupID = StreamerGroup.id()

And then I would do an event, clientMove, and upon client connection or something, do:

JavaScript:
----------

let keyNames = store.getKeys();

keyNames.forEach(keyName => {
let removeGroupID = store.get(`removeLater  ${ev.client.uid()}`);

if(removeGroup!= undefined)
{
  ev.client.removeFromServerGroup(removeGroupID);
  store.unset(`removeLater  ${ev.client.uid()}`);
}

});

----------
Will try it thanks :)
 

Multivitamin

Well-Known Member
Tier III
is awesome!
V.I.P.
is uber awesome!
Contributor
Insider
If I'm not wrong this will throw an error. Because "client" is undefined.
ah i thought you HAD the client object and then he goes offline and then you want to do something
why dont you just do

Code:
const client = backend.getClientByWhatever(whatever)
if (client) {
  //client online
} else {
  //client offline
}
 

DrWarpMan

Well-Known Member
Contributor
Insider
ah i thought you HAD the client object and then he goes offline and then you want to do something
why dont you just do

Code:
const client = backend.getClientByWhatever(whatever)
if (client) {
  //client online
} else {
  //client offline
}
That too, and also, was addClientByDatabaseId added in some kind of recent update? Cause I have never seen it?
 

Tuetchen

Diesmon Dominator
is awesome!
Contributor
Insider
Summoning completed - thanks to @flyth - Its just a missing documentation

3446

Just use that function then when you are not sure if the client is online at that moment
 

Kamikaze

Well-Known Member
Contributor
ah i thought you HAD the client object and then he goes offline and then you want to do something
why dont you just do

Code:
const client = backend.getClientByWhatever(whatever)
if (client) {
  //client online
} else {
  //client offline
}
I'm doing by UID so I get the Client if he is on or offline. But removing/adding only works if he is online :)
 
Top