Search code examples
pythondiscorddiscord.pybots

Incorrect value of ctx


I've got this code:

import discord
from discord.ext import commands
import logging
import os
import asyncio
import configure
from discord import app_commands

intents = discord.Intents.all()
BOT_TOKEN = configure.config["token"]
BOT_NAME = configure.config["name"]

client = commands.Bot(intents=discord.Intents.all(), command_prefix="/")

@client.event
async def on_ready():
    print("Online")
    try:
        synced = await client.tree.sync()
        print(f"Synced {len(synced)} commands")
    except Exception as e:
        print(e)
@client.tree.command(name='marry', description="Suggest to marry" )
async def marry(ctx, user: discord.Member):
    print(f'{ctx}||{user}')
    ctx.reply(f'{ctx.author} make a proposal to marry {user}')
    return
async def setup():
    print('setting up...')

async def main():
    await setup()
    await client.start(BOT_TOKEN)

asyncio.run(main())

The mistake is in "marry" command, that's what it returns:

discord.interactions.Interaction object at 0x0000016160E87F10>||rayanutka
(in this case I just put myself in second argument);

What should I do? I suggest there are mistakes outside that function.

I tried to write "contextlib = True" here:

@client.tree.command(name='marry', description="Suggest to marry", contextlib = True)
async def marry(ctx, user: discord.Member):
    print(f'{ctx}||{user}')
    ctx.reply(f'{ctx.author} make a proposal to marry {user}')
    return

but it makes a mistake:

File "D:\pyt_projects\dnevnik\venv\test.py", line 24, in <module>
    @client.tree.command(name='marry', description="Suggest to marry", contextlib = True)
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: CommandTree.command() got an unexpected keyword argument 'contextlib'

Solution

  • Application commands (those wrapped with the client.tree.command decorator) take an Interaction as defined at https://discordpy.readthedocs.io/en/stable/interactions/api.html?highlight=commands#discord.app_commands

    as their first argument not a Context object. On Interactions you must use relevant attributes and methods that are not on Context objects.

    Perhaps you are looking for:

    @client.tree.command(name='marry', description="Suggest to marry" )
    async def marry(interaction, user: discord.Member):
        message = await interaction.response.send_message(f'{interaction.user.name} make a proposal to marry {user}')
        return