简体   繁体   English

如何将所有固定消息插入文件?

[英]how to insert all pinned messages to a file?

@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)

Hey so I'm trying to make a command where it will get all pinned messages of a channel and insert the into a file.嘿,所以我正在尝试创建一个命令,它将获取频道的所有固定消息并将其插入文件中。

My problem is that await channel.pins() does not display the pinned messages.Instead it displays information about the message and the channel.我的问题是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'

How can I display all the pinned messages into the file?如何将所有固定的消息显示到文件中?

Any help is appreciated:)任何帮助表示赞赏:)

TextChannel.pins returns a list of discord.Message instances, you can loop through them and only get the message content and/or other information about the message, a simple example would be: 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)

The data variable is just a list joined with a newline as the delimiter which displays the message data in a nice way (feel free to change it as you wish). data变量只是一个以换行符作为分隔符的列表,它以一种很好的方式显示消息数据(随意更改它)。

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 pytelegrambotapi 在电报聊天中获取所有固定消息? - How to get all the pinned messages in a telegram chat using pytelegrambotapi? 使用 discord.py 获取频道中的所有固定消息 - using discord.py to get all pinned messages in a channel to 使用 Telethon 检索多个固定消息 - Retrieving multiple pinned messages with Telethon 如何在所有频道发送消息 - how to send messages all channel 执行并自动将所有错误消息存储到文件中 - Executing and automatically storing all error messages into file 如何使用Python或JAVA读取/写入备份文件中的所有Whatsapp消息 - how to Read /Write all Whatsapp Messages in backup file using Python or JAVA 当所有其他消息都将重定向到日志文件时,如何打印一些语句以进行控制台? - How to print some statement to console while all others messages will be redirected to a log file? 如何将GStreamer消息记录到文件 - How to log GStreamer messages to a file 有没有办法检查固定消息,并且只使用 discord.py 清除某些成员消息? - Is there a way to do a check for pinned messages, and only purge a certain members messages using discord.py? 如何将日志消息写入文件 - How to write logging messages to a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM