python - 如何将所有固定消息插入文件?
问题描述
@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'
如何将所有固定的消息显示到文件中?
任何帮助表示赞赏:)
解决方案
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
变量只是一个以换行符作为分隔符的列表,它以一种很好的方式显示消息数据(随意更改它)。
参考:
推荐阅读
- node.js - Cucumberjs - 在步骤定义中调用异步方法
- sql - 如何计算 PostgreSQL 中所有归属于 - 及其子级的实体?
- iis - 通过 Azure Pipelines 将 .NET Core Web 应用程序部署到 IIS 服务器时出现 500 错误
- git - 错误:git 在 SUSE 12.4 上找不到 https 的远程助手
- sql - Oracle中如何复用部分SQL代码
- javascript - 如何解决代理后面的 npm install 期间的连接问题?
- oauth - 使用 OAuth2 和 OpenID Connect 进行会话管理/撤销
- node.js - Express.js 应用程序错误:尝试访问某些路由时浏览器卡在加载状态
- python - Jetbrains Pycharm 2019.2 & Win10 WSL:无法同步实际的 python 包
- awk - 将“u”转换为“t”而不更改 FASTA 中的标题