API Discord

Discord

 

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 handle by component so you don't need to worry about it. When client connects to server, server sends a HELLO response with heartbeat interval, component reads response and adjust automatically heartbeat so send a ping every x seconds. Sometimes server can send a ping to client, this is handled automatically by client too.

 

Connection Ready

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

 

Connection Resume

 

If connection closes unexpectedly, when client tries to reconnect, it calls OnDiscordBeforeReconnect event, 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 fill be fired.

 

 

Dispatch Events

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

 


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

aEvent parameter contains parameter  name.

RawData contains full JSON message.    

 

HTTP Requests

In order to request info about guild, users, udpate data...  instead  of use gateway websocket messages, Discord requires to use HTTP requests,    so find bellow all methods available to do 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
}