pythondiscord-interactions

How do I fix this AttributeError in my interactions.py code?


In my interactions.py code, I have two different bot commands. One to get information about a user's products and one to retrieve a certain product. Whenever I run my code, I get AttributeError: 'Command' object has no attribute '_options'. This is my code:

import interactions, requests

bot = interactions.Client(token="tokenhere")


@bot.command(
    name="products",
    description="Get a list of all your products",
    scope=scopeid,
)
@interactions.option('Your roblox UserID',name='UserID', type=interactions.OptionType.INTEGER , required=True)
@bot.command(
    name="retrieve",
    description="Retrieve a certain product that you own",
    scope=scopeid,
    options=[
        interactions.Option(
            name='product',
            description='the product you would like to retrieve',
            type=interactions.OptionType.STRING,
            required=True
        )
    ]
)
async def products(ctx: interactions.CommandContext, userid: int):
    a = str(requests.get('https://jedistuff22.pythonanywhere.com/products/' + str(userid)).text)
    await ctx.send(a)
async def retrieve(ctx: interactions.CommandContext, product: str):
    a = ctx.author.user.username + '#' + ctx.author.user.discriminator
    print(a)
    await ctx.send(a)

bot.start()

For some reason, my code works when i just have one command but just flat out stops working when I have two.

I'm really stumped with this error. I have been looking online for about the past day and I am yet to find something that could help me.


Solution

  • You've used the same bot.command() decorator to define two bot commands. Because the bot.command() method can only define one command at a time, you must define each command separately.

    import interactions
    import requests
    
    bot = interactions.Client(token="tokenhere")
    
    @bot.command(
        name="products",
        description="Get a list of all your products",
        scope=scopeid,
    )
    @interactions.option('Your roblox UserID',name='UserID', type=interactions.OptionType.INTEGER , required=True)
    async def products(ctx: interactions.CommandContext, userid: int):
        a = str(requests.get('https://jedistuff22.pythonanywhere.com/products/' + str(userid)).text)
        await ctx.send(a)
    
    @bot.command(
        name="retrieve",
        description="Retrieve a certain product that you own",
        scope=scopeid,
    )
    @interactions.option(
        name='product',
        description='the product you would like to retrieve',
        type=interactions.OptionType.STRING,
        required=True
    )
    async def retrieve(ctx: interactions.CommandContext, product: str):
        a = ctx.author.user.username + '#' + ctx.author.user.discriminator
        print(a)
        await ctx.send(a)
    
    bot.start()