-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
##Creating the Instance Harmony allows 3 easy ways to create an instance. The first is to provide it directly with the email and password to log into discord
Harmony harmony = new Harmony("email", "password");It is also possible to supply Harmony with only a password to connect however the data file must contain the email
Harmony harmony = new Harmony("password");The final way is to have Harmony read in the password and email from its data file. This is not recommended as both the email and password are stored in clear text to allow the user to actually update them as needed.
Harmony harmony = new Harmony();##Data File
While not strictly required for Harmony to function Harmony will always create a data file. By default this file will be called auth.json and is only used for storing the email and an encrypted version of the token used to actually connect to everything. This token is stored so that Harmony does not have to spam login every time something using the library tries to log in. The password can also be stored and Harmony will use it if needed. To set a custom data file simply do
harmony.setDataFile("file");or
harmony.setDataFile(new File("file"));both of these will have the same effect and passing null in as the argument will reset Harmony to use the default data file.
If you want to add an email and/or password to the Data file it would look like this
{
"email": "email_here",
"password": "password_here"
}
##Adding Event Handlers
Events are the main way Harmony communicates with the application and while there are two different modes that throw different events or events at different times they are all setup the same basic ways. The first is to use a whole object with the @EventHandler annotation on the methods that handle the events.
public class Handler {
@EventHandler
public void handler(Event event) {
. . .
}
}with the code
harmony.getEventManager().addListener(new Handler());This method allows for one class to have many different methods that handle many different events. The next is to use Java 8's Lambda functions.
harmony.getEventmanager().addListener((Event event) -> System.out.println(event));which allows for a nice inline for methods that don't need much code. You could also create classes that extend the @FunctionalInterface's and pass those in if desired.
##Connecting Probably the easiest part. After the other setup is done initiating the connection to Discord is as simple as just calling
harmony.connect();and Harmony will begin connecting.
##Disconnecting
After the connect() method is called harmony will return control of the main thread to the application. It is recommended, though not needed, to disconnect when finished using something similar to the code below
Scanner in = new Scanner(System.in);
while(harmony.isConnected()) {
String command = in.nextLine();
if(command.equalsIgnoreCase("logout")) {
harmony.disconnect();
}
}and that's it! Harmony should run until the application is shut down or disconnect is called. Please remember to report any bugs!