Reputation: 1
I'm trying to do a discordBOT using Java and JDA. I've tried to work with them for several hours now and I don't get it to work. My Bot needs to process data which depends on the user. In JDA's event Handlers you cannot return any data types since they extend the ListenerAdapter. Its my first time working with Eventlisteners and I already googled a lot.
Upvotes: 0
Views: 1739
Reputation: 91
Create a ListenerAdapter and associate it to the JDA instance. I'll show you an example code so you can understand:
/**
* Logs the bot into Discord and sets the event listeners.
*/
public static void launchJDA(String botToken) {
try {
jdaInstance = new JDABuilder(AccountType.BOT).setToken(loadToken()).build().awaitReady();
jdaInstance.addEventListener(new EventsManager());
} catch (Exception e) {
e.printStackTrace();
}
}
public class EventsManager extends ListenerAdapter {
@Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
//Do what you want with the event here, for example replying with the message received:
String msg = event.getMessage().getContentDisplay();
event.getChannel().sendMessage(msg).queue();
}
}
Upvotes: 1
Reputation: 842
I'm not sure if this is what you are asking for, but your class needs to extend from ListenerAdapter.
public class yourClass extends ListenerAdapter {
//Your code.
}
Inside a class you can use the method you need for doing what you want. e.g.
@Override
public void onMessageReceived(MessageReceivedEvent event) {
//Your code again.
}
If you want to transfer data between two classes, you can use your own methods and just give the event to it.
[Method1]
public static void yourMethod(MessageReceivedEvent event) {
//Your code.
}
[Method2]
@Override
public void onMessageReceived(MessageReceivedEvent event) {
yourMethod(event);
}
Upvotes: 2