Skip to main content

disnake-ext-icallbacks

disnake-ext-icallbacks (interaction callbacks) is an extension that allows you to create callback listeners with custom IDs. You will find this useful if you hate View classes like me (and too lazy to stack on_interaction events, or simply love the callback decorators from interactions.py.


Installation

Python 3.8 or higher is required.

To install the extension, simply run the following command:

pip install disnake-ext-icallbacks

It will be installed with your disnake installation and can be imported with:

from disnake.ext import icallbacks
# or
from disnake.ext.icallbacks import *

Usage & Examples

To use this extension, you must first hook it to your bot instance:

import disnake
from disnake.ext import commands, icallbacks

bot = commands.Bot(...)
icallbacks.setup(bot)

This will inject the @component_callback and @modal_callback decorators to your bot instance and set up the necessary event listeners.


Then, you can use the decorators to create a component/modal listener with the custom ID:

@bot.slash_command()
async def button(self, inter: disnake.ApplicationCommandInteraction):
    await inter.send(
        components=[
            disnake.ui.Button(label="Click Me", custom_id="button_custom_id"),
        ],
    )

@bot.component_callback("button_custom_id")
async def on_button_click(inter: disnake.MessageInteraction):
    await inter.send("Button clicked!")

or in a cog class:

class MyCog(commands.Cog):
    @icallbacks.component_callback("button_custom_id")
    async def on_button_click(self, inter: disnake.MessageInteraction):
        await inter.send("Button clicked!")

You can also use the @modal_callback decorator to create a modal listener with the custom ID:

@bot.slash_command()
async def modal(self, inter: disnake.ApplicationCommandInteraction):
    await inter.response.send_modal(
        title="test",
        custom_id="modal_custom_id",
        components=[disnake.ui.TextInput(label="Input", custom_id="input1")]
    )

@bot.modal_callback("modal_custom_id")
async def on_modal_submit(inter: disnake.MessageInteraction):
    await inter.send(f"Input: {inter.text_values["input1"]}")

or again, in a cog class:

class MyCog(commands.Cog):
    @icallbacks.modal_callback("modal_custom_id")
    async def on_modal_submit(self, inter: disnake.MessageInteraction):
        await inter.send(f"Input: {inter.text_values["input1"]}")

Additionally, you can match the custom ID with a regex pattern:

import re

re_pattern = re.compile(r"button_custom_id:(\d+)")

@bot.slash_command()
async def buttons(self, inter: disnake.ApplicationCommandInteraction):
    await inter.send(
        components=[
            disnake.ui.Button(label="B1", custom_id="button_custom_id:1"),
            disnake.ui.Button(label="B2", custom_id="button_custom_id:2"),
        ],
    )

@bot.component_callback(re_pattern)
async def on_button_click(inter: disnake.MessageInteraction):
    match = re_pattern.match(inter.data.custom_id)
    await inter.send(f"Button {match.group(1)} clicked!")

Credits

This extension is inspired by the interactions.py library.

License

This project is licensed under the MIT License - see the LICENSE for details.

Metadata

Release files for disnake-ext-icallbacks 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for disnake-ext-icallbacks 1.0.0
File Size Uploaded
disnake_ext_icallbacks-1.0.0.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for disnake-ext-icallbacks 1.0.0
File Interpreter ABI Platform
disnake_ext_icallbacks-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.4 kB

Release files / disnake_ext_icallbacks-1.0.0.tar.gz

Download URL disnake_ext_icallbacks-1.0.0.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
64f58f51feadbbb9201bb7f271a8158b484cf9e5e77686e6bf86d71a3809103d
BLAKE2b-256 checksum
How to use checksums
8fcba5d739818db250b2c9c61082ffd1b72bf368985498db03eaa22f5b40af25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / disnake_ext_icallbacks-1.0.0-py3-none-any.whl

Download URL disnake_ext_icallbacks-1.0.0-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f1e8ee08a9cbca38d9123918ec10248ce65897a4296ba67b258b6f9c7e509f7a
BLAKE2b-256 checksum
How to use checksums
d3e8c0f65a81269395889a0a104ca6a5075490d906b592311a887bdbb6a9ad33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page