Skip to content
Bilal Bassam edited this page Dec 12, 2024 · 42 revisions

extends Emitter

The main point of entry into a Discordia application. All data relevant to Discord is accessible through a client instance or its child objects after a connection to Discord is established with the run method. In other words, client data should not be expected and most client methods should not be called until after the ready event is received. Base emitter methods may be called at any time. See client options.

Constructor

Client(options)

Parameter Type Optional
options table

Properties

Name Type Description
email string/nil The current user's owner's account's email address (user-accounts only).
groupChannels Cache An iterable cache of all group channels that are visible to the client. Only user-accounts should have these.
guilds Cache An iterable cache of all guilds that are visible to the client. Note that the guilds present here correspond to which shards the client is managing. If all shards are managed by one client, then all guilds will be present.
mfaEnabled boolean/nil Whether the current user's owner's account has multi-factor (or two-factor) authentication enabled. This is equivalent to verified
owner User/nil User object representing the current user's owner.
privateChannels Cache An iterable cache of all private channels that are visible to the client. The channel must exist and must be open for it to be cached here. To access a private channel that may exist but is not cached, User:getPrivateChannel.
relationships Cache An iterable cache of all relationships that are visible to the client. Only user-accounts should have these.
shardCount number/nil The number of shards that this client is managing.
totalShardCount number/nil The total number of shards that the current user is on.
user User/nil User object representing the current user.
users Cache An iterable cache of all users that are visible to the client. To access a user that may exist but is not cached, use Client:getUser.
verified boolean/nil Whether the current user's owner's account is verified.

Methods Inherited From Emitter

emit(name, ...)

Parameter Type Optional
name string
... *

Emits the named event and a variable number of arguments to pass to the event callbacks.

This method only operates on data in memory.

Returns: nil


getListenerCount(name)

Parameter Type
name string

Returns the number of callbacks registered to the named event.

This method only operates on data in memory.

Returns: number


getListeners(name)

Parameter Type
name string

Returns an iterator for all callbacks registered to the named event.

This method only operates on data in memory.

Returns: function


on(name, fn)

Parameter Type
name string
fn function

Subscribes a callback to be called every time the named event is emitted. Callbacks registered with this method will automatically be wrapped as a new coroutine when they are called. Returns the original callback for convenience.

This method only operates on data in memory.

Returns: function


onSync(name, fn)

Parameter Type
name string
fn function

Subscribes a callback to be called every time the named event is emitted. Callbacks registered with this method are not automatically wrapped as a coroutine. Returns the original callback for convenience.

This method only operates on data in memory.

Returns: function


once(name, fn)

Parameter Type
name string
fn function

Subscribes a callback to be called only the first time this event is emitted. Callbacks registered with this method will automatically be wrapped as a new coroutine when they are called. Returns the original callback for convenience.

This method only operates on data in memory.

Returns: function


onceSync(name, fn)

Parameter Type
name string
fn function

Subscribes a callback to be called only the first time this event is emitted. Callbacks registered with this method are not automatically wrapped as a coroutine. Returns the original callback for convenience.

This method only operates on data in memory.

Returns: function


removeAllListeners(name)

Parameter Type
name string/nil

Unregisters all callbacks for the emitter. If a name is passed, then only callbacks for that specific event are unregistered.

This method only operates on data in memory.

Returns: nil


removeListener(name, fn)

Parameter Type
name string
fn function

Unregisters all instances of the callback from the named event.

This method only operates on data in memory.

Returns: nil


waitFor(name, timeout, predicate)

Parameter Type Optional
name string
timeout number
predicate function

When called inside of a coroutine, this will yield the coroutine until the named event is emitted. If a timeout (in milliseconds) is provided, the function will return after the time expires, regardless of whether the event is emitted, and false will be returned; otherwise, true is returned. If a predicate is provided, events that do not pass the predicate will be ignored.

This method only operates on data in memory.

Returns: boolean, ...


Methods

createGroupChannel()

Creates a new group channel. This method is only available for user accounts.

This method always makes an HTTP request.

Returns: GroupChannel


createGuild(name)

Parameter Type
name string

Creates a new guild. The name must be between 2 and 100 characters in length. This method may not work if the current user is in too many guilds. Note that this does not return the created guild object; wait for the corresponding guildCreate event if you need the object.

This method always makes an HTTP request.

Returns: boolean


disableAllIntents()

Disables all gateway intents for this client. The new value will not be used internally until the client (re-)identifies.

This method only operates on data in memory.

Returns: nothing


disableIntents(...)

Parameter Type
... Intents-Resolvables

Disables individual gateway intents for this client. The new value will not be used internally until the client (re-)identifies.

This method only operates on data in memory.

Returns: nothing


enableAllIntents()

Enables all known gateway intents for this client. The new value will not be used internally until the client (re-)identifies.

This method only operates on data in memory.

Returns: nothing


enableIntents(...)

Parameter Type
... Intents-Resolvables

Enables individual gateway intents for this client. The new value will not be used internally until the client (re-)identifies.

