-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0feca94
commit c726dbd
Showing
4 changed files
with
97 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
title: Events | ||
description: A guide on handling events in arc | ||
hide: | ||
- toc | ||
--- | ||
|
||
!!! info "Gateway only" | ||
This guide is only relevant to those using a **Gateway** client, as REST bots **cannot receive events**. | ||
|
||
Your bot receives events based on what is happening in the guilds your bot is in, describing state changes (e.g. a new member joined, a message was sent) and allowing you to react to them. To make your bot react to a specific event, you must create a listener: | ||
|
||
```py | ||
# This only works with Gateway bots | ||
bot = hikari.GatewayBot("...") | ||
client = arc.GatewayClient(bot) | ||
|
||
# ... | ||
|
||
@client.listen() | ||
async def on_message(event: hikari.MessageCreateEvent) -> None: | ||
|
||
# Ignore ourselves & other bots | ||
if not event.is_human: | ||
return | ||
|
||
await client.rest.create_message(event.channel_id, "Hi!") | ||
``` | ||
|
||
With the above snippet, the bot will respond to *every* message sent with "Hi!". | ||
|
||
Plugins can also have listeners, and behave similarly to those registered on the client itself: | ||
|
||
```py | ||
plugin = arc.GatewayPlugin("name") | ||
|
||
# ... | ||
|
||
@plugin.listen() | ||
async def on_message(event: hikari.MessageCreateEvent) -> None: | ||
|
||
# Ignore ourselves & other bots | ||
if not event.is_human: | ||
return | ||
|
||
await plugin.client.rest.create_message(event.channel_id, "Hi!") | ||
``` | ||
|
||
For a list of all available events, see the [hikari documentation](https://docs.hikari-py.dev/en/latest/reference/hikari/events/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
title: Startup & Shutdown | ||
description: A guide on handling lifecycle in arc | ||
hide: | ||
- toc | ||
--- | ||
|
||
# Startup & Shutdown | ||
|
||
It is possible to execute code when the client has started up or shut down, this can be done via the [`@Client.set_startup_hook`][arc.abc.Client.set_startup_hook] and [`@Client.set_shutdown_hook`][arc.abc.Client.set_shutdown_hook] respectively. | ||
|
||
=== "Gateway" | ||
|
||
```py | ||
@client.set_startup_hook | ||
async def startup_hook(client: arc.GatewayClient) -> None: | ||
print("Client started up!") | ||
``` | ||
|
||
=== "REST" | ||
|
||
```py | ||
@client.set_startup_hook | ||
async def startup_hook(client: arc.RESTClient) -> None: | ||
print("Client started up!") | ||
``` | ||
|
||
The **startup hook** is a great place to initialize resources that require an async context and/or the bot to be started. It is called after the client has already synced all commands and the underlying bot has fully started. | ||
|
||
=== "Gateway" | ||
|
||
```py | ||
@client.set_shutdown_hook | ||
async def shutdown_hook(client: arc.GatewayClient) -> None: | ||
print("Client shut down!") | ||
``` | ||
|
||
=== "REST" | ||
|
||
```py | ||
@client.set_shutdown_hook | ||
async def shutdown_hook(client: arc.RESTClient) -> None: | ||
print("Client shut down!") | ||
``` | ||
|
||
The **shutdown hook** is where you can clean up any remaining resources, close connections, etc. It is called when the bot has started to shut down. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters