Skip to content

App commands in cogs annotated with @staticmethod error when loading #9551

@Sophon96

Description

@Sophon96

Summary

When defining commands with the @discord.app_commands.command() decorator with methods in a cog decorated with @staticmethod, a TypeError is raised with message "command function must be coroutine function."

Reproduction Steps

  1. Create a cog
  2. Define an app command with a function signature such as
@discord.app_commands.command()
@staticmethod
async def my_command(interaction: discord.Interaction):
  # ...
  1. Add code to load the cog
  2. Get TypeError: command must be coroutine function when running (though no type errors are reported by Pylance).

Such an example is attached in images (and see below):
image
image

Minimal Reproducible Code

import discord
from discord.ext import commands


class MyCog(commands.Cog):
    @discord.app_commands.command()
    @staticmethod
    async def my_command(interaction: discord.Interaction):
        await interaction.response.send("You will never see this")


class MyBot(commands.Bot):
    async def setup_hook(self):
        await self.add_cog(MyCog())


bot = MyBot("!", intents=discord.Intents.default())
bot.run("TOKEN")

Expected Results

A command is registered, as usual.

Actual Results

Traceback (most recent call last):
  File "/app/minrepo.py", line 5, in <module>
    class MyCog(commands.Cog):
  File "/app/minrepo.py", line 6, in MyCog
    @discord.app_commands.command()
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/venv/lib/python3.11/site-packages/discord/app_commands/commands.py", line 1965, in decorator
    raise TypeError('command function must be a coroutine function')
TypeError: command function must be a coroutine function

Intents

Just the defaults (irrelevant)

System Information

- Python v3.11.5-final
- discord.py v2.3.2-final
- aiohttp v3.8.5
- system info: Linux 5.10.0-23-amd64 #1 SMP Debian 5.10.179-1 (2023-05-12)

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    as designedThis feature is working as intended

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions