2020-09-11 16:46:44 -04:00
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
2021-06-06 14:25:06 -04:00
if not message . author . guild_permissions . administrator :
await message . channel . send ( ' Only administrators can request channel logs! ' )
return
2020-09-11 16:46:44 -04:00
await message . channel . send ( ' generating channel archive... ' )
with io . StringIO ( ) as buf :
2020-09-13 17:09:55 -04:00
try :
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 ] )
except discord . Forbidden :
await message . channel . send ( ' Looks like I don \' t have the right permissions :( Please make sure I have the " Read Message History " and " Attach Files " permissions for this channel. ' )
except discord . DiscordException as e :
await message . channel . send ( " Something went wrong: {} " . format ( e ) )
2020-09-11 16:46:44 -04:00
with open ( ' token ' ) as f :
token = f . read ( ) . strip ( )
client . run ( token )