AttributeError: 'str' object has no attribute 'kwargs' even when I am not using kwargs

207 Views Asked by At

I am using interactions.py to make a discord bot. In one of its commands, I am calling a function called whitelist_add, which is giving me this error. I don't understand it, because I never used kwargs anywhere in the code. Looking at the traceback, I think it is a problem with ctx, but still, I never use kwargs.

Here is whitelist_add:

async def whitelist_add(mc_username, cmd_author):
    result = await fetch_player(mc_username)
    check_success = result[0]
    if not check_success:
        return False, "Could not whitelist. Check your username input"

    mc_username, uuid, _, = result
    whitelist, _ = await whitelist_get()
    whitelist['mc_users'].append(mc_username)
    whitelist['mc_uuids'].append(uuid)
    whitelist['discord_ids'].append(cmd_author)
    await quick_dump(whitelist)

    response = await send_rcon(f"whitelist add {mc_username}")
    return True, f"Success: {response}"

fetch_player and send_rcon are other functions I have in another file.

This is the code where I am getting the AttributeError:

async def admin_add(ctx: SlashContext, minecraft_username: str, discord_id: str = ""):
#if the user is an admin, they can whitelist other users
    
    response1 = await whitelist_add("Test", 0)

    if not response1[0]:
        print(response1)
        await ctx.send("Error")

And the traceback

Ignoring exception in cmd `/whitelist administrator-add`:
Traceback (most recent call last):
  File "C:\Users\name\OneDrive\Python\Mc_discord_bot\venv\Lib\site-packages\interactions\client\client.py", line 1897, in __dispatch_interaction
    response = await callback
               ^^^^^^^^^^^^^^
  File "C:\Users\name\OneDrive\Python\Mc_discord_bot\venv\Lib\site-packages\interactions\client\client.py", line 1768, in _run_slash_command
    return await command(ctx, **ctx.kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\name\OneDrive\Python\Mc_discord_bot\venv\Lib\site-packages\interactions\models\internal\command.py", line 132, in __call__
    await self.call_callback(self.callback, context)
  File "C:\Users\name\OneDrive\Python\Mc_discord_bot\venv\Lib\site-packages\interactions\models\internal\application_commands.py", line 833, in call_callback
    return await self.call_with_binding(callback, ctx, *new_args, **new_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\name\OneDrive\Python\Mc_discord_bot\venv\Lib\site-packages\interactions\models\internal\callback.py", line 43, in call_with_binding
    return await callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\name\OneDrive\Python\Mc_discord_bot\main.py", line 307, in admin_add
    response1 = await whitelist_add("Test", 0)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\name\OneDrive\Python\Mc_discord_bot\venv\Lib\site-packages\interactions\models\internal\command.py", line 132, in __call__
    await self.call_callback(self.callback, context)
  File "C:\Users\name\OneDrive\Python\Mc_discord_bot\venv\Lib\site-packages\interactions\models\internal\application_commands.py", line 804, in call_callback
    kwargs_copy = ctx.kwargs.copy()
                  ^^^^^^^^^^
AttributeError: 'str' object has no attribute 'kwargs'
1

There are 1 best solutions below

1
JBYoshi On

The traceback shows that the ctx.kwargs line is in the file venv\Lib\site-packages\interactions\models\internal\application_commands.py, which is part of the interactions.py command handler. That part of the code does in fact use ctx.kwargs, which is where the error is coming from.

I don't think that code is usually supposed to run in the middle of your own command code. What I think is happening is that you marked whitelist_add as @slash_command, which tells interactions.py to make a new slash command for just the whitelist_add function.

If you want whitelist_add to be used both as part of admin_add and as its own command, then you need to add a ctx: SlashContext parameter to it, and when you call it from admin_add, you need to include ctx in the call.

If you only want the admin_add command, and you don't want whitelist_add to be a separate slash command too, you'll need to remove the @slash_command line from right before the whitelist_add function.