Skip to main content

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

Release files for discord-ext-typed-commands 1.0.3

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

Source distribution (sdist)

Source distribution for discord-ext-typed-commands 1.0.3
File Size Uploaded
discord-ext-typed-commands-1.0.3.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for discord-ext-typed-commands 1.0.3
File Interpreter ABI Platform
discord_ext_typed_commands-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 9.7 kB

Release files / discord-ext-typed-commands-1.0.3.tar.gz

Download URL discord-ext-typed-commands-1.0.3.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9bffb46dfc93b50356624bfe7756a4f0dad3a8933ffc160c9a44babaa7bc194d
BLAKE2b-256 checksum
How to use checksums
decf5c9dfab1cb22e66de1c6af5ec662d73ccffd15f86ee23aa0f5f4e7eaac87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.6 Linux/5.4.0-1032-azure

Release files / discord_ext_typed_commands-1.0.3-py3-none-any.whl

Download URL discord_ext_typed_commands-1.0.3-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d19e01bef9b256df7506d735ca7a42e4ecb49f8a68fc4536d4221e1921d8c5c
BLAKE2b-256 checksum
How to use checksums
d5ba0354a5bca1481c5aed309e9766ca10446b127918d83f8c79d1038d231fa2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.6 Linux/5.4.0-1032-azure

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

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