#Error when using file: discord.Attatchment in slash commands

1 messages · Page 1 of 1 (latest)

charred pike
#
import discord
from discord.ext import commands
from bot.sessions import PDFSession
from bot.cogs.commands.signup import signup_required
from discord import option

class AskPDF(commands.Cog):
    """
    PDF session with the bot to get answers to questions from a PDF
    """
    
    def __init__(self, bot):
        self.bot = bot

    @discord.slash_command(
        description="Ask a question about a any PDF file (big files may take a while to process)",
    )
    @option(
        "pdf",
        discord.Attachment,
        description="A file to attach to the message",
    )
    @option(
        "question",
        str,
        description="The question to ask",
    )
    @signup_required
    async def askpdf(self, ctx, file: discord.Attachment, question: str):
        
        interaction: discord.Interaction = await ctx.respond(embed=discord.Embed(
            title="Searching through the PDF...",
        ))
        
        response = await PDFSession(ctx, self.bot, file).get_response(file, question)
        
        await interaction.edit(embed=discord.Embed(
            title=question,
            description=response
        ))
atomic cape
#

?

charred pike
#

This error is not caused by me

#

there must be something wrong in handling files in slash commands

silk wolf
#

You're definitely doing something wrong

atomic cape
#

what is this? response = await PDFSession(ctx, self.bot, file).get_response(file, question)

charred pike
#

this code is not running when the error happens

charred pike
atomic cape
#

is the same if you remove here the discord.Attachment?

charred pike
#

yes I added this because I thought maybe its needed

atomic cape
#

you already using it here

charred pike
#

I can remove all @options it does not change smt

#

await self.callback(self.cog, ctx, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: AskPDF.askpdf() got an unexpected keyword argument 'file'

#

this part is strange

#

why is file unexpected?

atomic cape
#

what is the py-cord version?

#

do you have something else installed?

charred pike
#

upgraded to 2.4.1 was 2.4.0 before

#

py-cord==2.4.1
python-dotenv==1.0.0
openai==0.27.0
PyNaCl==1.5.0
cffi==1.15.1
pycparser==2.21
gtts==2.3.1
sqlalchemy==2.0.4
alembic==1.10.2
topggpy==1.4.0
langchain==0.0.139
chromadb==0.3.21
aiofiles==23.1.0

atomic cape
#

you have topggpy installed

charred pike
#

is this a problem?

atomic cape
#

uninstall it, same for py-cord and reinstall py-cord

charred pike
#

Ahh yeah true i know what you mean but I am uninstalling discord.py

#

still same

atomic cape
charred pike
#

I need this package

#

but I can try

#

still same didnt changed something

#

I changed the code a bit and realized somthing is really wrong with passing input args

class AskPDF(commands.Cog):
    """
    PDF session with the bot to get answers to questions from a PDF
    """
    
    def __init__(self, bot):
        self.bot = bot

    @discord.slash_command(description="Ask a question about a any PDF file (big files may take a while to process)")
    @signup_required
    async def askpdf(self, ctx, question: str):
        """ Ask a question about a PDF file """
        interaction: discord.Interaction = await ctx.respond(embed=discord.Embed(
            title="Searching through the PDF...",
        ))
        response = question
        # response = await PDFSession(ctx, self.bot, file).get_response(file, question)
        
        await interaction.edit(embed=discord.Embed(
            title=question,
            description=response
        ))
#

Got same error:

Ignoring exception in command askpdf:
Traceback (most recent call last):
  File "/Users/fungus/dev/miniforge3/envs/vgpt/lib/python3.11/site-packages/discord/commands/core.py", line 124, in wrapped
    ret = await coro(arg)
          ^^^^^^^^^^^^^^^
  File "/Users/fungus/dev/miniforge3/envs/vgpt/lib/python3.11/site-packages/discord/commands/core.py", line 978, in _invoke
    await self.callback(self.cog, ctx, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: AskPDF.askpdf() got an unexpected keyword argument 'question'

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

Traceback (most recent call last):
  File "/Users/fungus/dev/miniforge3/envs/vgpt/lib/python3.11/site-packages/discord/bot.py", line 1114, in invoke_application_command
    await ctx.command.invoke(ctx)
  File "/Users/fungus/dev/miniforge3/envs/vgpt/lib/python3.11/site-packages/discord/commands/core.py", line 375, in invoke
    await injected(ctx)
  File "/Users/fungus/dev/miniforge3/envs/vgpt/lib/python3.11/site-packages/discord/commands/core.py", line 132, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: TypeError: AskPDF.askpdf() got an unexpected keyword argument 'question'
atomic cape
#

what is that?

hybrid violet
#

it is a separate library, not a fork of dpy

hybrid violet
atomic cape
near orchid
#

You can use the github version of topggpy that doesn’t have discord.py in it’s dependencies.

atomic cape
#

wait

#

only the pip one need d.py?

near orchid
hybrid violet
hybrid violet
near orchid
hybrid violet
#

pretty sure they did pip uninstall discord.py

#

unless topggpy has discord in its requirements instead of dpy

near orchid
#

iirc discord.py installs discord. Uninstalling discord.py does not uninstall discord

near orchid
hybrid violet