Skip to main content

Typed commands for discord.py

Project description

discord-ext-typed-commands

License Unit tests CodeQL Analysis pre-commit Checked with mypy Code style: black

This package contains a discord.py extension to provide classes to more easily use typed commands

Installation

pip install discord-ext-typed-commands

NOTE: Because discord.py uses namespace packages for its extensions, mypy must be configured to use namespace packages either with the --namespace-packages command line flag, or by setting namespace_packages = True in your mypy configuration file. See the import discovery section of the mypy documentation for more details.

Usage

The most common usage will be in connection with discord.py-stubs in order to allow bot authors to use the command classes from discord.py while also using the generics defined in the stubs:

from typing import Any, Type, TypeVar, Union, cast, overload

import discord
from discord.ext import typed_commands

OtherContextType = TypeVar('OtherContextType', bound=typed_commands.Context)


class MyContext(typed_commands.Context):
    async def send_with_hello(self, text: str) -> None:
        await self.send(f'Hello! {text}')


class MyCog(typed_commands.Cog[MyContext]):
    @typed_commands.command()
    async def speak(self, ctx: MyContext, text: str) -> None:
        await ctx.send_with_hello(text)


class MyBot(typed_commands.Bot[MyContext]):
    @overload
    async def get_context(self, message: discord.Message) -> MyContext:
        ...

    @overload
    async def get_context(
        self, message: discord.Message, *, cls: Type[OtherContextType]
    ) -> OtherContextType:
        ...

    async def get_context(
        self,
        message: discord.Message,
        *,
        cls: Type[OtherContextType] = cast(Any, MyContext),
    ) -> Union[MyContext, OtherContextType]:
        return await super().get_context(message, cls=cls)


my_bot = MyBot('$')
my_bot.add_cog(MyCog())
my_bot.run('...')

NOTE: Because it is not a runtime dependency, discord.py-stubs will need to be explicitly installed to type check a bot while it is being developed. It is recommended that the stubs be added as a development dependency using your preferred method of package management.

Development

Make sure you have poetry installed.

poetry install
poetry run pre-commit install --hook-type pre-commit --hook-type post-checkout

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

discord-ext-typed-commands-1.0.2.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file discord-ext-typed-commands-1.0.2.tar.gz.

File metadata

File hashes

Hashes for discord-ext-typed-commands-1.0.2.tar.gz
Algorithm Hash digest
SHA256 6e5ea78ffe3666261a817b36cab153e94ab311c24cc10fcdaab740f90a9d1f2a
MD5 807ad59c8db3616cda73b15c0569a46f
BLAKE2b-256 fa08cf10d249f6b6162cf07f1c47ce79234f3a67bed53b67a4ae417be3799037

See more details on using hashes here.

Provenance

File details

Details for the file discord_ext_typed_commands-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for discord_ext_typed_commands-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ec6b6972ab5f24bdb870ca27ee4156afba67c5f22aa4be748ce04ca0c0defe44
MD5 428819220049c8d8f8c20d49a5e301e8
BLAKE2b-256 d57010af8b2e2c4b8ea51a2e5a3b89286031358d64decbebc9cb6ad8dbe53a18

See more details on using hashes here.

Provenance

Supported by

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