Making your first bot is very simple. This tutorial will walk you through how to make a basic bot that responds to a ping command and listens to some other events.
The first thing you need to do to actually create the bot is to construct the client. This is done using
DiscordClientBuilder.create or directly through
- DiscordClientBuilder Javadocs
- DiscordClient Javadocs
- GatewayBootstrap Javadocs
- GatewayDiscordClient Javadocs
This is the very least that you need to be able to create a bot and login to show the bot as online.
You do not need to call block on
onDisconnect, but if you do not have a non daemon thread running your program will instantly exit. Only call subscribe for login if you know what you're doing.
Next let's listen to a couple of events. First let's listen to the ReadyEvent to figure out when we first originally connect to the gateway.
This will listen to when we connect to the gateway and when
ReadyEvent fires. This'll then print out the name and discriminator of the bot that is signing in. Next we'll figure out how to listen for a
!ping command from only users and respond with a pong!
Now let's look back on this, there is a lot going on here so let's walk through it.
This listens for all
MessageCreateEvents that come in to the bot.
This turns all
MessageCreate events into the messages that were sent. The
:: syntax is just shorthand for
map(event -> event.getMessage()).
.filter(message -> message.getAuthor().map(user -> !user.isBot()).orElse(false))
This filters out all members that are bots, so that we only get events from users.
.filter(message -> message.getContent().equalsIgnoreCase("!ping"))
This filters out any messages that don't equal the content
This turns it into the channel the message came from.
.flatMap(channel -> channel.createMessage("Pong!"))
This creates the message with the content
Telling it to execute!
Now you have a basic bot up and running, you can continue to build on it. Including making your own system so all events aren't in the same class where you construct the client.