ctx 值不正确

问题描述 投票:0回答:1

我有这个代码:

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())

错误出现在“marry”命令中,这就是它返回的内容:

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

我该怎么办?我建议该功能之外还有错误。

我尝试在这里写“contextlib = True”:

@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

但它犯了一个错误:

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'
python discord discord.py bots
1个回答
0
投票

应用程序命令(使用 client.tree.command 装饰器包装的命令)采用 https://discordpy.readthedocs.io/en/stable/interactions/api.html?highlight=commands#discord.app_commands 中定义的交互

作为他们的第一个参数不是 Context 对象。 在交互上,您必须关联不在 Context 对象上的属性和方法。

© www.soinside.com 2019 - 2024. All rights reserved.