API Discord

Discord

Note: As of API v10, the Discord API domain has changed from discordapp.com to discord.com. The component has been updated to use API version 10 and the new domain.

 

Gateways are Discord's form of real-time communication over secure WebSockets. Clients will receive events and data over the gateway they are connected to and send data over the REST API.

 

Authorization

First you must generate a new Bot, and copy Bot Token which will be used to authenticate through API. Then set this token in API Component.

 


TsgcWSAPI_Discord1.DiscordOptions.BotOptions.Token := '...bot token here...';

 

Intents

Maintaining a stateful application can be difficult when it comes to the amount of data you're expected to process, especially at scale. Gateway Intents are a system to help you lower that computational burden.

When identifying to the gateway, you can specify an intents parameter which allows you to conditionally subscribe to pre-defined "intents", groups of events defined by Discord. If you do not specify a certain intent, you will not receive any of the gateway events that are batched into that group. The valid intents are (zero value means all events are received):

 

GUILDS (1 << 0) = Integer (1)

  - GUILD_CREATE

  - GUILD_DELETE

  - GUILD_ROLE_CREATE

  - GUILD_ROLE_UPDATE

  - GUILD_ROLE_DELETE

  - CHANNEL_CREATE

  - CHANNEL_UPDATE

  - CHANNEL_DELETE

  - CHANNEL_PINS_UPDATE

GUILD_MEMBERS (1 << 1) = Integer (2)

  - GUILD_MEMBER_ADD

  - GUILD_MEMBER_UPDATE

  - GUILD_MEMBER_REMOVE

GUILD_BANS (1 << 2) = Integer (4)

  - GUILD_BAN_ADD

  - GUILD_BAN_REMOVE

GUILD_EMOJIS (1 << 3) = Integer (8)

  - GUILD_EMOJIS_UPDATE

GUILD_INTEGRATIONS (1 << 4) = Integer (16)

  - GUILD_INTEGRATIONS_UPDATE

GUILD_WEBHOOKS (1 << 5) = Integer (32)

  - WEBHOOKS_UPDATE

GUILD_INVITES (1 << 6) = Integer (64)

  - INVITE_CREATE

  - INVITE_DELETE

GUILD_VOICE_STATES (1 << 7) = Integer (128)

  - VOICE_STATE_UPDATE

GUILD_PRESENCES (1 << 8) = Integer (256)

  - PRESENCE_UPDATE

GUILD_MESSAGES (1 << 9) = Integer (512)

  - MESSAGE_CREATE

  - MESSAGE_UPDATE

  - MESSAGE_DELETE

GUILD_MESSAGE_REACTIONS (1 << 10) = Integer (1024)

  - MESSAGE_REACTION_ADD

  - MESSAGE_REACTION_REMOVE

  - MESSAGE_REACTION_REMOVE_ALL

  - MESSAGE_REACTION_REMOVE_EMOJI

GUILD_MESSAGE_TYPING (1 << 11) = Integer (2048)

  - TYPING_START

DIRECT_MESSAGES (1 << 12) = Integer (4096)

  - CHANNEL_CREATE

  - MESSAGE_CREATE

  - MESSAGE_UPDATE

  - MESSAGE_DELETE

  - CHANNEL_PINS_UPDATE

DIRECT_MESSAGE_REACTIONS (1 << 13) = Integer (8192)

  - MESSAGE_REACTION_ADD

  - MESSAGE_REACTION_REMOVE

  - MESSAGE_REACTION_REMOVE_ALL

  - MESSAGE_REACTION_REMOVE_EMOJI

DIRECT_MESSAGE_TYPING (1 << 14) = Integer (16384)

  - TYPING_START

 

Heartbeat

Heartbeats are automatically handled by the component so you don't need to worry about them. When the client connects to the server, the server sends a HELLO response with a heartbeat interval, and the component reads the response and automatically adjusts the heartbeat to send a ping every x seconds. Sometimes the server can send a ping to the client; this is handled automatically by the client too.

 

Connection Ready

When the connection is ready, after a successful login and authorization by the server, OnDiscordReady event is raised and then you can start to receive updates from server.

 

Connection Resume

 

If the connection closes unexpectedly, when the client tries to reconnect, it calls OnDiscordBeforeReconnect event. The component automatically saves all data needed to make a successful resume, but parameters can be changed if needed. If you don't want to reconnect and start a new clean session, just set Reconnect to False.

 

If session is resumed, OnDiscordResumed event is fired. If it's a new session, OnDiscordReady will be fired.

 

 

Dispatch Events

Events are dispatched through OnDiscordDispatch, so here you can read events sent by the server to the client.

 


procedure OnDiscordDispatch(Sender: TObject; const aEvent, RawData: string);
begin
  DoLog('#discord dispatch: ' + aEvent + ' ' + RawData);
end;

aEvent parameter contains the event name.

RawData contains full JSON message.    

 

HTTP Requests

In order to request info about guilds, users, or update data...  instead of using gateway websocket messages, Discord requires the use of HTTP requests,    Find below all methods available to make an HTTP request:       

 

function GET_Request(const aPath: String): string;    

function POST_Request(const aPath, aMessage:  String): string;     

function PUT_Request(const aPath, aMessage:  String): string;     

function PATCH_Request(const aPath, aMessage:  String): string;     

function DELETE_Request(const aPath: String):  string;  

 

Example: get current user info
 


result := GET_Request('/users/@me');

 

sample response from server:


{
"id": "637423922035480852",
"username": "test",
"avatar": null,
"discriminator": "5125",
"bot": true,
"email": null,
"verified": true,
"locale": "en-US",
"mfa_enabled": false,
"flags": 0
}