Skip to content

Commit

Permalink
Add more guides
Browse files Browse the repository at this point in the history
  • Loading branch information
hypergonial committed Jan 7, 2024
1 parent 0feca94 commit c726dbd
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 8 deletions.
49 changes: 49 additions & 0 deletions docs/guides/events.md
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/).
46 changes: 46 additions & 0 deletions docs/guides/startup_shutdown.md
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.
8 changes: 0 additions & 8 deletions docs/tutorial/index.md

This file was deleted.

2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ nav:
- guides/options.md
- guides/command_groups.md
- guides/context_menu.md
- guides/startup_shutdown.md
- guides/hooks.md
- guides/error_handling.md
- guides/plugins_extensions.md
- guides/dependency_injection.md
- guides/events.md
- API Reference:
- api_reference/index.md
- api_reference/client.md
Expand Down

0 comments on commit c726dbd

Please sign in to comment.