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 be discord.Client or any subclass of it such as discord.ext.commands.Bot

  • aiohttp_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.Application currently 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:

True if the web server is running, otherwise False.

New 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.

New 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.

New 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 v2.2. Use aiohttp’s asynchronous startup instead. Below is an alternative.

import asyncio

import aiointeractions
from aiohttp import web

client = discord.Client(...)
app = aiointeractions.InteractionsApp(client, ...)

async def main():
    async with client:
        await app.setup('token')

        runner = web.AppRunner(app.app)
        await runner.setup()
        site = web.TCPSite(runner)
        await site.start()

        try:
            while True:
                await asyncio.sleep(3600)
        finally:
            await runner.cleanup()

asyncio.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.

The parameters are the same as on_interaction_request().