diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 8066a3c..5780e19 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -3,6 +3,8 @@ package wtf.beatrice.hidekobot; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.entities.Activity; +import net.dv8tion.jda.api.requests.GatewayIntent; +import wtf.beatrice.hidekobot.listeners.MessageListener; import wtf.beatrice.hidekobot.utils.Logger; import javax.security.auth.login.LoginException; @@ -38,8 +40,13 @@ public class HidekoBot // try to create the bot object and authenticate it with discord. jdaBuilder = JDABuilder.createDefault(botToken); jdaBuilder.setActivity(Activity.playing("the piano")); - jda = jdaBuilder.build(); - } catch (LoginException e) + + jdaBuilder.enableIntents(GatewayIntent.MESSAGE_CONTENT, + GatewayIntent.DIRECT_MESSAGES, + GatewayIntent.GUILD_MESSAGES); + + jda = jdaBuilder.build().awaitReady(); + } catch (LoginException | InterruptedException e) { logger.log(e.getMessage()); // print the error message, omit the stack trace. return; // if we failed connecting and authenticating, then quit. @@ -55,6 +62,10 @@ public class HidekoBot // log the invite-link to console so noob users can just click on it. logger.log("Bot User ID: " + botUserId, 4); logger.log("Invite Link: " + standardInviteLink, 5); + + // register listeners + jda.addEventListener(new MessageListener()); + } } diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java new file mode 100644 index 0000000..edd18d8 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java @@ -0,0 +1,24 @@ +package wtf.beatrice.hidekobot.listeners; + +import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.jetbrains.annotations.NotNull; +import wtf.beatrice.hidekobot.utils.Logger; + +public class MessageListener extends ListenerAdapter +{ + + private final Logger logger = new Logger(MessageListener.class); + + @Override + public void onMessageReceived(@NotNull MessageReceivedEvent event) + { + if(event.getMessage().getContentDisplay().equalsIgnoreCase("ping")) + { + MessageChannel channel = event.getChannel(); + channel.sendMessage("Pong!").queue(); + } + } + +}