How do you get the main "function" of a command in discord.py?

Viewed 128

I'm trying to create a command that sends the code of another command in discord.py. For example, if I had a command for rock paper scissors and i ran .code rps, It would send the code for the rps command. Here is my code so far:

import inspect 
import discord
from discord.ext.commands import Bot, cooldown, BucketType, CommandOnCooldown
from discord.ext import commands, tasks

@bot.command(hidden=True)
@commands.is_owner()
async def code(ctx, *, command_name):
    """Sends code of a command"""

    com = bot.get_command(command_name)
    if com is None:
        await ctx.reply(f"Bruh that isn't a command", mention_author=False)
    elif com:
        source = inspect.getsource(com)
        try:
            await ctx.reply(f"```\n{source}\n```", mention_author=False)
        except discord.HTTPException:
            await ctx.reply(f"That command's code is too long for me to send lmao", mention_author=False)
            print(source)

But there's an error because it can't find the source code of a command type

Ignoring exception in command code:
Traceback (most recent call last):
  File "/Users/Bello/PycharmProjects/larry/venv3.8/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "/Users/Bello/PycharmProjects/larry/venv3.8/test.py", line 351, in code
    source = inspect.getsource(com)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py", line 985, in getsource
    lines, lnum = getsourcelines(object)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py", line 967, in getsourcelines
    lines, lnum = findsource(object)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py", line 780, in findsource
    file = getsourcefile(object)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py", line 696, in getsourcefile
    filename = getfile(object)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py", line 676, in getfile
    raise TypeError('module, class, method, function, traceback, frame, or '
TypeError: module, class, method, function, traceback, frame, or code object was expected, got Command

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/Bello/PycharmProjects/larry/venv3.8/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "/Users/Bello/PycharmProjects/larry/venv3.8/lib/python3.8/site-packages/discord/ext/commands/core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/Users/Bello/PycharmProjects/larry/venv3.8/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: module, class, method, function, traceback, frame, or code object was expected, got Command

Is there a way to find the source code of a command object or figure out a loophole that lets me do that? Thanks!!

1 Answers

Simply use the callback attribute

func = com.callback
source = inspect.getsource(func)

Reference:

Related