Interface Catnip
- All Superinterfaces:
AutoCloseable
- All Known Implementing Classes:
CatnipImpl
Note that this interface extends AutoCloseable
; this is meant for
cases where some relatively-fast blocking operations are desirable, and a
long-term catnip instance is not needed. An example of this is doing some
quick REST calls when blocking a thread is not an issue. Another possible
use-case is (ab)using the built-in TaskScheduler
for one reason or
another, although creating an entire catnip instance solely for that would
be quite silly.
- Since:
- 9/3/18.
- Author:
- amy
-
Method Summary
Modifier and TypeMethodDescriptiondefault EntityCache
cache()
default EntityCacheWorker
The cache worker being used by this catnip instance.static Catnip
catnip
(CatnipOptions options) Create a new catnip instance with the given options.static Catnip
Create a new catnip instance with the given token.static io.reactivex.rxjava3.core.Single<Catnip>
catnipAsync
(CatnipOptions options) Create a new catnip instance with the given options.static io.reactivex.rxjava3.core.Single<Catnip>
catnipAsync
(String token) Create a new catnip instance with the given token.default void
chunkMembers
(long guildId) Request all guild members for the given guild.default void
chunkMembers
(long guildId, int limit) Request guild members for the given guild.default void
chunkMembers
(long guildId, String query) Request guild members for the given guild.default void
chunkMembers
(long guildId, String query, int limit) Request guild members for the given guild.default void
chunkMembers
(String guildId) Request all guild members for the given guild.default void
chunkMembers
(String guildId, int limit) Request guild members for the given guild.default void
chunkMembers
(String guildId, int limit, String nonce) Request guild members for the given guild.default void
chunkMembers
(String guildId, String query) Request guild members for the given guild.default void
chunkMembers
(String guildId, String nonce, boolean _useless) Request guild members for the given guild.void
chunkMembers
(String guildId, String query, int limit, String nonce) Request guild members for the given guild.default void
chunkMembers
(String guildId, String query, String nonce) Request guild members for the given guild.clientId()
The ID of this clientlong
The ID of this client, as a long.default void
close()
void
closeVoiceConnection
(long guildId) Closes the voice connection on the specified guild.void
closeVoiceConnection
(String guildId) Closes the voice connection on the specified guild.connect()
Start all shards asynchronously.default DispatchManager
Handles dispatching and listening to events.The entity builder used by this catnip instance.default EntitySerializer<?>
default EventBuffer
default <T extends Extension>
TReturn a single extension by class.io.reactivex.rxjava3.core.Single<GatewayInfo>
Fetches the gateway info and updates the cache.default <T,
E> io.reactivex.rxjava3.core.Flowable<org.apache.commons.lang3.tuple.Pair<T, E>> flowable
(DoubleEventType<T, E> type) Add a reactive stream handler for events of the given type.default <T> io.reactivex.rxjava3.core.Flowable<T>
Add a reactive stream handler for events of the given type.default void
game
(String game, Presence.ActivityType type, String url) Update the activity status for all shards.injectOptions
(Extension extension, UnaryOperator<CatnipOptions> optionsPatcher) Inject options into this catnip instance from the given extension.boolean
isUnavailable
(String guildId) loadExtension
(Extension extension) Load an extension for this catnip instance.default LogAdapter
The logging adapter.default <T,
E> io.reactivex.rxjava3.core.Observable<org.apache.commons.lang3.tuple.Pair<T, E>> observable
(DoubleEventType<T, E> type) Add a reactive stream handler for events of the given type.default <T> io.reactivex.rxjava3.core.Observable<T>
observable
(EventType<T> type) Add a reactive stream handler for events of the given type.default void
openVoiceConnection
(long guildId, long channelId) Opens a voice connection to the provided guild and channel.default void
openVoiceConnection
(long guildId, long channelId, boolean selfMute, boolean selfDeaf) Opens a voice connection to the provided guild and channel.default void
openVoiceConnection
(String guildId, String channelId) Opens a voice connection to the provided guild and channel.void
openVoiceConnection
(String guildId, String channelId, boolean selfMute, boolean selfDeaf) Opens a voice connection to the provided guild and channel.options()
static long
parseIdFromToken
(String token) Parses a token and returns the client id encoded therein.default io.reactivex.rxjava3.core.Single<Webhook>
parseWebhook
(String webhookUrl) Get a webhook object for the specified webhook URL.default io.reactivex.rxjava3.core.Single<Webhook>
parseWebhook
(String id, String token) Get a webhook object for the specified webhook URL.presence
(int shardId) Get the presence for the specified shard.void
Update the presence for all shards.void
presence
(Presence.OnlineStatus status, String game, Presence.ActivityType type, String url) Update the presence for all shards by specifying each part of the presence individually.void
Update the presence for a specific shard.rest()
default io.reactivex.rxjava3.core.Scheduler
io.reactivex.rxjava3.core.Maybe<User>
selfUser()
default SessionManager
default ShardManager
void
shutdown()
Shutdown the catnip instance, and undeploy all shards.default void
status
(Presence.OnlineStatus status) Update the online status for all shards.default TaskScheduler
The task scheduler allows for scheduling one-off and recurring tasks that are executed at some point in the future.default boolean
validateSignature
(String signature, String ts, String data) Validates an ed25519 signature for interactions.
-
Method Details
-
catnip
Create a new catnip instance with the given token.This method may block while validating the provided token.
- Parameters:
token
- The token to be used for all API operations.- Returns:
- A new catnip instance.
-
catnipAsync
Create a new catnip instance with the given token.This method may block while validating the provided token.
- Parameters:
token
- The token to be used for all API operations.- Returns:
- A new catnip instance.
-
catnip
Create a new catnip instance with the given options.This method may block while validating the provided token.
- Parameters:
options
- The options to be applied to the catnip instance.- Returns:
- A new catnip instance.
-
catnipAsync
Create a new catnip instance with the given options.This method may block while validating the provided token.
- Parameters:
options
- The options to be applied to the catnip instance.- Returns:
- A new catnip instance.
-
parseIdFromToken
Parses a token and returns the client id encoded therein. Throws aIllegalArgumentException
if the provided token is not well-formed.
See the following image for an explanation of the Discord token format:
- Parameters:
token
- The token to parse.- Returns:
- The client id encoded in the token.
- Throws:
IllegalArgumentException
- If the provided token is not well-formed.
-
options
- Returns:
- An immutable view of the current instance's options.
-
gatewayInfo
- Returns:
- The cached gateway info. May be null if it hasn't been fetched yet.
-
fetchGatewayInfo
Fetches the gateway info and updates the cache. Calls made togatewayInfo()
after this stage completes successfully are guaranteed to return a non null value.Updates the cached gateway info.
- Returns:
- The gateway info fetched from discord.
-
connect
Start all shards asynchronously. To customize the shard spawning / management strategy, seeCatnipOptions
.- Returns:
- Itself.
-
rxScheduler
@Nonnull @CheckReturnValue default io.reactivex.rxjava3.core.Scheduler rxScheduler() -
dispatchManager
Handles dispatching and listening to events.- Returns:
- The current dispatch manager instance.
-
shardManager
- Returns:
- The shard manager being used by this catnip instance.
-
sessionManager
- Returns:
- The session manager being used by this catnip instance.
-
eventBuffer
- Returns:
- The event buffer being used by this catnip instance.
-
rest
- Returns:
- The REST API instance for this catnip instance.
-
logAdapter
The logging adapter. This is used throughout the lib to log things, and may additionally be used by user code if you don't want to set up your own logging things. The logging adapter is exposed like this because it is possible to specify a custom logging adapter inCatnipOptions
; generally you should just stick with the provided default SLF4J logging adapter.- Returns:
- The logging adapter being used by this catnip instance.
-
cache
- Returns:
- The entity cache being used by this catnip instance.
-
cacheWorker
The cache worker being used by this catnip instance. You should use this if you need to do special caching for some reason.- Returns:
- The cache worker being used by this catnip instance.
-
taskScheduler
The task scheduler allows for scheduling one-off and recurring tasks that are executed at some point in the future. By default, the task scheduler is effectively just a simple wrapper overObservable.timer(long, TimeUnit)
andObservable.interval(long, TimeUnit)
.- Returns:
- The task scheduler used by this catnip instance.
-
extensionManager
- Returns:
- The extension manager being used by this catnip instance.
-
loadExtension
Load an extension for this catnip instance. SeeExtension
for more information.- Parameters:
extension
- The extension to load.- Returns:
- Itself.
-
extension
Return a single extension by class. If multiple extensions are loaded from the same class, there is no guarantee which extension instance will be returned, in which case you should be usingExtensionManager.matchingExtensions(Class)
.- Type Parameters:
T
- Type of the extension.- Parameters:
extensionClass
- The extension class to find instances of- Returns:
- A possibly-
null
instance of the passed extension class.
-
injectOptions
@Nonnull Catnip injectOptions(@Nonnull Extension extension, @Nonnull UnaryOperator<CatnipOptions> optionsPatcher) Inject options into this catnip instance from the given extension. This allows extensions to do things like automatically register a new cache worker without having to tell the end-user to specify options. By default, options that get injected will be logged.- Parameters:
extension
- The extension injecting the options.optionsPatcher
- Function responsible for updating the settings.- Returns:
- Itself.
- Throws:
IllegalArgumentException
- When the given extension isn't loaded.
-
selfUser
- Returns:
- The currently-logged-in user. May be
null
if no shards have logged in.
-
clientId
String clientId()The ID of this client- Returns:
- The ID of this client.
-
clientIdAsLong
long clientIdAsLong()The ID of this client, as a long.- Returns:
- The ID of the client, as a long.
-
entitySerializer
- Returns:
- The entity serializer that catnip uses for converting entities into an external-friendly format.
-
openVoiceConnection
Opens a voice connection to the provided guild and channel. The connection is opened asynchronously, withVOICE_STATE_UPDATE
andVOICE_SERVER_UPDATE
events being fired when the connection is opened.- Parameters:
guildId
- Guild to connect.channelId
- Channel to connect.
-
openVoiceConnection
void openVoiceConnection(@Nonnull String guildId, @Nonnull String channelId, boolean selfMute, boolean selfDeaf) Opens a voice connection to the provided guild and channel. The connection is opened asynchronously, withVOICE_STATE_UPDATE
andVOICE_SERVER_UPDATE
events being fired when the connection is opened.- Parameters:
guildId
- Guild to connect.channelId
- Channel to connect.selfMute
- Whether or not to connect as muted.selfDeaf
- Whether or not to connect as deafened.
-
openVoiceConnection
default void openVoiceConnection(long guildId, long channelId) Opens a voice connection to the provided guild and channel. The connection is opened asynchronously, withVOICE_STATE_UPDATE
andVOICE_SERVER_UPDATE
events being fired when the connection is opened.- Parameters:
guildId
- Guild to connect.channelId
- Channel to connect.
-
openVoiceConnection
default void openVoiceConnection(long guildId, long channelId, boolean selfMute, boolean selfDeaf) Opens a voice connection to the provided guild and channel. The connection is opened asynchronously, withVOICE_STATE_UPDATE
andVOICE_SERVER_UPDATE
events being fired when the connection is opened.- Parameters:
guildId
- Guild to connect.channelId
- Channel to connect.selfMute
- Whether or not to connect as muted.selfDeaf
- Whether or not to connect as deafened.
-
closeVoiceConnection
Closes the voice connection on the specified guild.- Parameters:
guildId
- Guild to disconnect.
-
closeVoiceConnection
void closeVoiceConnection(long guildId) Closes the voice connection on the specified guild.- Parameters:
guildId
- Guild to disconnect.
-
chunkMembers
default void chunkMembers(long guildId) Request all guild members for the given guild.- Parameters:
guildId
- Guild to request for.
-
chunkMembers
Request all guild members for the given guild.- Parameters:
guildId
- Guild to request for.
-
chunkMembers
Request guild members for the given guild.- Parameters:
guildId
- Guild to request for.query
- Member names must start with this.
-
chunkMembers
Request guild members for the given guild.- Parameters:
guildId
- Guild to request for.query
- Member names must start with this.
-
chunkMembers
default void chunkMembers(long guildId, @Nonnegative int limit) Request guild members for the given guild.- Parameters:
guildId
- Guild to request for.limit
- Maximum number of members to return. 0 for no limit.
-
chunkMembers
Request guild members for the given guild.- Parameters:
guildId
- Guild to request for.limit
- Maximum number of members to return. 0 for no limit.
-
chunkMembers
Request guild members for the given guild.- Parameters:
guildId
- Guild to request for.query
- Members returned must have a username starting with this.limit
- Maximum number of members to return. 0 for no limit.
-
chunkMembers
Request guild members for the given guild.- Parameters:
guildId
- Guild to request for.nonce
- Nonce to use for knowing which chunks came from which request._useless
- Differentiates this method fromchunkMembers(String, String)
. Otherwise useless.
-
chunkMembers
Request guild members for the given guild.- Parameters:
guildId
- Guild to request for.query
- Members returned must have a username starting with this.nonce
- Nonce to use for knowing which chunks came from which request.
-
chunkMembers
Request guild members for the given guild.- Parameters:
guildId
- Guild to request for.limit
- Maximum number of members to return. 0 for no limit.nonce
- Nonce to use for knowing which chunks came from which request.
-
chunkMembers
void chunkMembers(@Nonnull String guildId, @Nonnull String query, @Nonnegative int limit, @Nullable String nonce) Request guild members for the given guild.- Parameters:
guildId
- Guild to request for.query
- Members returned must have a username starting with this.limit
- Maximum number of members to return. 0 for no limit.nonce
- Nonce to use for knowing which chunks came from which request.
-
presence
Get the presence for the specified shard.- Parameters:
shardId
- The shard id to get presence for.- Returns:
- The shard's presence.
-
presence
Update the presence for all shards.- Parameters:
presence
- The new presence to set.
-
presence
Update the presence for a specific shard.- Parameters:
presence
- The new presence to set.shardId
- The shard to set presence for.
-
presence
void presence(@Nullable Presence.OnlineStatus status, @Nullable String game, @Nullable Presence.ActivityType type, @Nullable String url) Update the presence for all shards by specifying each part of the presence individually.- Parameters:
status
- The new online status. Set tonull
for online.game
- The new game name. Set tonull
to clear.type
- The type of the new game status. Set tonull
for "playing."url
- The new URL for the presence. Will be ignored iftype
is notPresence.ActivityType.STREAMING
.
-
status
Update the online status for all shards. Will clear the activity status.- Parameters:
status
- The new online status to set.
-
game
Update the activity status for all shards. Will set the online status toPresence.OnlineStatus.ONLINE
- Parameters:
game
- The new game to set.type
- The type of the activity.url
- The URL if streaming. Will be ignored iftype
is notPresence.ActivityType.STREAMING
.
-
observable
Add a reactive stream handler for events of the given type. Can be disposed of withObservable.unsubscribeOn(Scheduler)
. Thescheduler
argument can be created withrxScheduler()
.This method automatically subscribes on
rxScheduler()
.- Type Parameters:
T
- The object type of the event being streamed.- Parameters:
type
- The type of event to stream.- Returns:
- The observable.
-
flowable
Add a reactive stream handler for events of the given type. Can be disposed of withFlowable.unsubscribeOn(Scheduler)
. Thescheduler
argument can be created withrxScheduler()
.This method automatically subscribes on
rxScheduler()
.- Type Parameters:
T
- The object type of the event being streamed.- Parameters:
type
- The type of event to stream.- Returns:
- The flowable.
-
observable
default <T,E> io.reactivex.rxjava3.core.Observable<org.apache.commons.lang3.tuple.Pair<T,E>> observable(@Nonnull DoubleEventType<T, E> type) Add a reactive stream handler for events of the given type. Can be disposed of withObservable.unsubscribeOn(Scheduler)
. Thescheduler
argument can be created withrxScheduler()
.This method automatically subscribes on
rxScheduler()
.- Type Parameters:
T
- The object type of the event being streamed.E
- The object type of the event being streamed.- Parameters:
type
- The type of event to stream.- Returns:
- The observable.
-
flowable
default <T,E> io.reactivex.rxjava3.core.Flowable<org.apache.commons.lang3.tuple.Pair<T,E>> flowable(@Nonnull DoubleEventType<T, E> type) Add a reactive stream handler for events of the given type. Can be disposed of withFlowable.unsubscribeOn(Scheduler)
. Thescheduler
argument can be created withrxScheduler()
.This method automatically subscribes on
rxScheduler()
.- Type Parameters:
T
- The object type of the event being streamed.E
- The object type of the event being streamed.- Parameters:
type
- The type of event to stream.- Returns:
- The flowable.
-
shutdown
void shutdown()Shutdown the catnip instance, and undeploy all shards. -
parseWebhook
Get a webhook object for the specified webhook URL. This method will attempt to validate the webhook.- Parameters:
webhookUrl
- The URL of the webhook.- Returns:
- A Single that completes when the webhook is validated.
-
parseWebhook
Get a webhook object for the specified webhook URL. This method will attempt to validate the webhook.- Parameters:
id
- The webhook's id.token
- The webhook's token.- Returns:
- A Single that completes when the webhook is validated.
-
close
default void close()- Specified by:
close
in interfaceAutoCloseable
-
validateSignature
default boolean validateSignature(@Nonnull String signature, @Nonnull String ts, @Nonnull String data) Validates an ed25519 signature for interactions.- Parameters:
signature
- The signature to validate. This is thex-signature-ed25519
header.ts
- The timestamp of the request. This is thex-signature-timestamp
heaqder.data
- The data to validate. This is the body of the request.- Returns:
- Whether or not the signature is valid.
-
entityBuilder
EntityBuilder entityBuilder()The entity builder used by this catnip instance. This is exposed publicly so that, if necessary, it can be used to construct entities from JSON objects as needed.- Returns:
- This catnip instance's
EntityBuilder
.
-