Steven Smith revised this gist . Go to revision
1 file changed, 43 insertions, 39 deletions
savebot.py renamed to channelsave.py
| @@ -1,10 +1,11 @@ | |||
| 1 | 1 | #!/usr/bin/env python3 | |
| 2 | 2 | import discord | |
| 3 | - | import peewee | |
| 4 | 3 | from peewee import * | |
| 4 | + | from argparse import ArgumentParser | |
| 5 | 5 | ||
| 6 | 6 | client = discord.Client() | |
| 7 | 7 | db = SqliteDatabase("logs.db") | |
| 8 | + | args = None | |
| 8 | 9 | ||
| 9 | 10 | class BaseModel(Model): | |
| 10 | 11 | class Meta: | |
| @@ -39,50 +40,54 @@ except OperationalError: | |||
| 39 | 40 | pass | |
| 40 | 41 | ||
| 41 | 42 | @client.event | |
| 42 | - | async def on_message(message): | |
| 43 | - | if message.author != client.user: | |
| 43 | + | async def on_ready(): | |
| 44 | + | print("Logged in as {} ({})".format(client.user.name, client.user.id)) | |
| 45 | + | if client.user.bot: | |
| 46 | + | print(discord.utils.oauth_url(client.user.id, permissions=discord.Permissions(74752))) | |
| 47 | + | target = client.get_channel(args.channel) | |
| 48 | + | if not target: | |
| 49 | + | print("Channel not found") | |
| 50 | + | await client.logout() | |
| 44 | 51 | return | |
| 45 | - | if message.content.startswith("save"): | |
| 46 | - | await client.delete_message(message) | |
| 47 | - | x = 0 | |
| 48 | - | server = None | |
| 49 | - | channel = None | |
| 50 | - | authors = {} | |
| 51 | - | cache = [] | |
| 52 | - | try: | |
| 53 | - | async for logline in client.logs_from(message.channel, limit=30000): | |
| 52 | + | x = 0 | |
| 53 | + | server = None | |
| 54 | + | channel = None | |
| 55 | + | authors = {} | |
| 56 | + | cache = [] | |
| 57 | + | try: | |
| 58 | + | async for logline in client.logs_from(target, limit=args.limit): | |
| 59 | + | if logline.server: | |
| 54 | 60 | if not server: | |
| 55 | 61 | try: | |
| 56 | 62 | server = Server.get(id=logline.server.id) | |
| 57 | 63 | except Server.DoesNotExist: | |
| 58 | 64 | server = Server.create(id=logline.server.id, name=logline.server.name) | |
| 59 | - | if not channel: | |
| 60 | - | try: | |
| 61 | - | channel = Channel.get(id=logline.channel.id) | |
| 62 | - | except Channel.DoesNotExist: | |
| 63 | - | channel = Channel.create(id=logline.channel.id, name=logline.channel.name, server=server) | |
| 64 | - | if not logline.author.id in authors: | |
| 65 | - | try: | |
| 66 | - | authors[logline.author.id] = Author.get(id=logline.author.id) | |
| 67 | - | except Author.DoesNotExist: | |
| 68 | - | authors[logline.author.id] = Author.create(id=logline.author.id, username=logline.author.name, discriminator=logline.author.discriminator) | |
| 69 | - | cache.append(dict(author=authors[logline.author.id], channel=channel, server=server, content=logline.content, id=logline.id, timestamp=logline.timestamp)) | |
| 70 | - | x += 1 | |
| 71 | - | if len(cache) >= 50: | |
| 72 | - | Message.insert_many(cache).upsert().execute() | |
| 73 | - | cache = [] | |
| 74 | - | print(x, end="\r") | |
| 75 | - | finally: | |
| 76 | - | Message.insert_many(cache).upsert().execute() | |
| 77 | - | print("Saved {} messages from {}".format(x, message.channel.name)) | |
| 78 | - | ||
| 79 | - | @client.event | |
| 80 | - | async def on_ready(): | |
| 81 | - | print("Logged in as {} ({})".format(client.user.name, client.user.id)) | |
| 82 | - | if client.user.bot: | |
| 83 | - | print(discord.utils.oauth_url(client.user.id, permissions=discord.Permissions(74752))) | |
| 65 | + | if not channel: | |
| 66 | + | try: | |
| 67 | + | channel = Channel.get(id=logline.channel.id) | |
| 68 | + | except Channel.DoesNotExist: | |
| 69 | + | channel = Channel.create(id=logline.channel.id, name=logline.channel.name, server=server) | |
| 70 | + | if not logline.author.id in authors: | |
| 71 | + | try: | |
| 72 | + | authors[logline.author.id] = Author.get(id=logline.author.id) | |
| 73 | + | except Author.DoesNotExist: | |
| 74 | + | authors[logline.author.id] = Author.create(id=logline.author.id, username=logline.author.name, discriminator=logline.author.discriminator) | |
| 75 | + | cache.append(dict(author=authors[logline.author.id], channel=channel, server=server, content=logline.content, id=logline.id, timestamp=logline.timestamp)) | |
| 76 | + | x += 1 | |
| 77 | + | if len(cache) >= 50: | |
| 78 | + | Message.insert_many(cache).upsert().execute() | |
| 79 | + | cache = [] | |
| 80 | + | print(x, end="\r") | |
| 81 | + | finally: | |
| 82 | + | Message.insert_many(cache).upsert().execute() | |
| 83 | + | print("Saved {} messages from {}".format(x, target.name)) | |
| 84 | + | await client.logout() | |
| 84 | 85 | ||
| 85 | 86 | try: | |
| 87 | + | parser = ArgumentParser() | |
| 88 | + | parser.add_argument("channel") | |
| 89 | + | parser.add_argument("--limit", default=10**15, type=int) | |
| 90 | + | args = parser.parse_args() | |
| 86 | 91 | with open(".bottoken") as f: | |
| 87 | 92 | creds = f.read() | |
| 88 | 93 | if " " in creds or "." in creds: | |
| @@ -90,5 +95,4 @@ try: | |||
| 90 | 95 | else: | |
| 91 | 96 | client.run(creds.strip()) | |
| 92 | 97 | finally: | |
| 93 | - | db.close() | |
| 94 | - | client.close() | |
| 98 | + | db.close() | |
Steven Smith revised this gist . Go to revision
1 file changed, 31 insertions, 20 deletions
savebot.py
| @@ -5,8 +5,6 @@ from peewee import * | |||
| 5 | 5 | ||
| 6 | 6 | client = discord.Client() | |
| 7 | 7 | db = SqliteDatabase("logs.db") | |
| 8 | - | # install MySQLdb or pymysqlthen uncomment | |
| 9 | - | # db = MySQLDatabase("logs", host="localhost", port=3306, user="root", passwd="") | |
| 10 | 8 | ||
| 11 | 9 | class BaseModel(Model): | |
| 12 | 10 | class Meta: | |
| @@ -47,22 +45,35 @@ async def on_message(message): | |||
| 47 | 45 | if message.content.startswith("save"): | |
| 48 | 46 | await client.delete_message(message) | |
| 49 | 47 | x = 0 | |
| 50 | - | async for logline in client.logs_from(message.channel, limit=30000): | |
| 51 | - | try: | |
| 52 | - | author = Author.get(id=logline.author.id) | |
| 53 | - | except Author.DoesNotExist: | |
| 54 | - | author = Author.create(id=logline.author.id, username=logline.author.name, discriminator=logline.author.discriminator) | |
| 55 | - | try: | |
| 56 | - | server = Server.get(id=logline.server.id) | |
| 57 | - | except Server.DoesNotExist: | |
| 58 | - | server = Server.create(id=logline.server.id, name=logline.server.name) | |
| 59 | - | try: | |
| 60 | - | channel = Channel.get(id=logline.channel.id) | |
| 61 | - | except Channel.DoesNotExist: | |
| 62 | - | channel = Channel.create(id=logline.channel.id, name=logline.channel.name, server=server) | |
| 63 | - | msg = Message.create(author=author, channel=channel, server=server, content=logline.content, id=logline.id, timestamp=logline.timestamp) | |
| 64 | - | x += 1 | |
| 65 | - | print(x, end="\r") | |
| 48 | + | server = None | |
| 49 | + | channel = None | |
| 50 | + | authors = {} | |
| 51 | + | cache = [] | |
| 52 | + | try: | |
| 53 | + | async for logline in client.logs_from(message.channel, limit=30000): | |
| 54 | + | if not server: | |
| 55 | + | try: | |
| 56 | + | server = Server.get(id=logline.server.id) | |
| 57 | + | except Server.DoesNotExist: | |
| 58 | + | server = Server.create(id=logline.server.id, name=logline.server.name) | |
| 59 | + | if not channel: | |
| 60 | + | try: | |
| 61 | + | channel = Channel.get(id=logline.channel.id) | |
| 62 | + | except Channel.DoesNotExist: | |
| 63 | + | channel = Channel.create(id=logline.channel.id, name=logline.channel.name, server=server) | |
| 64 | + | if not logline.author.id in authors: | |
| 65 | + | try: | |
| 66 | + | authors[logline.author.id] = Author.get(id=logline.author.id) | |
| 67 | + | except Author.DoesNotExist: | |
| 68 | + | authors[logline.author.id] = Author.create(id=logline.author.id, username=logline.author.name, discriminator=logline.author.discriminator) | |
| 69 | + | cache.append(dict(author=authors[logline.author.id], channel=channel, server=server, content=logline.content, id=logline.id, timestamp=logline.timestamp)) | |
| 70 | + | x += 1 | |
| 71 | + | if len(cache) >= 50: | |
| 72 | + | Message.insert_many(cache).upsert().execute() | |
| 73 | + | cache = [] | |
| 74 | + | print(x, end="\r") | |
| 75 | + | finally: | |
| 76 | + | Message.insert_many(cache).upsert().execute() | |
| 66 | 77 | print("Saved {} messages from {}".format(x, message.channel.name)) | |
| 67 | 78 | ||
| 68 | 79 | @client.event | |
| @@ -74,8 +85,8 @@ async def on_ready(): | |||
| 74 | 85 | try: | |
| 75 | 86 | with open(".bottoken") as f: | |
| 76 | 87 | creds = f.read() | |
| 77 | - | if "." in creds: | |
| 78 | - | client.run(creds.strip(), bot=False) | |
| 88 | + | if " " in creds or "." in creds: | |
| 89 | + | client.run(*creds.split(None, 1), bot=False) | |
| 79 | 90 | else: | |
| 80 | 91 | client.run(creds.strip()) | |
| 81 | 92 | finally: | |
Steven Smith revised this gist . Go to revision
1 file changed, 83 insertions
savebot.py(file created)
| @@ -0,0 +1,83 @@ | |||
| 1 | + | #!/usr/bin/env python3 | |
| 2 | + | import discord | |
| 3 | + | import peewee | |
| 4 | + | from peewee import * | |
| 5 | + | ||
| 6 | + | client = discord.Client() | |
| 7 | + | db = SqliteDatabase("logs.db") | |
| 8 | + | # install MySQLdb or pymysqlthen uncomment | |
| 9 | + | # db = MySQLDatabase("logs", host="localhost", port=3306, user="root", passwd="") | |
| 10 | + | ||
| 11 | + | class BaseModel(Model): | |
| 12 | + | class Meta: | |
| 13 | + | database = db | |
| 14 | + | ||
| 15 | + | class Author(BaseModel): | |
| 16 | + | discriminator = CharField() | |
| 17 | + | id = CharField(primary_key=True) | |
| 18 | + | username = CharField() | |
| 19 | + | ||
| 20 | + | class Server(BaseModel): | |
| 21 | + | name = CharField() | |
| 22 | + | id = CharField(primary_key=True) | |
| 23 | + | ||
| 24 | + | class Channel(BaseModel): | |
| 25 | + | name = CharField() | |
| 26 | + | server = ForeignKeyField(Server, related_name="channels") | |
| 27 | + | id = CharField(primary_key=True) | |
| 28 | + | ||
| 29 | + | class Message(BaseModel): | |
| 30 | + | author = ForeignKeyField(Author, related_name="messages") | |
| 31 | + | server = ForeignKeyField(Server, related_name="messages") | |
| 32 | + | channel = ForeignKeyField(Channel, related_name="messages") | |
| 33 | + | content = CharField() | |
| 34 | + | id = CharField(primary_key=True) | |
| 35 | + | timestamp = DateField() | |
| 36 | + | ||
| 37 | + | db.connect() | |
| 38 | + | try: | |
| 39 | + | db.create_tables([Author, Server, Channel, Message]) | |
| 40 | + | except OperationalError: | |
| 41 | + | pass | |
| 42 | + | ||
| 43 | + | @client.event | |
| 44 | + | async def on_message(message): | |
| 45 | + | if message.author != client.user: | |
| 46 | + | return | |
| 47 | + | if message.content.startswith("save"): | |
| 48 | + | await client.delete_message(message) | |
| 49 | + | x = 0 | |
| 50 | + | async for logline in client.logs_from(message.channel, limit=30000): | |
| 51 | + | try: | |
| 52 | + | author = Author.get(id=logline.author.id) | |
| 53 | + | except Author.DoesNotExist: | |
| 54 | + | author = Author.create(id=logline.author.id, username=logline.author.name, discriminator=logline.author.discriminator) | |
| 55 | + | try: | |
| 56 | + | server = Server.get(id=logline.server.id) | |
| 57 | + | except Server.DoesNotExist: | |
| 58 | + | server = Server.create(id=logline.server.id, name=logline.server.name) | |
| 59 | + | try: | |
| 60 | + | channel = Channel.get(id=logline.channel.id) | |
| 61 | + | except Channel.DoesNotExist: | |
| 62 | + | channel = Channel.create(id=logline.channel.id, name=logline.channel.name, server=server) | |
| 63 | + | msg = Message.create(author=author, channel=channel, server=server, content=logline.content, id=logline.id, timestamp=logline.timestamp) | |
| 64 | + | x += 1 | |
| 65 | + | print(x, end="\r") | |
| 66 | + | print("Saved {} messages from {}".format(x, message.channel.name)) | |
| 67 | + | ||
| 68 | + | @client.event | |
| 69 | + | async def on_ready(): | |
| 70 | + | print("Logged in as {} ({})".format(client.user.name, client.user.id)) | |
| 71 | + | if client.user.bot: | |
| 72 | + | print(discord.utils.oauth_url(client.user.id, permissions=discord.Permissions(74752))) | |
| 73 | + | ||
| 74 | + | try: | |
| 75 | + | with open(".bottoken") as f: | |
| 76 | + | creds = f.read() | |
| 77 | + | if "." in creds: | |
| 78 | + | client.run(creds.strip(), bot=False) | |
| 79 | + | else: | |
| 80 | + | client.run(creds.strip()) | |
| 81 | + | finally: | |
| 82 | + | db.close() | |
| 83 | + | client.close() | |