Skip to content

Commit d5745bb

Browse files
EepyElvyraToricane
andauthored
docs: Add page describing how to use events (#879)
* create events.rst * docs: stuff * docs: stuff * docs: stuff * docs: stuff * docs: stuff * docs: socket create and on command * docs: socket create and on command * docs: socket create and on command * docs: socket create and on command * docs: discord event * refactor: discord events * refactor: discord events * fix: attempt to fix links * docs: stuff * Update events.rst * Update docs/events.rst Co-authored-by: Toricane <73972068+Toricane@users.noreply.github.com> Co-authored-by: Toricane <73972068+Toricane@users.noreply.github.com>
1 parent 46fa80e commit d5745bb

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

docs/events.rst

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
Event Documentation
2+
====================
3+
4+
You all probably already know that there are several events, internal and from Discord, that you can receive with your
5+
bot. This page will lead you through all dispatched internal events and a few from Discord.
6+
7+
8+
9+
How to listen for events
10+
************************
11+
12+
Generally, you can listen to an event like this:
13+
14+
.. code-block:: python
15+
16+
import interactions
17+
18+
bot = interactions.Client(...)
19+
20+
# possibility 1
21+
@bot.event
22+
async def on_(...):
23+
... # do smth
24+
25+
# possibility 2
26+
@bot.event(name="on_")
27+
async def you_are_free_to_name_this_as_you_want(...):
28+
... # do smth
29+
30+
bot.start()
31+
32+
33+
```` represents the Discord event name - but lowercase and any spaces replaced with ``_``.
34+
35+
For example:
36+
37+
* ``READY`` -> ``on_ready``
38+
* ``GUILD MEMBER ADD`` -> ``on_guild_member_add``
39+
40+
``(...)`` represents the different arguments a function takes. Those arguments differ per event.
41+
42+
43+
44+
Now, let us have a look at different events and how they work, starting with internal events.
45+
46+
Internal Events
47+
****************
48+
49+
All events mentioned in this section have the exact naming as they must be put into the function.
50+
51+
There are several different internal events:
52+
53+
- ``raw_socket_create``
54+
- ``on_interaction``
55+
- ``on_command``
56+
- ``on_component``
57+
- ``on_autocomplete``
58+
- ``on_modal``
59+
60+
Lets now have a look at those events in detail:
61+
62+
Event: ``raw_socket_create``
63+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
64+
This event fires on any event sent by Discord, including ``Typing Start`` and ``Voice State Update``.
65+
``Hello``, ``Resumed``, ``Reconnect`` and ``Invalid Session`` still will not be dispatched.
66+
67+
The function handling the event should take in one argument, the type of this argument is a ``dict``.
68+
69+
The value of the argument will be the *raw* data sent from Discord, so it is not recommended to use that event
70+
as long as you don't absolutely need it.
71+
72+
73+
Event: ``on_interaction``
74+
^^^^^^^^^^^^^^^^^^^^^^^^^^
75+
This event fires on any interaction (commands, components, autocomplete and modals).
76+
77+
The function needs one argument. It will have the type ``CommandContext`` or ``ComponentContext``.
78+
79+
Because you will have to check for everything, from the ``InteractionType`` to any data inside the context, we do not
80+
recommend using this event unless you have experience with it.
81+
82+
83+
Event: ``on_command``
84+
^^^^^^^^^^^^^^^^^^^^^
85+
This event fires on any Application Command (slash command + context menu command) used.
86+
87+
The function takes in one argument of the type ``CommandContext``.
88+
89+
Using this event, you will have to manually check everything, from name to whether the used commands have sub commands,
90+
options or anything else - everything will be in the raw context and you will have to search for it
91+
92+
93+
Event: ``on_component``
94+
^^^^^^^^^^^^^^^^^^^^^^
95+
This event fires on any Component used (for now, those are Buttons and Select Menus).
96+
97+
The function takes in one argument of the type ``ComponentContext``.
98+
99+
Like ``on_command``, you will have to manually check for everything, i.e for custom id and component type.
100+
Also, you will have to look through the argument to find the selected choices, if you have a select menu.
101+
102+
103+
Event: ``on_autocomplete``
104+
^^^^^^^^^^^^^^^^^^^^^^^^^^
105+
This event fires on any autocomplete triggered.
106+
107+
The function takes in one argument of the type ``CommandContext``.
108+
109+
As already in the events above, you will have to get the important values yourself. Those values are here the
110+
autocompleted option and the user input.
111+
112+
113+
Event: ``on_modal``
114+
^^^^^^^^^^^^^^^^^^^
115+
This event fires on every modal that is submitted.
116+
117+
The function takes in one argument of the type ``CommandContext``.
118+
119+
You will have to get all values yourself and check what modal was used when using this event.
120+
121+
122+
Additionally, if you struggle with getting the values, you can check
123+
:ref:`how it is handled internally `.
124+
125+
126+
After this, let us look at events from the Discord API.
127+
128+
Discord API Events
129+
******************
130+
131+
Events in this section do not match the name needed to put into the function. Please check
132+
:ref:`above ` for how to get the correct name.
133+
134+
135+
There are a lot of events dispatched by the Discord API. All of those can be found `here`_.
136+
137+
The events ``HELLO``, ``RESUMED``, ``RECONNECT``, ``INVALID SESSION`` and ``TYPING START`` are not dispatched by the library.
138+
139+
``TYPING START`` will be included in the raw socket create event. You can
140+
also listen for it if you choose to subclass the WebSocketClient
141+
142+
The event ``VOICE STATE UPDATE`` can be only received with the voice :ref:`Extension `.
143+
144+
145+
Let's now have a look at a few events:
146+
147+
Event: ``READY``
148+
^^^^^^^^^^^^^^^^
149+
This event fires whenever ``READY`` is dispatched by Discord. This happens when connecting to the web socket server.
150+
151+
This function takes no arguments.
152+
153+
.. attention::
154+
Due to the bot reconnecting during runtime, ``on_ready`` will be dispatched multiple times. If you rely on
155+
``on_ready`` to do certain things once, check against a global variable as shown below:
156+
157+
.. code-block:: python
158+
159+
_ready: bool = False
160+
bot = interactions.Client(...)
161+
162+
@bot.event
163+
async def on_ready():
164+
global _ready
165+
if not _ready:
166+
... # do stuff
167+
_ready = True
168+
169+
170+
Events: ``GUILD MEMBER UPDATE`` and ``GUILD MEMBER ADD``
171+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
172+
These events fire whenever a member joins a guild or a member of a guild gets modified.
173+
174+
The function takes in one argument of the type ``GuildMember``.
175+
176+
The argument has the same methods as a normal ``Member`` object, except the methods *do not take in a guild id*.
177+
Please keep that in mind when using this event.
178+
179+
180+
.. _here: https://Discord.com/developers/docs/topics/gateway#commands-and-events-gateway-events

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Please look at our pages below to find out where to go.
5050

5151
quickstart.rst
5252
api.rst
53+
events.rst
5354
faq.rst
5455
migration.rst
5556

0 commit comments

Comments
 (0)