Guest User

Untitled

a guest
Oct 20th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import sqlite3
  4. import sys
  5. import datetime
  6.  
  7. import discord
  8.  
  9. client = discord.Client()
  10.  
  11. @client.async_event
  12. async def on_ready():
  13. if __name__ == '__main__':
  14. print("Logged in")
  15. if len(sys.argv) == 2:
  16. await make_logs(sys.argv[1],sys.argv[1])
  17. elif len(sys.argv) > 2:
  18. await make_logs(sys.argv[1],sys.argv[2])
  19. await client.logout()
  20.  
  21. async def make_logs(guildid, filename):
  22. database = None
  23. def scrub(s):
  24. while "-" in s:
  25. s = s.replace("-", "")
  26. return s
  27. def check_table_exists(tablename):
  28. dbcur = database.cursor()
  29. dbcur.execute("""
  30. SELECT name FROM sqlite_master WHERE type='table' AND name='{0}';
  31. """.format(str(tablename).replace('\'', '\'\'')))
  32. if dbcur.fetchone():
  33. dbcur.close()
  34. return True
  35.  
  36. dbcur.close()
  37. return False
  38.  
  39. guild = client.get_guild(int(sys.argv[1]))
  40. database = sqlite3.connect("{}.sqlite".format(filename))
  41.  
  42. cursor = database.cursor()
  43. if check_table_exists('channels'):
  44. cursor.execute("""DROP TABLE channels""")
  45. cursor.execute("""CREATE TABLE channels(cid INTEGER, position INTEGER, name TEXT, topic TEXT, type TEXT)""")
  46. cursor.close()
  47. database.commit()
  48. for channel in guild.channels:
  49. if isinstance(channel, discord.TextChannel):
  50. cursor = database.cursor()
  51. cursor.execute(
  52. """INSERT INTO channels(cid, position, name, topic, type) VALUES (?, ?, ?, ?, ?)""",
  53. (channel.id, channel.position, channel.name, str(channel.topic), 'discord.TextChannel')
  54. )
  55. cursor.close()
  56. database.commit()
  57. if channel.permissions_for(guild.get_member(client.user.id)).read_message_history:
  58. sys.stdout.write("Logging {0}: Counting".format(channel.name))
  59. sys.stdout.flush()
  60. cursor = database.cursor()
  61. after = datetime.datetime(2015,3,1)
  62. if check_table_exists(channel.name):
  63. cursor.execute("""SELECT timestamp FROM `{0}` ORDER BY timestamp DESC LIMIT 1""".format(channel.name))
  64. try:
  65. after = datetime.datetime.strptime(cursor.fetchone()[0], "%Y-%m-%d %H:%M:%S.%f")
  66. except TypeError:
  67. pass
  68. else:
  69. cursor.execute("""CREATE TABLE `{0}`(uid INTEGER, uname TEXT, mid INTEGER, message TEXT, files TEXT, timestamp TEXT)""".format(channel.name))
  70. database.commit()
  71. count = 0
  72. msg_c = 0
  73. async for message in channel.history(limit=None,after=after):
  74. msg_c += 1
  75. print("\rLogging {0}: 0/{1} ".format(channel.name, msg_c), end="")
  76. async for message in channel.history(limit=None,after=after):
  77. at = ",".join([i.url for i in message.attachments])
  78. cursor.execute("""
  79. INSERT INTO `{0}`(uid, uname, mid, message, files, timestamp)
  80. VALUES (?, ?, ?, ?, ?, ?)""".format(channel.name), (message.author.id, message.author.name, message.id, message.content, at, message.created_at))
  81. count += 1
  82. if count % 200 == 0:
  83. database.commit()
  84. print("\rLogging {0}: {1}/{2}".format(channel.name, count, msg_c), end="")
  85. database.commit()
  86. print("\rLogging {0}: [DONE] ".format(channel.name))
  87. elif isinstance(channel, discord.VoiceChannel):
  88. cursor = database.cursor()
  89. cursor.execute(
  90. """INSERT INTO channels(cid, position, name, topic, type) VALUES (?, ?, ?, ?, ?)""",
  91. (channel.id, channel.position, channel.name, '', 'discord.VoiceChannel')
  92. )
  93. cursor.close()
  94. database.commit()
  95. print("LOGS FINISHED!")
  96.  
  97. if __name__ == '__main__':
  98. #bot=False may have to be changed if you are using a bot
  99. client.run(open('token.txt','r').read().split('\n')[0], bot=False)
Add Comment
Please, Sign In to add comment