Skip to content

Instantly share code, notes, and snippets.

@zekroTJA
Created March 5, 2017 11:16
Show Gist options
  • Save zekroTJA/0f06b352e1c336e0d9cc100e7f8d7c89 to your computer and use it in GitHub Desktop.
Save zekroTJA/0f06b352e1c336e0d9cc100e7f8d7c89 to your computer and use it in GitHub Desktop.
JDA Discord Bot Tutorial #02: Listeners
// public class Main() {...
//public static void main(String[] Args) {...}
public static void addListeners() {
builder.addListener(new readyListener());
builder.addListener(new voiceListener());
}
// }
package listeners;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.events.ReadyEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
/**
* © zekro 2017
*
* @author zekro
*/
public class readyListener extends ListenerAdapter {
public void onReady(ReadyEvent event) {
String out = "\nThis bot is running on following servers: \n";
for (Guild g : event.getJDA().getGuilds() ) {
out += g.getName() + " (" + g.getId() + ") \n";
}
System.out.println(out);
for (Guild g : event.getJDA().getGuilds() ) {
g.getTextChannels().get(0).sendMessage(
"Hey guys! Im back again!"
).queue();
}
}
}
package listeners;
import net.dv8tion.jda.core.events.guild.voice.GuildVoiceJoinEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
/**
* © zekro 2017
*
* @author zekro
*/
public class voiceListener extends ListenerAdapter {
public void onGuildVoiceJoin(GuildVoiceJoinEvent event) {
event.getGuild().getTextChannelsByName("voicelog", true).get(0).sendMessage(
"Member " + event.getVoiceState().getMember().getUser().getName() + " joined voice channel " + event.getChannelJoined().getName() + "."
).queue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment