Add basic ping response
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Bea 2022-08-25 23:20:51 +02:00
parent 1cff5890bf
commit 59a63b724a
2 changed files with 37 additions and 2 deletions

View File

@ -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());
}
}

View File

@ -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();
}
}
}