TheHolyWaffle / TeamSpeak-3-Java-API

A Java wrapper of TeamSpeak's 3 server query API.
MIT License
306 stars 107 forks source link

Get Connection State #301

Closed Luuuuuis closed 5 years ago

Luuuuuis commented 5 years ago

Hi, So when I connect in the normal way but the credentials are wrong I get a Log error (no java error but slf4j log). Fine. And now I wanna know whether the bot is logged in or if there was an error. How do I do that?

I could provide your source if needed. Looking forward hearing from you ;) ~ Luis

rogermb commented 5 years ago

Heyo!

This API uses exceptions to notify you when something went wrong. For example,

api.login("doesn't", "exist");

would throw a TS3CommandFailedException. If this exception isn't caught anywhere, it'll usually end up in you log as

Exception in thread "main" com.github.theholywaffle.teamspeak3.api.exception.TS3CommandFailedException: A "login" command returned with a server error.
>> invalid loginname or password (ID 520)
    at com.github.theholywaffle.teamspeak3.api.CommandFuture.checkForFailure(CommandFuture.java:416)
    at com.github.theholywaffle.teamspeak3.api.CommandFuture.getUninterruptibly(CommandFuture.java:357)
    at com.github.theholywaffle.teamspeak3.TS3Api.login(TS3Api.java:3000)
    at Test.main(Test.java:61)

But you can (and are supposed to) catch these exceptions and handle them appropriately:

try {
    api.login("doesn't", "exist");
} catch (TS3Exception ex) {
    System.out.println("Login failed: " + ex.getMessage());
    return;
}

You can get more information about what the query is doing by using the whoami command, i.e.

ServerQueryInfo info = api.whoAmI();

This should tell you the current virtual server ID and channel ID, your current username, and so on.


Also, since you seem to be making something that runs on a Minecraft server - please don't block the main tick thread by waiting for the TS3 server to respond. Use the async API, a different Thread, or a java.util.concurrent.ExecutorService where appropriate 😄

rogermb commented 5 years ago

Seems to be resolved, closed 😃