This method only operates on data in memory.

Returns: nothing


getApplicationInformation()

Returns a raw data table that contains information about the current OAuth2 application, with no formatting beyond what is provided by the Discord API.

This method always makes an HTTP request.

Returns: table


getChannel(id)

Parameter Type
id Channel-ID-Resolvable

Gets a channel object by ID. For guild channels, the current user must be in the channel's guild and the client must be running the appropriate shard that serves the channel's guild. For private channels, the channel must have been previously opened and cached. If the channel is not cached, User:getPrivateChannel should be used instead.

This method only operates on data in memory.

Returns: Channel


getConnections()

Returns a raw data table that contains a list of connections as provided by Discord, with no formatting beyond what is provided by the Discord API. This is unrelated to voice connections.

This method always makes an HTTP request.

Returns: table


getEmoji(id)

Parameter Type
id Emoji-ID-Resolvable

Gets an emoji object by ID. The current user must be in the emoji's guild and the client must be running the appropriate shard that serves the emoji's guild.

This method only operates on data in memory.

Returns: Emoji


getGuild(id)

Parameter Type
id Guild-ID-Resolvable

Gets a guild object by ID. The current user must be in the guild and the client must be running the appropriate shard that serves this guild. This method never makes an HTTP request to obtain a guild.

This method only operates on data in memory.

Returns: Guild


getIntents()

Returns a number that represents the gateway intents enabled for this client.

This method only operates on data in memory.

Returns: number


getInvite(code, counts)

Parameter Type Optional
code string
counts boolean

Gets an invite object by code. This always makes an HTTP request to obtain a static object that is not cached and is not updated by gateway events.

This method always makes an HTTP request.

Returns: Invite


getRole(id)

Parameter Type
id Role-ID-Resolvable

Gets a role object by ID. The current user must be in the role's guild and the client must be running the appropriate shard that serves the role's guild.

This method only operates on data in memory.

Returns: Role


getSticker(id)

Parameter Type
id Sticker-ID-Resolvable

Gets a sticker object by ID. The current user must be in the sticker's guild and the client must be running the appropriate shard that serves the sticker's guild.

This method only operates on data in memory.

Returns: Sticker


getUser(id)

Parameter Type
id User-ID-Resolvable

Gets a user object by ID. If the object is already cached, then the cached object will be returned; otherwise, an HTTP request is made. Under circumstances which should be rare, the user object may be an old version, not updated by gateway events.

This method may make an HTTP request.

Returns: User


getWebhook(id)

Parameter Type
id string

Gets a webhook object by ID. This always makes an HTTP request to obtain a static object that is not cached and is not updated by gateway events.

This method always makes an HTTP request.

Returns: Webhook


listVoiceRegions()

Returns a raw data table that contains a list of voice regions as provided by Discord, with no formatting beyond what is provided by the Discord API.

This method always makes an HTTP request.

Returns: table


run(token, presence)

Parameter Type Optional
token string
presence table

Authenticates the current user via HTTPS and launches as many WSS gateway shards as are required or requested. By using coroutines that are automatically managed by Luvit libraries and a libuv event loop, multiple clients per process and multiple shards per client can operate concurrently. This should be the last method called after all other code and event handlers have been initialized. If a presence table is provided, it will act as if the user called setStatus and setActivity after run.

This method always makes a WebSocket request.

Returns: nil


setAFK(afk)

Parameter Type
afk boolean/nil

Set the current user's AFK status on all shards that are managed by this client. This generally applies to user accounts and their push notifications. Passing nil removes AFK status.

This method always makes a WebSocket request.

Returns: nil


setActivity(activity)

Parameter Type
activity string/table/nil

Sets the current user's activity on all shards that are managed by this client. If a string is passed, it is treated as the activity name. If a table is passed, it must have a name field and may optionally have a url or type field. Pass nil to remove the activity status. Passing nil removes previously set activities.

This method always makes a WebSocket request.

Returns: nil


setAvatar(avatar)

Parameter Type
avatar Base64-Resolvable

Sets the client's avatar. To remove the avatar, pass an empty string or nil. This does not change the application image.

This method always makes an HTTP request.

Returns: boolean


setIntents(intents)

Parameter Type
intents Intents-Resolvable

Sets the gateway intents that this client will use. The new value will not be used internally until the client (re-)identifies.

This method only operates on data in memory.

Returns: nothing


setStatus(status)

Parameter Type
status string/nil

Sets the current user's status on all shards that are managed by this client. See the status enumeration for acceptable status values. Passing nil removes previously set status.

This method always makes a WebSocket request.

Returns: nil


setUsername(username)

Parameter Type
username string

Sets the client's username. This must be between 2 and 32 characters in length. This does not change the application name.

This method always makes an HTTP request.

Returns: boolean


stop()

Disconnects all shards and effectively stops their loops. This does not empty any data that the client may have cached.

This method always makes a WebSocket request.

Returns: nil


Clone this wiki locally