【问题标题】:Discord.py bot.get_context() does not return ctx objectDiscord.py bot.get_context() 不返回 ctx 对象
【发布时间】:2021-05-21 12:50:54
【问题描述】:

我想从 on_message() 运行命令,但 bot.get_context() 返回协程对象,而不是上下文。 代码给出的错误:await bot.get_context(message).invoke(bot.get_command("do_something")) AttributeError: 'coroutine' 对象没有属性 'invoke'

import discord, asyncio
from discord.ext import commands, tasks
bot_token = ""
bot = commands.Bot(command_prefix=("!"))

@bot.event
async def on_message(message):
    #print(type(bot.get_context(message)))
    if message.content == "do something":
        await bot.get_context(message).invoke(bot.get_command("do_something"))
    await bot.process_commands(message)

@bot.command()
async def do_something(ctx):
    #print(type(ctx))
    await ctx.send("Working")

bot.run(bot_token)

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    您在协程对象上调用.invoke(),而不是最终返回的对象(上下文)。

    要么将上下文存储在一个变量中,然后调用它:

    ctx = await bot.get_context(message)
    ctx.invoke...
    

    或者,这是我没有尝试过并且实际上不知道它是否有效(但它可能!),试试这个:

    (await bot.get_context(message)).invoke...
    

    【讨论】:

      猜你喜欢
      • 2021-03-12
      • 2020-05-02
      • 2022-10-03
      • 1970-01-01
      • 2022-07-20
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多