【问题标题】:How to send a direct message when bot is ready机器人准备好后如何发送直接消息
【发布时间】:2018-09-25 08:09:08
【问题描述】:

我正在尝试让机器人在准备好后向我发送 dm:

@bot.event
async def on_ready():
    print("Ready when you are")
    print("I am running on: " + bot.user.name)
    print("With the ID: " + bot.user.id)
    await bot.send_message(discord.PrivateChannel(4803), "Ready", tts=True)

我怎样才能做到这一点?

谢谢。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    您可以告诉它使用您的 id 向您发送消息(顺便说一下,discord.py 0.16 中的 id 是字符串,而不是整数)。如果您必须在其他地方重用此代码,其他机器人不一定能够访问您和此机器人之间的私人通道。比如:

    @bot.event
    async def on_ready():
        print("Ready when you are")
        print("I am running on: " + bot.user.name)
        print("With the ID: " + bot.user.id)
        owner = await bot.get_user_info("Your ID")
        await bot.send_message(owner, "Ready", tts=True)
    

    【讨论】:

    • 收到此错误 discord.errors.InvalidArgument:Destination 必须是 Channel、PrivateChannel、User 或 Object。接收生成器
    • @SachaTerry 哎呀,get_user_info 是协程,必须是 awaited。
    • @SachaTerry 我更改了答案中的代码。只需在调用bot.get_user_info前添加await
    猜你喜欢
    • 2020-09-02
    • 2021-03-21
    • 2021-09-10
    • 2016-02-13
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 2017-11-06
    • 2022-01-03
    相关资源
    最近更新 更多