Client - truemedian/Discordia GitHub Wiki

Inherits Emitter

The main point of entry into a Discordia application. All data relevant to Discord are 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.

Methods

run
stop
setUsername
setAvatar
createGuild
createGroupChannel
getWebhook
getInvite
getUser
getGuild
getChannel
getRole
getEmoji
listVoiceRegions
getConnections
setStatus
setGame
setAFK

Inherited from Emitter
on
once
onSync
onceSync
emit
getListeners
getListenerCount
removeListener
removeAllListeners
waitFor

Properties

shardCount
totalShardCount
user
owner
verified
mfaEnabled
email
guilds
users
privateChannels
groupChannels
relationships

Methods

:run( token, [ presence ] )

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.

Name Type Optional
token string
presence table x

Returns: nil

:stop( )

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

Returns: nil

:setUsername( username )

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

Name Type
username string

Returns: boolean

:setAvatar( avatar )

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

Name Type
avatar Base64 Resolveable

Returns: boolean

:createGuild( name )

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; listen for the corresponding guildCreate event if you need the object.

Name Type
name string

Returns: boolean

:createGroupChannel( )

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

Returns: GroupChannel

:getWebhook( id )

Gets a webhook object by ID. This always makes an HTTP request to obtain the object, which is a static copy of the server object; it is not automatically updated via gateway events.

Name Type
id string

Returns: Webhook

:getInvite( code, [ counts ] )

Gets an invite object by code. This always makes an HTTP request to obtain the object, which is a static copy of the server object; it is not automatically updated via gateway events.

Name Type Optional
code string
counts boolean x

Returns: Invite

:getUser( id )

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.

Name Type
id User ID Resolvable

Returns: User

:getGuild( id )

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.

Name Type
id Guild ID Resolvable

Returns: Guild

:getChannel( id )

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.

Name Type
id Channel ID Resolvable

Returns: Channel

:getRole( id )

Returns a raw data table that contains a list of voice regions as provided by Discord, with no additional parsing.

Name Type
id Role ID Resolvable

Returns: Role

:getEmoji( id )

Gets a user object by ID. This method nevermakes an HTTP request to obtain a guild. Under circumstances which should be rare, the user object may be an old version, not updated by gateway events.

Name Type
id Emoji ID Resolvable

Returns: Emoji

:listVoiceRegions( )

Returns a raw data table that contains a list of voice regions as provided by Discord, with no additional parsing.

Returns: table

:getConnections( )

Returns a raw data table that contains a list of connections as provided by Discord, with no additional parsing.

Returns: table

:setStatus( status )

Sets the current users's status on all shards that are managed by this client. Valid statuses are online, idle, dnd, and invisible.

Name Type
status string

Returns: nil

:setGame( game )

Sets the current users's game on all shards that are managed by this client. If a string is passed, it is treated as the game name. If a table is passed, it must have a name field and may optionally have a url field. Pass nil to remove the game status.

Name Type
game string

Returns: nil

:setAFK( afk )

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.

Name Type
afk boolean

Returns: nil

Inherited from Emitter

:on( name, fn )

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.

Name Type
name string
fn function

Returns: function

:once( name, fn )

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.

Name Type
name string
fn function

Returns: function

:onSync( name, fn )

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.

Name Type
name string
fn function

Returns: function

:onceSync( name, fn )

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.

Name Type
name string
fn function

Returns: function

:emit( name, [ ... ] )

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

Name Type Optional
name string
... * x

Returns: nil

:getListeners( name )

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

Name Type
name string

Returns: function

:getListenerCount( name )

Returns the number of callbacks registered to the named event.

Name Type
name string

Returns: number

:removeListener( name )

Unregisters all instances of the callback from the named event.

Name Type
name string

Returns: nil

:removeAllListeners( name )

Unregisters all callbacks from the named event.

Name Type
name string

Returns: nil

:waitFor( name, [ timeout ], [ predicate ] )

When called inside of a coroutine, this will yield the coroutine until the specific named event is emitted or until a timeout (in milliseconds) expires. If the coroutine is resumed by the event, then true is returned with any event arguments. If the coroutine is resumed by the timeout's expiration, then false is returned without any other arguments.

Name Type Optional Optional
name string
timeout number x
predicate function x

Returns: boolean, ...

Properties

.shardCount

The number of shards that this client is managing.

Type: number|nil

.totalShardCount

The total number of shards that this user is on.

Type: number|nil

.user

User object representing the current user.

Type: User|nil

.owner

User object representing the current user's owner.

Type: User|nil

.verified

Whether the current user's owner's account is verified.

Type: boolean|nil

.mfaEnabled

Whether the current user's owner's account has multi-factor (or two-factor) authentication enabled.

Type: boolean|nil

.email

The current user's owner's account's email address (user-accounts only).

Type: string|nil

.guilds

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.

Type: Cache

.users

An iterable cache of all users that are visible to the client. Users that are not referenced elsewhere are eventually garbage collected. To access a user that may exist but is not cached, use Client:getUser.

Type: Cache

.privateChannels

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.

Type: Cache

.groupChannels

An iterable cache of all group channels that are visible to the client. Only user-accounts should have these.

Type: Cache

.relationships

An iterable cache of all relationships that are visible to the client. Only user-accounts should have these.

Type: Cache

⚠️ **GitHub.com Fallback** ⚠️