Skip to main content

Disnake mini library with Dishka integration

Project description

Dishka-Disnake Documentation

Note: Before using dishka-disnake, it is recommended to familiarize yourself with the principles of Dishka, as this library builds on them while providing a Disnake-like interface.

dishka-disnake is a wrapper for Disnake that allows you to use all standard Disnake interactions (slash commands, user commands, message commands, buttons, selects, and modals) with a slightly different import structure and simplified setup.


Installation

pip install dishka-disnake

Setup

To initialize your bot with Dishka, you need to call setup_dishka before creating your bot instance. This ensures that all Dishka integrations are properly configured in the asynchronous container:

from dishka import make_async_container, AsyncContainer
from dishka_disnake import setup_dishka
from disnake.ext.commands import Bot

# Create the async container for your bot
container: AsyncContainer = make_async_container(...)

async def main():
    # Setup Dishka integration on the container before creating the bot
    setup_dishka(container)

    # Now you can create your bot instance
    bot = Bot(...)

    # Start your bot
    await bot.start("YOUR_BOT_TOKEN")

setup_dishka(async_container) prepares your async container so that commands, buttons, selects, and modals work correctly with Dishka.


DishkaCog

DishkaCog is a base class for Cogs that automatically injects dependencies from the Dishka container into all supported handlers — no decorators needed.

Supported handler types:

  • Slash commands (@commands.slash_command)
  • User commands (@commands.user_command)
  • Message commands (@commands.message_command)
  • Prefix commands (@commands.command)
  • Events (@Cog.listener)
from dishka import FromDishka
from dishka_disnake import DishkaCog
from disnake import AppCmdInter
from disnake.ext import commands
from disnake.ext.commands import Bot, Context

class MyCog(DishkaCog):
    def __init__(self, bot: Bot):
        self.bot = bot

    # Slash command — session is injected automatically
    @commands.slash_command(name="hello")
    async def hello(self, interaction: AppCmdInter, session: FromDishka[AsyncSession]):
        await interaction.response.send_message(f"Hello! {session}")

    # Prefix command — session is injected automatically
    @commands.command(name="ping")
    async def ping(self, ctx: Context, session: FromDishka[AsyncSession]):
        await ctx.send(f"Pong! {session}")

    # User command — session is injected automatically
    @commands.user_command(name="info")
    async def info(self, interaction: AppCmdInter, session: FromDishka[AsyncSession]):
        await interaction.response.send_message(f"Info! {session}")

    # Message command — session is injected automatically
    @commands.message_command(name="quote")
    async def quote(self, interaction: AppCmdInter, session: FromDishka[AsyncSession]):
        await interaction.response.send_message(f"Quote! {session}")

    # Listener event — session is injected automatically
    @Cog.listener("on_ready")
    async def on_ready(self, session: FromDishka[AsyncSession]):
        print(f"Ready! {session}")

def setup(bot: Bot):
    bot.add_cog(MyCog(bot))

Subcommands and subcommand groups

DishkaCog also works with subcommands and subcommand groups:

class MyCog(DishkaCog):
    def __init__(self, bot: Bot):
        self.bot = bot

    @commands.slash_command()
    async def base(self, interaction: AppCmdInter):
        pass

    @base.sub_command()
    async def sub(self, interaction: AppCmdInter, session: FromDishka[AsyncSession]):
        await interaction.response.send_message(f"Sub! {session}")

    @commands.slash_command()
    async def group(self, _: AppCmdInter):
        pass

    @group.sub_command_group()
    async def subgroup(self, _: AppCmdInter):
        pass

    @subgroup.sub_command()
    async def nested(self, interaction: AppCmdInter, session: FromDishka[AsyncSession]):
        await interaction.response.send_message(f"Nested! {session}")

Difference from @inject

If you only need injection on specific methods rather than the entire Cog, use the @inject decorator on individual methods in a regular Cog instead of inheriting from DishkaCog:

from dishka_disnake import inject

class MyCog(Cog):

    @slash_command(name="hello")
    @inject
    async def hello(self, interaction: AppCmdInter, session: FromDishka[AsyncSession]):
        await interaction.response.send_message(f"Hello! {session}")

Commands

dishka-disnake supports all standard Disnake command types (only cogs):


Slash Commands

from dishka_disnake.commands import slash_command

