savebot.py
· 3.2 KiB · Python
Raw
#!/usr/bin/env python3
import discord
import peewee
from peewee import *
client = discord.Client()
db = SqliteDatabase("logs.db")
class BaseModel(Model):
class Meta:
database = db
class Author(BaseModel):
discriminator = CharField()
id = CharField(primary_key=True)
username = CharField()
class Server(BaseModel):
name = CharField()
id = CharField(primary_key=True)
class Channel(BaseModel):
name = CharField()
server = ForeignKeyField(Server, related_name="channels")
id = CharField(primary_key=True)
class Message(BaseModel):
author = ForeignKeyField(Author, related_name="messages")
server = ForeignKeyField(Server, related_name="messages")
channel = ForeignKeyField(Channel, related_name="messages")
content = CharField()
id = CharField(primary_key=True)
timestamp = DateField()
db.connect()
try:
db.create_tables([Author, Server, Channel, Message])
except OperationalError:
pass
@client.event
async def on_message(message):
if message.author != client.user:
return
if message.content.startswith("save"):
await client.delete_message(message)
x = 0
server = None
channel = None
authors = {}
cache = []
try:
async for logline in client.logs_from(message.channel, limit=30000):
if not server:
try:
server = Server.get(id=logline.server.id)
except Server.DoesNotExist:
server = Server.create(id=logline.server.id, name=logline.server.name)
if not channel:
try:
channel = Channel.get(id=logline.channel.id)
except Channel.DoesNotExist:
channel = Channel.create(id=logline.channel.id, name=logline.channel.name, server=server)
if not logline.author.id in authors:
try:
authors[logline.author.id] = Author.get(id=logline.author.id)
except Author.DoesNotExist:
authors[logline.author.id] = Author.create(id=logline.author.id, username=logline.author.name, discriminator=logline.author.discriminator)
cache.append(dict(author=authors[logline.author.id], channel=channel, server=server, content=logline.content, id=logline.id, timestamp=logline.timestamp))
x += 1
if len(cache) >= 50:
Message.insert_many(cache).upsert().execute()
cache = []
print(x, end="\r")
finally:
Message.insert_many(cache).upsert().execute()
print("Saved {} messages from {}".format(x, message.channel.name))
@client.event
async def on_ready():
print("Logged in as {} ({})".format(client.user.name, client.user.id))
if client.user.bot:
print(discord.utils.oauth_url(client.user.id, permissions=discord.Permissions(74752)))
try:
with open(".bottoken") as f:
creds = f.read()
if " " in creds or "." in creds:
client.run(*creds.split(None, 1), bot=False)
else:
client.run(creds.strip())
finally:
db.close()
client.close()
| 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 | |
| 9 | class BaseModel(Model): |
| 10 | class Meta: |
| 11 | database = db |
| 12 | |
| 13 | class Author(BaseModel): |
| 14 | discriminator = CharField() |
| 15 | id = CharField(primary_key=True) |
| 16 | username = CharField() |
| 17 | |
| 18 | class Server(BaseModel): |
| 19 | name = CharField() |
| 20 | id = CharField(primary_key=True) |
| 21 | |
| 22 | class Channel(BaseModel): |
| 23 | name = CharField() |
| 24 | server = ForeignKeyField(Server, related_name="channels") |
| 25 | id = CharField(primary_key=True) |
| 26 | |
| 27 | class Message(BaseModel): |
| 28 | author = ForeignKeyField(Author, related_name="messages") |
| 29 | server = ForeignKeyField(Server, related_name="messages") |
| 30 | channel = ForeignKeyField(Channel, related_name="messages") |
| 31 | content = CharField() |
| 32 | id = CharField(primary_key=True) |
| 33 | timestamp = DateField() |
| 34 | |
| 35 | db.connect() |
| 36 | try: |
| 37 | db.create_tables([Author, Server, Channel, Message]) |
| 38 | except OperationalError: |
| 39 | pass |
| 40 | |
| 41 | @client.event |
| 42 | async def on_message(message): |
| 43 | if message.author != client.user: |
| 44 | 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): |
| 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() |
| 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))) |
| 84 | |
| 85 | try: |
| 86 | with open(".bottoken") as f: |
| 87 | creds = f.read() |
| 88 | if " " in creds or "." in creds: |
| 89 | client.run(*creds.split(None, 1), bot=False) |
| 90 | else: |
| 91 | client.run(creds.strip()) |
| 92 | finally: |
| 93 | db.close() |
| 94 | client.close() |