import discord import io import sys client = discord.Client() @client.event async def on_ready(): print('logged in as {0.user}'.format(client), file=sys.stderr) @client.event async def on_message(message): if message.author == client.user: return if not isinstance(message.channel, discord.TextChannel): return if not (client.user in message.mentions or [client.user] in [role.members for role in message.role_mentions]): return await message.channel.send('generating channel archive...') with io.StringIO() as buf: async for m in message.channel.history(limit=None, oldest_first=True): buf.write('@{0.author.name}#{0.author.discriminator} {0.created_at}\n{0.clean_content}\n\n'.format(m)) buf.seek(0) f = discord.File(buf, '{}_{}.txt'.format(message.channel.guild.name, message.channel.name)) await message.channel.send('complete:', files=[f]) with open('token') as f: token = f.read().strip() client.run(token)