Discord Gateway API
通过 WebSocket 连接 Discord Gateway,构建实时响应消息、在线状态、服务器和语音状态事件的机器人。
通过 WebSocket 连接 Discord Gateway,构建实时响应消息、在线状态、服务器和语音状态事件的机器人。
用于 Discord Gateway WebSocket API 的类型化 Delphi/C++Builder 客户端 — 处理心跳、identify、intents、分片和事件分发。
TsgcWSAPI_Discord
Windows, macOS, Linux, iOS, Android
Standard / Professional / Enterprise
与 TsgcWebSocketClient 配对,设置 Discord.Token 和 Discord.Intents,然后挂接 OnDiscordMessageCreate / OnDiscordReady 以处理 Gateway 事件。
uses
sgcWebSocket, sgcWebSocket_API_Discord;
var
WSClient: TsgcWebSocketClient;
Discord: TsgcWSAPI_Discord;
begin
WSClient := TsgcWebSocketClient.Create(nil);
Discord := TsgcWSAPI_Discord.Create(nil);
Discord.Client := WSClient;
Discord.Discord.Token := 'your-bot-token';
Discord.OnDiscordReady := procedure(Connection: TsgcWSConnection;
const aPayload: string)
begin
Memo1.Lines.Add('Discord ready');
end;
Discord.OnDiscordMessageCreate := procedure(Connection: TsgcWSConnection;
const aMessage: string)
begin
Memo1.Lines.Add(aMessage);
end;
WSClient.Active := True;
end;
// uses: sgcWebSocket, sgcWebSocket_API_Discord
TsgcWebSocketClient *WSClient = new TsgcWebSocketClient(this);
TsgcWSAPI_Discord *Discord = new TsgcWSAPI_Discord(this);
Discord->Client = WSClient;
Discord->Discord->Token = "your-bot-token";
WSClient->Active = true;
在 TsgcWebSocketClient 之上实现 Discord Gateway 协议:IDENTIFY、RESUME、HEARTBEAT、事件分发路由。
连接时,组件发送带有令牌、intents 和属性的 IDENTIFY;断开时发送带有最后序列号的 RESUME,确保事件不丢失。
Discord requires opcode-1 HEARTBEAT messages on the interval the gateway returns in HELLO. The component schedules them and watches HEARTBEAT_ACK to detect zombied connections.
Discord.Intents 选择 Gateway 分发的事件类别(GUILD_MESSAGES、GUILD_MEMBERS、MESSAGE_CONTENT、VOICE_STATES — 完整 intent 位掩码)。
每个 Gateway 分发事件作为类型化属性公开:OnDiscordReady、OnDiscordMessageCreate、OnDiscordGuildCreate、OnDiscordPresenceUpdate 等。
接收 VOICE_STATE_UPDATE 和 VOICE_SERVER_UPDATE 事件 — 与语音 WebSocket 端点结合,实现语音频道自动化。
设置 Discord.Shard 和 Discord.ShardCount,当机器人超过推荐服务器数量阈值时参与 Discord 的分片 Gateway。
本组件所实现 API 的权威参考来源。