【问题标题】:How can I make my Discord bot look like it's typing?如何让我的 Discord 机器人看起来像是在打字?
【发布时间】:2020-09-23 17:01:31
【问题描述】:

我有一个运行 discord.py 的 Discord 机器人,当我使用帮助功能时,消息之间有时会有几秒钟的小延迟。当一个活生生的用户在 Discord 上打字时,它通常会显示 3 个交替阴影的小点,同时说 Example 正在打字。我怎样才能用我的机器人显示这个? 我有一个现在不起作用的功能,即 和


async def balabala (ctx):
    async with ctx.typing():
        await message.channel.send(prefix + "random [Value 1] [Value 2]  -  Gives a random number between Value 1 and Value 2, inclusive")
    await ctx.send(prefix + "choose [Value 1] (Value 2) (Value 3) etc.  -  Chooses a random value from the list of values you provide") # what you want to do after typing    
@client.event
async def on_message(message):
    if message.content.lower() == prefix + "help":
        await message.channel.send("The prefix for this bot is " + prefix)
        await message.channel.send("Note: Do not actually enter [](required) or ()(Not required)")
        await message.channel.send(prefix + "help  -  Shows this message" )
        await message.channel.send("Hello  -  Gives a hello back, because you are so nice")
        await message.channel.send(prefix + "invite - Gives an invite to the bot, and Canary versions")
        
        await balabala()

出现的错误是

  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "discordbotcanary.py", line 45, in on_message
    await balabala()
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 354, in __call__
    return await self.callback(*args, **kwargs)
TypeError: balabala() missing 1 required positional argument: 'ctx'

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您将能够打开 Messageable 对象(例如 TextChannel、DMChannel)的上下文管理器并在其中执行您的任务。

    我以asyncio.sleep() 为例延迟:

    @bot.command()
    async def hello(ctx):
        async with ctx.typing():
            await asyncio.sleep(2) # some tasks go here, the sleep is just for a delay
        await ctx.send(f"Hello, {ctx.author.mention}!") # what you want to do after typing
    

    参考资料:

    【讨论】:

    • 在我完成帮助功能的输入后,如何让我的机器人不再看起来像是在输入?
    • 抱歉,我应该取消最后一部分的缩进!查看编辑^
    • 运行此命令时出现错误提示缺少 1 个必需的位置参数:'ctx' 如何解决此问题
    • 请问您能用您目前拥有的代码编辑您的问题吗?
    猜你喜欢
    • 2016-02-16
    • 1970-01-01
    • 2021-06-13
    • 2021-08-04
    • 2021-03-22
    • 2021-04-09
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    相关资源
    最近更新 更多