Overview of Events 活动概览
Apps can listen to events happening in Discord to stay up-to-date with changes and updates to servers, users, and even your app. The following sections cover basic information about the different transport methods that can be used to receive events, and link out to relevant documentation.
应用程序可以侦听 Discord 中发生的事件,以了解服务器、用户甚至您的应用程序的更改和更新。以下部分介绍了可用于接收事件的不同传输方法的基本信息,并链接到相关文档。
Receiving Events 接收事件
There are many event types that can be accessed using different transport methods:
可以使用不同的传输方法访问许多事件类型:
- Gateway events are sent over a WebSocket connection between your app and Discord, and is the primary way to receive and send events. Most events are only available via Gateway connections.
网关事件通过应用程序和 Discord 之间的 WebSocket 连接发送,是接收和发送事件的主要方式。大多数活动只能通过网关连接进行。 - Webhook events are sent to your app's Webhook Event URL over HTTP.
Webhook 事件通过 HTTP 发送到应用程序的 Webhook 事件 URL。 - SDK events are sent to your app when using the Embedded App SDK.
使用嵌入式应用程序 SDK 时, SDK 事件会发送到您的应用程序。
Read details about each way to receive events in the sections below.
请阅读以下部分中有关接收事件的每种方式的详细信息。
Using Gateway 使用网关
Gateway events are the primary way apps can listen and send events. Most events related to resources in Discord, like updates to channels, guilds, roles, and messages, are only available over Gateway.
网关事件是应用程序侦听和发送事件的主要方式。大多数与 Discord 中的资源相关的事件(例如频道、行会、角色和消息的更新)只能通过Gateway进行。
Gateway events are sent over a WebSocket-based Gateway connection between Discord and your app. To receive Gateway events, your app must open and maintain a persistent Gateway connection which you can read details about in the Gateway documentation. To make receiving Gateway events simpler, we suggest using a developer library which helps setup, maintain, and handle common pitfalls with Gateway connections (like rate limits).
网关事件通过 Discord 和您的应用程序之间基于 WebSocket 的网关连接发送。要接收网关事件,您的应用程序必须打开并维护持久的网关连接,您可以在网关文档中阅读有关详细信息。为了使接收网关事件变得更简单,我们建议使用开发人员库,它可以帮助设置、维护和处理网关连接的常见陷阱(例如速率限制)。
Details about receiving events using the Gateway API is in the Gateway documentation, and the full list of available Gateway events is in the Gateway Events documentation.
有关使用网关 API 接收事件的详细信息位于网关文档中,可用网关事件的完整列表位于网关事件文档中。
Using Webhooks 使用网络钩子
Webhook events let you receive a small number of events over HTTP. While many events aren't supported over HTTP, some events like Application Authorized (sent when your app is installed to a user or server) aren't available using other transport methods like Gateway.
Webhook 事件允许您通过 HTTP 接收少量事件。虽然许多事件不支持通过 HTTP,但某些事件(如应用程序授权) (当您的应用程序安装到用户或服务器时发送)无法使用其他传输方法(如网关)。
Find the list of webhook events and details about subscribing and handling them in the Webhook Events documentation.
在Webhook 事件文档中查找Webhook 事件列表以及有关订阅和处理这些事件的详细信息。
Using the Embedded App SDK 使用嵌入式应用程序 SDK
When developing Activities, you can listen to a collection of SDK events, like updates to a user's voice status or screen orientation. To listen to SDK events, you can call subscribe()
with the SDK event name.
开发Activity时,您可以监听SDK 事件的集合,例如用户语音状态或屏幕方向的更新。要监听 SDK 事件,您可以使用 SDK 事件名称调用subscribe()
。
Details about listening to events using the Embedded App SDK is in the Embedded App SDK documentation.
有关使用嵌入式应用程序 SDK 侦听事件的详细信息,请参阅嵌入式应用程序 SDK 文档。