class HelloCog(Cog):

    @slash_command(name="hello", description="Say hello")
    async def hello_command(self, interaction: AppCmdInter, usecase: FromDishka[HelloUseCase]):
        ...

UserCommands

from dishka_disnake.commands import user_command

class HelloCog(Cog):

    @user_command(name="hello", description="Say hello")
    async def hello_command(self, interaction: AppCmdInter, usecase: FromDishka[HelloUseCase]):
        ...

MessageCommands

from dishka_disnake.commands import message_command

class HelloCog(Cog):

    @message_command(name="hello", description="Say hello")
    async def hello_command(self, interaction: AppCmdInter, usecase: FromDishka[HelloUseCase]):
        ...

Components

Buttons

from disnake import ui, MessageInteraction
from dishka_disnake.ui import Button, button


class MyButton(Button):
    def __init__(self):
        super().__init__(label="My Button", style=ButtonStyle.primary)

    async def callback(self, interaction: MessageInteraction, repo: FromDishka[UserRepo]):
        ...


class MyView(ui.View):
    def __init__(self):
        super().__init__()
        self.add_item(MyButton())

    @button(label="My Button")
    async def my_button_callback(self, interaction: MessageInteraction, repo: FromDishka[UserRepo]):
        ...

Selects

from disnake import ui, MessageInteraction, SelectOption
from dishka_disnake.ui import Select, select


class MySelect(Select):
    def __init__(self):
        super().__init__(placeholder="My Select", options=[
            SelectOption(label="Option 1", value="1"),
            SelectOption(label="Option 2", value="2"),
        ])

    async def callback(self, interaction: MessageInteraction, repo: FromDishka[UserRepo]):
        ...


class MyView(ui.View):
    def __init__(self):
        super().__init__()
        self.add_item(MySelect())

    @select(placeholder="My Select")
    async def my_select_callback(self, interaction: MessageInteraction, repo: FromDishka[UserRepo]):
        ...

similar with UserSelect, RoleSelect, MentionableSelect, ChannelSelect and StringSelect

Modals

from disnake import ModalInteraction
from dishka_disnake.ui import Modal, modal


class MyModal(Modal):
    def __init__(self):
        super().__init__(title="My Modal", components=[
            TextInput(label="My Input", style=TextInputStyle.short),
            TextInput(label="My Input Description", style=TextInputStyle.paragraph),
        ])

    async def callback(self, interaction: ModalInteraction, repo: FromDishka[UserRepo]):
        ...

Notes

  • The usage of commands, buttons, selects, and modals is identical to Disnake.
  • The main difference is the import path (from dishka_disnake instead of from disnake).
  • Make sure to call setup_dishka before initializing your bot, passing the asynchronous container to properly configure Dishka integration.
  • DishkaCog automatically patches Disnake's parameter validation when imported, so no additional setup is required.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dishka_disnake-0.1.6.tar.gz (30.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dishka_disnake-0.1.6-py3-none-any.whl (43.9 kB view details)

Uploaded Python 3

File details

Details for the file dishka_disnake-0.1.6.tar.gz.

File metadata

  • Download URL: dishka_disnake-0.1.6.tar.gz
  • Upload date:
  • Size: 30.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dishka_disnake-0.1.6.tar.gz
Algorithm Hash digest
SHA256 d69bdc24b257ddea3acc12a0c889faa5112ca0a9acb91ac92815dadd6d9d999a
MD5 fe757fcf8a2dcd29070c7d0a81929255
BLAKE2b-256 6dc941070f6aa6058fe7df2542a5a4df51db69ffac3707e569df7c9625b461db

See more details on using hashes here.

Provenance

The following attestation bundles were made for dishka_disnake-0.1.6.tar.gz:

Publisher: release.yml on NodusPanic/dishka-disnake

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dishka_disnake-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: dishka_disnake-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 43.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dishka_disnake-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 3b9ddc39009e264a805508bf5072447a2d621987f27b5e4eee02e4efbdda34f3
MD5 add03261aa36c1ea75a5c6405bc73aba
BLAKE2b-256 924903a63611db7c1ede65c8886634ab954709680e79d3f51c30145e32c084b8

See more details on using hashes here.

Provenance

The following attestation bundles were made for dishka_disnake-0.1.6-py3-none-any.whl:

Publisher: release.yml on NodusPanic/dishka-disnake

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page