Reference
Preamble
You should be somewhat familiar with the Quarkus framework before creating your Quarkus Discord bot. Quarkus has extensive documentation at https://quarkus.io/guides to help you get started.
Additionally, the Discord4J documentation and the Discord API Gateway event documentation are essential resources to have on hand.
Discord4J uses Project Reactor as its reactive framework instead of Mutiny, the reactive layer most Quarkus users will be familiar with. For this reason, reactive listener methods support returning Mono instead of Uni and Flux instead of Multi , but you can still convert your Reactor streams into Mutiny streams if you want.
|
The Choosing an Operator guide might be useful to you if you don’t want to convert your streams.
Events
Listening to events
To listen to Gateway events, simply declare a method with an event type parameter and annotate it with @GatewayEvent
. The method can return Mono
, Uni
, Flux
, or Multi
.
Here’s an example of an imperative listener method:
class AddReaction {
Mono<Void> onMessageCreate(@GatewayEvent MessageCreateEvent event) {
return event.getMessage().addReaction(ReactionEmoji.of("🤖"));
}
}
A few details about Gateway listener methods:
-
The declaring class and method can be public, protected, or package private
-
The declaring class has to be a concrete class
-
The method should be concrete
-
The annotated event must be the first parameter
Event types
All the currently supported event types can be found by checking out the subclasses of the Event class.
Bean injection
Gateway client
You can inject a GatewayDiscordClient
bean that exposes high level methods to manage Discord resources via REST, access the underlying entity cache, and manage your bot’s connection to the Gateway and Voice Gateway.
This usually isn’t necessary as each Discord4J event exposes the Gateway client via Event#getClient() , and you can manage most resources directly from its corresponding entity instance.
|
An example:
@ApplicationScoped
class MyBean {
@Inject
GatewayDiscordClient gateway;
// do something with gateway
}
Metrics
If your Quarkus app uses a metrics extension like Micrometer or SmallRye Metrics, you can enable metrics collection for your bot exposed at the /q/metrics
endpoint by setting the configuration property quarkus.discord4j.metrics.enabled
to true.
Currently, metrics are collected for the number of servers your bot is in and the number of voice channels your bot is in.
Health check
A readiness health check for your bot is automatically exposed at the /q/health/ready
endpoint if your Quarkus app depends on SmallRye Health. All of your bot’s shards will be checked and the health status will be DOWN
if any shard isn’t connected.
You can disable the health check by setting quarkus.discord4j.health.enabled
to false.
Native executable
You can package your Discord bot into a native executable with GraalVM or Mandrel to reduce its memory footprint and speed up its boot time. See the Building a native executable guide for more information.
Configuration
You can configure the following properties:
Configuration property fixed at build time - All other configuration properties are overridable at runtime
Type |
Default |
|||
---|---|---|---|---|
Whether a health check should be published for the Gateway clients managed by this extension if the quarkus-smallrye-health extension is present Environment variable: |
boolean |
|
||
Whether metrics should be collected for the Gateway clients managed by this extension if the quarkus-micrometer or quarkus-smallrye-metrics extension is present Environment variable: |
boolean |
|
||
Whether to serialize and register the JSON files found in Environment variable: |
boolean |
|
||
The path to the JSON files of the global commands. Environment variable: |
string |
|
||
Whether to delete registered global commands whose JSON representation is not found in Environment variable: |
boolean |
|
||
The bot token used to authenticate to the Discord API. Environment variable: |
string |
required |
||
The status of the bot. Environment variable: |
|
|||
The Gateway intents that should be enabled. Specific Gateway intents are required to receive certain Gateway events. Non-privileged intents will be enabled by default. Environment variable: |
list of Intent |
|||
The strategy to use for retrieving Discord entities. Default is Environment variable: |
EntityRetrievalStrategy |
|||
The number of shards that this bot should be split into. Environment variable: |
int |
|||
Which shards from the configured number of shards that this bot will receive events from. The bot will receive events from all specified shards by default. Environment variable: |
list of int |
|
||
The number of shards that this bot will concurrently identify to the Gateway.
Environment variable: |
int |
|||
Whether to serialize and register the JSON files found in Environment variable: |
boolean |
|
||
The path to the JSON files of the guild commands. By default, the path is Environment variable: |
string |
|||
Whether to delete commands registered in this guild whose JSON representation is not found in Environment variable: |
boolean |
|
||
The ID of the guild. Environment variable: |
long |
|
||
Activity configuration This configuration section is optional |
Type |
Default |
||
The type of activity. Environment variable: |
|
required |
||
The name of the activity. Environment variable: |
string |
required |
||
The YouTube or Twitch URL of the stream, if the activity type is Environment variable: |
string |