journal3/bot.py

40 lines
1.5 KiB
Python

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
if not message.author.guild_permissions.administrator:
await message.channel.send('Only administrators can request channel logs!')
return
await message.channel.send('generating channel archive...')
with io.StringIO() as buf:
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))
with open('token') as f:
token = f.read().strip()
client.run(token)