i.py-like interaction callbacks for disnake.
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file disnake_ext_icallbacks-1.0.0.tar.gz
.
File metadata
- Download URL: disnake_ext_icallbacks-1.0.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64f58f51feadbbb9201bb7f271a8158b484cf9e5e77686e6bf86d71a3809103d |
|
MD5 | 768d1de35cdc203327172ca616442053 |
|
BLAKE2b-256 | 8fcba5d739818db250b2c9c61082ffd1b72bf368985498db03eaa22f5b40af25 |
File details
Details for the file disnake_ext_icallbacks-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: disnake_ext_icallbacks-1.0.0-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1e8ee08a9cbca38d9123918ec10248ce65897a4296ba67b258b6f9c7e509f7a |
|
MD5 | 72c3bd1113b442471e599da66c4750ca |
|
BLAKE2b-256 | d3e8c0f65a81269395889a0a104ca6a5075490d906b592311a887bdbb6a9ad33 |