API Reference¶
- class aiointeractions.InteractionsApp(client, *, aiohttp_app=None, route='/interactions', success_response=<do_nothing>, forbidden_response=<do_nothing>)¶
A web application made with aiohttp for receiving interactions from Discord.
- Parameters:
client (
discord.Client) – The discord.py client instance for the web application to use. This can bediscord.Clientor any subclass of it such asdiscord.ext.commands.Botaiohttp_app (Optional[
aiohttp.web.Application]) –A pre-existing web application to add the interactions route to. If not passed, a new web application instance will be created.
Changed in version 2.0: Renamed from app to aiohttp_app to add more distinction.
route (
str) – The route to add the interactions handler to. Defaults to/interactions.success_response (Callable[[
web.Request], Any]) – A function (synchronous or asynchronous) that accepts 1 argument, request that would return the body for the response. Defaults to a function that would do nothing.forbidden_response (Callable[[
web.Request], Any]) –A function (synchronous or asynchronous) that accepts 1 argument, request that would return the body for the response. Defaults to a function that would do nothing.
Changed in version 2.0: Removed the raise_for_bad_response parameter and always raise
aiohttp.web.HTTPUnauthorized.Warning
If the return value of success_response or forbidden_response are meant to be a JSON response, make sure to serialize the object to JSON format with json.dumps.
Note
You can use discord.utils.setup_logging() for basic logging. Use
discord.utils.setup_logging(root=False)to disable logging for aiohttp.- client¶
The discord.py client instance currently being used.
- Type:
discord.Client
- aiohttp_app¶
The instance of
aiohttp.web.Applicationcurrently being used.Changed in version 2.0: Renamed from app to aiohttp_app to add more distinction.
- Type:
Optional[
aiohttp.web.Application]
- success_response¶
The current success response function.
- Type:
Callable[[
web.Request], Any]
- forbidden_response¶
The current forbidden response function.
- Type:
Callable[[
web.Request], Any]
- is_running()¶
- Return type:
Trueif the web server is running, otherwiseFalse.
Added in version 1.1.
- run(token, **kwargs)¶
A top-level blocking call that automatically handles cleanup for the discord client, event loop, and provides a graceful shutdown. This automatically calls
setup()but note that it is called after the web server is started.- Parameters:
token (
str) – The authentication token for discord.**kwargs – The keyword arguments to pass onto aiohttp.web.run_app.
Added in version 1.3.
Changed in version 2.0:
setup()is called after the web server is started, instead of before.
- async setup(token)¶
Setup the discord client by logging in and fetching the servers verification keys. Call this method if you are using aiohttp’s asynchronous startup instead of
run()- Parameters:
token (
str) – The authentication token.
Added in version 2.0.
- async start(token, **kwargs)¶
Start the web server and call the login method. This method gives more control over initialization and cleanup than
run().- Parameters:
token (
str) – The authentication token for discord.**kwargs –
The keyword arguments to pass onto aiohttp.web.run_app.
Warning
When using this method, this library will not handle discord client cleanup, event loop cleanup, nor provide a graceful shutdown. It is recommended to use
run()instead for simplicity.Deprecated since version 2.0:
start()will be removed in v3 (if this project makes it that far). Use aiohttp’s asynchronous startup instead. Below is an alternative.main.py¶1import asyncio 2 3import aiointeractions 4from aiohttp import web 5 6client = discord.Client(...) 7app = aiointeractions.InteractionsApp(client, ...) 8 9async def main(): 10 async with client: 11 await app.setup('token') 12 13 runner = web.AppRunner(app.app) 14 await runner.setup() 15 site = web.TCPSite(runner) 16 await site.start() 17 18 try: 19 while True: 20 await asyncio.sleep(3600) 21 finally: 22 await runner.cleanup() 23 24asyncio.run(main())
Event Reference¶
You can listen to the following events on your client instance.
- on_interaction_request(request)¶
Called when a request to the interactions endpoint has been received. The interaction has not been verified yet so it can be from anywhere.
- Parameters:
request (aiohttp.web.Request) – The request object.
Note
The request may not be an interaction, it may be a ping from Discord.
- on_verified_interaction_request(request)¶
Called when a request to the interactions endpoint has been received and it has been verified.
- Parameters:
request – The request object.
Note
The request may not be an interaction, it may be a ping from Discord.