【问题标题】:Discord Py Sending Live Python OutputDiscord Py 发送实时 Python 输出
【发布时间】:2021-11-05 06:44:00
【问题描述】:

我目前正在开发一个文本到图像的生成机器人,我希望它能够发送实时消息,其中包含有关生成剩余时间、迭代和生成速度的信息,所有这些都可以找到在控制台中,但我不知道如何将控制台输出发送到不和谐。

【问题讨论】:

    标签: discord console discord.py output


    【解决方案1】:

    我假设您正在使用 print("output") 打印到控制台。

    您可以通过Messageable#send()将消息发送到 Discord
    例如发送到频道将是

    await channel.send(content="content")
    

    您可以通过多种方式获取Messageable

    # In a cog
    channel = ctx.channel
    # In a cog you can directly use ctx.send() however
    
    # In a on_message event
    channel = message.channel
    

    现在很简单,将您要打印的任何内容发送到控制台到 Discord

    # Printing to console
    print("hello world")
    
    # Sending to message
    await channel.send(content="hello world")
    

    这种方法可能有点“垃圾邮件”,因此更好的做法是编辑消息

    # channel.send() returns the message
    
    # Printing
    print("status update")
    
    # Sending message
    message = await channel.send(content="status update")
    
    # [Image generation code]
    
    # Printing
    print("new status update")
    
    # Editing message
    await message.edit(content="new status update")
    

    【讨论】:

    • 理论上,如果我知道正在打印的变量,我想打印的变量来自导入,这就是为什么我正在寻找一条消息以捕获所有输出
    • 也许可以显示您帖子中已有的代码?
    猜你喜欢
    • 2020-04-07
    • 2021-06-17
    • 1970-01-01
    • 2019-02-20
    • 2020-10-01
    • 2021-05-28
    • 1970-01-01
    • 2021-09-15
    • 2020-10-26
    相关资源
    最近更新 更多