【问题标题】:how to insert all pinned messages to a file?如何将所有固定消息插入文件?
【发布时间】:2021-06-24 21:47:00
【问题描述】:
@commands.command()
async def pins(self, ctx,channel:discord.TextChannel):
    pins=await channel.pins()
    f = BytesIO(bytes(str(pins), encoding="utf-8"))
    file = discord.File(fp=f, filename="pins.txt")
    await ctx.send(file=file)

嘿,所以我正在尝试创建一个命令,它将获取频道的所有固定消息并将其插入文件中。

我的问题是await channel.pins() 不显示固定消息。而是显示有关消息和频道的信息。

Message id=823211324343245892304 channel=<TextChannel id=7242345725488373823 name='channel' position=2 nsfw=True news=False category_id=724975728488373821> type=<MessageType.default: 0> author=<Member id=8048242432231802419 name='author's name'

如何将所有固定的消息显示到文件中?

任何帮助表示赞赏:)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    TextChannel.pins 返回discord.Message 实例的列表,您可以遍历它们并且只获取消息内容和/或有关消息的其他信息,一个简单的示例是:

    pins = await channel.pins()
    data = '\n'.join([f"[{m.created_at}][{m.author}]: {m.content}" for m in pins])
    buffer = BytesIO(bytes(data, encoding="utf-8"))
    f = discord.File(buffer, filename="pins.txt")
    await ctx.send(file=f)
    

    data 变量只是一个以换行符作为分隔符的列表,它以一种很好的方式显示消息数据(随意更改它)。

    参考:

    【讨论】:

    • 谢谢。你说得对,我应该更好地阅读文档然后问。将在 5m 中添加你的评论作为答案
    猜你喜欢
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-01
    • 1970-01-01
    • 2020-01-02
    • 2021-09-14
    • 1970-01-01
    相关资源
    最近更新 更多