简体   繁体   中英

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.

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:

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

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM