public class GatewayDiscordClient extends Object implements EntityRetriever
GatewayBootstrap
and therefore
tracks state updates from all connected shards.
The following are some of the resources available through this aggregate:
DiscordClient
for direct REST API operations through rest()
.CoreResources
like the RestClient
used to perform API requests.GatewayResources
that configure Gateway stores, actions and coordination among shards.EventDispatcher
publishing events from all participating shards.Constructor and Description |
---|
GatewayDiscordClient(DiscordClient discordClient,
GatewayResources gatewayResources,
MonoProcessor<Void> closeProcessor,
GatewayClientGroup gatewayClientGroup,
VoiceConnectionFactory voiceConnectionFactory,
EntityRetrievalStrategy entityRetrievalStrategy) |
Modifier and Type | Method and Description |
---|---|
Mono<Guild> |
createGuild(Consumer<? super GuildCreateSpec> spec)
Requests to create a guild.
|
Mono<User> |
edit(Consumer<? super UserEditSpec> spec)
Requests to edit this client (i.e., modify the current bot user).
|
Mono<ApplicationInfo> |
getApplicationInfo()
Requests to retrieve the application info.
|
Mono<Channel> |
getChannelById(Snowflake channelId)
Requests to retrieve the channel represented by the supplied ID.
|
CoreResources |
getCoreResources()
Returns the set of resources essential to operate on a
DiscordClient for entity manipulation,
scheduling and API communication, like the RestClient , JacksonResources and
ReactorResources . |
EventDispatcher |
getEventDispatcher()
Distributes events to subscribers.
|
Optional<GatewayClient> |
getGatewayClient(int shardId)
Returns a
GatewayClient instance created by this GatewayDiscordClient , which is associated to
a given shard index. |
GatewayClientGroup |
getGatewayClientGroup()
Returns the
GatewayClientGroup capable of performing operations across all GatewayClient
instances created or managed by this GatewayDiscordClient . |
GatewayResources |
getGatewayResources()
Returns the set of resources essential to build
GatewayClient instances and manage multiple Discord
Gateway connections. |
Mono<Guild> |
getGuildById(Snowflake guildId)
Requests to retrieve the guild represented by the supplied ID.
|
Flux<GuildChannel> |
getGuildChannels(Snowflake guildId)
Requests to retrieve the guild's channels.
|
Mono<GuildEmoji> |
getGuildEmojiById(Snowflake guildId,
Snowflake emojiId)
Requests to retrieve the guild emoji represented by the supplied IDs.
|
Flux<GuildEmoji> |
getGuildEmojis(Snowflake guildId)
Requests to retrieve the guild's emojis.
|
Flux<Member> |
getGuildMembers(Snowflake guildId)
Requests to retrieve the guild's members.
|
Flux<Role> |
getGuildRoles(Snowflake guildId)
Requests to retrieve the guild's roles.
|
Flux<Guild> |
getGuilds()
Requests to retrieve the guilds the current client is in.
|
Mono<Invite> |
getInvite(String inviteCode)
Requests to retrieve an invite.
|
Mono<Member> |
getMemberById(Snowflake guildId,
Snowflake userId)
Requests to retrieve the member represented by the supplied IDs.
|
Mono<Message> |
getMessageById(Snowflake channelId,
Snowflake messageId)
Requests to retrieve the message represented by the supplied IDs.
|
Flux<Region> |
getRegions()
Requests to retrieve the voice regions that are available.
|
RestClient |
getRestClient()
Returns the
RestClient used to execute REST API requests. |
Mono<Role> |
getRoleById(Snowflake guildId,
Snowflake roleId)
Requests to retrieve the role represented by the supplied IDs.
|
Mono<User> |
getSelf()
Requests to retrieve the bot user.
|
Snowflake |
getSelfId()
Gets the bot user's ID.
|
Mono<User> |
getUserById(Snowflake userId)
Requests to retrieve the user represented by the supplied ID.
|
Flux<User> |
getUsers()
Retrieve the currently stored (cached) users.
|
VoiceConnectionFactory |
getVoiceConnectionFactory()
Returns the
VoiceConnectionFactory instance created by this GatewayDiscordClient . |
VoiceConnectionRegistry |
getVoiceConnectionRegistry()
Return the
VoiceConnectionRegistry for this GatewayDiscordClient . |
Mono<Webhook> |
getWebhookById(Snowflake webhookId)
Requests to retrieve the webhook represented by the supplied ID.
|
Mono<Void> |
logout()
Disconnects this
GatewayDiscordClient from Discord upon subscribing. |
<E extends Event> |
on(Class<E> eventClass)
|
<E extends Event,T> |
on(Class<E> eventClass,
Function<E,Publisher<T>> mapper)
|
Mono<Void> |
onDisconnect()
Return a
Mono that signals completion when all GatewayClient instances in this shard group have
disconnected. |
Flux<Member> |
requestMembers(discord4j.discordjson.json.gateway.RequestGuildMembers request)
|
Flux<Member> |
requestMembers(Snowflake guildId)
|
Flux<Member> |
requestMembers(Snowflake guildId,
Set<Snowflake> userIds)
|
DiscordClient |
rest()
Access the parent
DiscordClient capable of performing direct REST API requests and REST entities. |
Mono<Void> |
updatePresence(discord4j.discordjson.json.gateway.StatusUpdate statusUpdate)
Update the bot's
Presence (client status) for every shard in this shard group. |
Mono<Void> |
updatePresence(discord4j.discordjson.json.gateway.StatusUpdate statusUpdate,
int shardId)
Update the bot's
Presence (status) for the given shard index, provided it belongs in this shard group. |
EntityRetriever |
withRetrievalStrategy(EntityRetrievalStrategy retrievalStrategy)
Applies the given strategy to retrieve entities using this
GatewayDiscordClient . |
public GatewayDiscordClient(DiscordClient discordClient, GatewayResources gatewayResources, MonoProcessor<Void> closeProcessor, GatewayClientGroup gatewayClientGroup, VoiceConnectionFactory voiceConnectionFactory, EntityRetrievalStrategy entityRetrievalStrategy)
public DiscordClient rest()
DiscordClient
capable of performing direct REST API requests and REST entities.DiscordClient
that created this GatewayDiscordClient
public CoreResources getCoreResources()
DiscordClient
for entity manipulation,
scheduling and API communication, like the RestClient
, JacksonResources
and
ReactorResources
.RestResources
for the parent DiscordClient
public GatewayResources getGatewayResources()
GatewayClient
instances and manage multiple Discord
Gateway connections.GatewayResources
tied to this GatewayDiscordClient
public EventDispatcher getEventDispatcher()
EventDispatcher
is capable of distributing
events from all GatewayClient
connections (shards) that were specified when this
GatewayDiscordClient
was created.EventDispatcher
tied to this GatewayDiscordClient
public Optional<GatewayClient> getGatewayClient(int shardId)
GatewayClient
instance created by this GatewayDiscordClient
, which is associated to
a given shard index.shardId
- the shard index used to get the client instanceGatewayClient
instance represented by the given shard, if presentpublic GatewayClientGroup getGatewayClientGroup()
GatewayClientGroup
capable of performing operations across all GatewayClient
instances created or managed by this GatewayDiscordClient
.GatewayClientGroup
to aggregate gateway operationspublic VoiceConnectionFactory getVoiceConnectionFactory()
VoiceConnectionFactory
instance created by this GatewayDiscordClient
.VoiceConnectionFactory
instance capable of initiating voice connectionspublic VoiceConnectionRegistry getVoiceConnectionRegistry()
VoiceConnectionRegistry
for this GatewayDiscordClient
. This allows you to retrieve
currently registered VoiceConnection
instances.VoiceConnectionRegistry
for voice connectionspublic RestClient getRestClient()
RestClient
used to execute REST API requests.RestClient
tied to this Gateway client.public Mono<Webhook> getWebhookById(Snowflake webhookId)
public Mono<ApplicationInfo> getApplicationInfo()
Mono
where, upon successful completion, emits the application info
. If
an error is received, it is emitted through the Mono
.public Flux<Region> getRegions()
public Snowflake getSelfId()
public Mono<Guild> createGuild(Consumer<? super GuildCreateSpec> spec)
spec
- A Consumer
that provides a "blank" GuildCreateSpec
to be operated on.Mono
where, upon successful completion, emits the created Guild
. If an error is
received, it is emitted through the Mono
.public Mono<Void> updatePresence(discord4j.discordjson.json.gateway.StatusUpdate statusUpdate)
Presence
(client status) for every shard in this shard group.
Factories exist to build an StatusUpdate
object to update the bot's status:
Presence.online()
and Presence.online(ActivityUpdateRequest)
Presence.idle()
and Presence.idle(ActivityUpdateRequest)
Presence.doNotDisturb()
and Presence.doNotDisturb(ActivityUpdateRequest)
Presence.invisible()
Factories exist to build an ActivityUpdateRequest
object for StatusUpdate
:
statusUpdate
- The updated client status.Mono
that signals completion upon successful update. If an error is received, it is emitted
through the Mono
.public Mono<Void> updatePresence(discord4j.discordjson.json.gateway.StatusUpdate statusUpdate, int shardId)
Presence
(status) for the given shard index, provided it belongs in this shard group.
Factories exist to build an StatusUpdate
object to update the bot's status:
Presence.online()
and Presence.online(ActivityUpdateRequest)
Presence.idle()
and Presence.idle(ActivityUpdateRequest)
Presence.doNotDisturb()
and Presence.doNotDisturb(ActivityUpdateRequest)
Presence.invisible()
Factories exist to build an ActivityUpdateRequest
object for StatusUpdate
:
statusUpdate
- The updated client presence.Mono
that signals completion upon successful update. If an error is received, it is emitted
through the Mono
.public Mono<User> edit(Consumer<? super UserEditSpec> spec)
spec
- A Consumer
that provides a "blank" UserEditSpec
to be operated on.Mono
where, upon successful completion, emits the edited User
. If an error is received,
it is emitted through the Mono
.public Mono<Void> logout()
GatewayDiscordClient
from Discord upon subscribing. All GatewayClient
instances in this shard group will attempt to close their current Gateway session and complete this
Mono
after all of them have disconnected.Mono
that upon subscription, will disconnect each Gateway connection established by this
GatewayDiscordClient
and complete after all of them have closed.public Mono<Void> onDisconnect()
Mono
that signals completion when all GatewayClient
instances in this shard group have
disconnected.Mono
that will complete once all GatewayClient
instances in this shard group have
disconnected.public <E extends Event> Flux<E> on(Class<E> eventClass)
Flux
with elements of the given Event
type.
Note: Errors occurring while processing events will terminate your sequence. If you wish to use
a version capable of handling errors for you, use on(Class, Function)
. See
Reactive Streams Spec
explaining this behavior.
A recommended pattern to use this method is wrapping your code that may throw exceptions within a flatMap
block and use Mono.onErrorResume(Function)
, Flux.onErrorResume(Function)
or
equivalent methods to maintain the sequence active:
client.on(MessageCreateEvent.class) .flatMap(event -> myCodeThatMightThrow(event) .onErrorResume(error -> { // log and then discard the error to keep the sequence alive log.error("Failed to handle event!", error); return Mono.empty(); })) .subscribe();
For more alternatives to handling errors, please see Error Handling wiki page.
E
- the type of the event classeventClass
- the event class to obtain events fromFlux
with the requested eventspublic <E extends Event,T> Flux<T> on(Class<E> eventClass, Function<E,Publisher<T>> mapper)
Flux
with elements of the given Event
type, processing them through a given
Function
. Errors occurring within the mapper will be logged and discarded, preventing the termination of
the "infinite" event sequence.
There are multiple ways of using this event handling method, for example:
client.on(MessageCreateEvent.class, event -> { // myCodeThatMightThrow should return a Reactor type (Mono or Flux) return myCodeThatMightThrow(event); }) .subscribe(); client.on(MessageCreateEvent.class, event -> { // myCodeThatMightThrow *can* be blocking myCodeThatMightThrow(event); return Mono.empty(); // but we have to return a Reactor type }) .subscribe();
Continuing the chain after on(class, event -> ...)
will require your own error handling strategy.
Check the docs for on(Class)
for more details.
E
- the type of the event classT
- the type of the event mapper functioneventClass
- the event class to obtain events frommapper
- an event mapping function called on each event. If you do not wish to perform further operations
you can return Mono.empty()
.Flux
with the type resulting from the given event mapperpublic Flux<Member> requestMembers(Snowflake guildId)
members
from the given guildId
using the current Gateway connection.
This method performs a check to validate whether the given guild's data can be obtained from this
GatewayDiscordClient
.public Flux<Member> requestMembers(Snowflake guildId, Set<Snowflake> userIds)
members
from the given guildId
using the current Gateway connection.
This method performs a check to validate whether the given guild's data can be obtained from this
GatewayDiscordClient
.public Flux<Member> requestMembers(discord4j.discordjson.json.gateway.RequestGuildMembers request)
RequestGuildMembers
payload using the current Gateway connection and wait for its completion,
delivering Member
elements asynchronously through a Flux
. This method performs a check to
validate whether the given guild's data can be obtained from this GatewayDiscordClient
.public EntityRetriever withRetrievalStrategy(EntityRetrievalStrategy retrievalStrategy)
GatewayDiscordClient
.retrievalStrategy
- the strategy to applypublic Mono<Channel> getChannelById(Snowflake channelId)
EntityRetriever
getChannelById
in interface EntityRetriever
channelId
- The ID of the channel.Mono
where, upon successful completion, emits the Channel
as represented by the
supplied ID. If an error is received, it is emitted through the Mono
.public Mono<Guild> getGuildById(Snowflake guildId)
EntityRetriever
getGuildById
in interface EntityRetriever
guildId
- The ID of the guild.Mono
where, upon successful completion, emits the Guild
as represented by the supplied
ID. If an error is received, it is emitted through the Mono
.public Mono<GuildEmoji> getGuildEmojiById(Snowflake guildId, Snowflake emojiId)
EntityRetriever
getGuildEmojiById
in interface EntityRetriever
guildId
- The ID of the guild.emojiId
- The ID of the emoji.Mono
where, upon successful completion, emits the GuildEmoji
as represented by the
supplied IDs. If an error is received, it is emitted through the Mono
.public Mono<Member> getMemberById(Snowflake guildId, Snowflake userId)
EntityRetriever
getMemberById
in interface EntityRetriever
guildId
- The ID of the guild.userId
- The ID of the user.Mono
where, upon successful completion, emits the Member
as represented by the supplied
IDs. If an error is received, it is emitted through the Mono
.public Mono<Message> getMessageById(Snowflake channelId, Snowflake messageId)
EntityRetriever
getMessageById
in interface EntityRetriever
channelId
- The ID of the channel.messageId
- The ID of the message.Mono
where, upon successful completion, emits the Message
as represented by the
supplied IDs. If an error is received, it is emitted through the Mono
.public Mono<Role> getRoleById(Snowflake guildId, Snowflake roleId)
EntityRetriever
getRoleById
in interface EntityRetriever
guildId
- The ID of the guild.roleId
- The ID of the role.Mono
where, upon successful completion, emits the Role
as represented by the supplied
IDs. If an error is received, it is emitted through the Mono
.public Mono<User> getUserById(Snowflake userId)
EntityRetriever
getUserById
in interface EntityRetriever
userId
- The ID of the user.Mono
where, upon successful completion, emits the User
as represented by the supplied
ID. If an error is received, it is emitted through the Mono
.public Flux<Guild> getGuilds()
EntityRetriever
getGuilds
in interface EntityRetriever
Flux
that continually emits the guilds
that the current client is in. If an error
is received, it is emitted through the Flux
.public Mono<User> getSelf()
EntityRetriever
getSelf
in interface EntityRetriever
Mono
where, upon successful completion, emits the bot user
. If an error is
received, it is emitted through the Mono
.public Flux<Member> getGuildMembers(Snowflake guildId)
EntityRetriever
getGuildMembers
in interface EntityRetriever
guildId
- the ID of the guild.Flux
that continually emits the members
of the guild. If an error is received,
it is emitted through the Flux
.public Flux<GuildChannel> getGuildChannels(Snowflake guildId)
EntityRetriever
The order of items emitted by the returned Flux
is unspecified. Use
OrderUtil.orderGuildChannels(Flux)
to consistently order channels.
getGuildChannels
in interface EntityRetriever
guildId
- the ID of the guild.Flux
that continually emits the guild's channels
. If an error is received,
it is emitted through the Flux
.public Flux<Role> getGuildRoles(Snowflake guildId)
EntityRetriever
The order of items emitted by the returned Flux
is unspecified. Use OrderUtil.orderRoles(Flux)
to consistently order roles.
getGuildRoles
in interface EntityRetriever
Flux
that continually emits the guild's roles
. If an error is received, it is
emitted through the Flux
.public Flux<GuildEmoji> getGuildEmojis(Snowflake guildId)
EntityRetriever
getGuildEmojis
in interface EntityRetriever
Flux
that continually emits the guild's emojis
. If an error is received,
it is emitted through the Flux
.