【问题标题】:Discord Bot cannot send messages because send is not an attribute of contextDiscord Bot 无法发送消息,因为发送不是上下文的属性
【发布时间】:2019-05-27 16:35:13
【问题描述】:

我正在根据文档以正确的方式重新实现我的命令,使用上下文和命令装饰器而不是 on_message 侦听器,将我的命令转移过来有点痛苦,但感谢文档非常有用。不幸的是,我遇到了一个阻止我发送消息的问题...

搬家之前,我发消息的方式是这样的

@client.event
async def on_message(message):
    if message.author.id in AdminID:
    await client.send_message(message.channel. 'message')

不幸的是,这不适用于新格式,因为没有可以从中获取信息的消息参数,您需要做的是使用 ctx(上下文)参数,而不是根据文档看起来像这样

@bot.command()
async def test(ctx, arg):
    await ctx.send(arg)

虽然机器人识别命令并去那里,但我无法发送消息,因为 send 不是 ctx 的属性,此代码已从文档中删除,我是否遗漏了什么?有人可以帮我解决这个问题吗?谢谢

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    您正在查看与您正在使用的库不同版本的文档。

    您正在使用版本0.16,也称为“异步”分支。该分支的文档是here

    您正在阅读1.0 版本的文档,也称为重写分支。

    你的命令看起来像

    @bot.command(pass_context=True)
    async def test(ctx):
        if ctx.message.author.id in AdminID:
            await client.send_message(ctx.message.channel, 'message')
    

    【讨论】:

    • 非常感谢!应该猜到它会是这样的,但是在发送部分,你用句号而不是逗号分隔每个参数,这会导致它崩溃,但如果用逗号替换,它会完美运行!
    猜你喜欢
    • 2021-03-02
    • 2021-07-06
    • 1970-01-01
    • 2020-12-01
    • 2021-01-12
    • 2021-06-05
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多