Guest User

Untitled

a guest
Jan 10th, 2018
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. import configparser
  2.  
  3. import praw
  4.  
  5. import discord
  6. import asyncio
  7.  
  8. import time
  9.  
  10. config = configparser.ConfigParser()
  11. config.read("config.txt")
  12.  
  13. subname = "dota2"
  14. r = None
  15.  
  16. modAuthors = ['m4rx', 'klopjobacid', 'Decency', '0Hellspawn0',
  17. 'crimson589', 'Intolerable', 'lestye', 'coronaria',
  18. 'leafeator', 'VRCkid', 'JohnScofield', 'Pohka']
  19.  
  20. alreadySeen = []
  21.  
  22. r = praw.Reddit(client_id=config.get("config", "CLIENT_ID"),
  23. client_secret=config.get("config", "CLIENT_SECRET"),
  24. password=config.get("config", "BOT_PASSWORD"),
  25. user_agent='Dota 2 sidebar bot',
  26. username=config.get("config", "BOT_USERNAME"))
  27. subreddit = r.subreddit(subname)
  28.  
  29. def getModmail():
  30. new = subreddit.modmail.conversations(state='new')
  31. inprogress = subreddit.modmail.conversations(state='inprogress')
  32.  
  33. modmails = []
  34.  
  35. for c in new:
  36. m = list(c.messages)
  37. last = m[-1]
  38.  
  39. body = last.body_markdown
  40. if len(body) > 1024:
  41. body = body[:1021] + "..."
  42.  
  43. if last.id not in alreadySeen:
  44. alreadySeen.append(last.id)
  45.  
  46. modmails.append({"kind": "new", "title": c.subject, "author": last.author.name, "body": body, "url": "https://mod.reddit.com/mail/new/" + c.id})
  47.  
  48.  
  49. for c in inprogress:
  50. m = list(c.messages)
  51.  
  52. if len(m) < 2:
  53. continue
  54.  
  55. last = m[-1]
  56. prevReply = m[-2]
  57.  
  58. if last.author.name in modAuthors:
  59. continue
  60.  
  61. if last.id not in alreadySeen:
  62. alreadySeen.append(last.id)
  63.  
  64. body = last.body_markdown
  65. if len(body) > 1024:
  66. body = body[:1021] + "..."
  67.  
  68. prevBody = prevReply.body_markdown
  69. if len(prevBody) > 1024:
  70. prevBody = prevBody[:1021] + "..."
  71.  
  72. modmails.append({"kind": "inprogress", "title": c.subject, "author": last.author.name, "body": body, "prevAuthor": prevReply.author.name, "prevBody": prevBody, "url": "https://mod.reddit.com/mail/inprogress/" + c.id})
  73.  
  74. print(modmails)
  75. return modmails
  76.  
  77. client = discord.Client()
  78.  
  79. @client.event
  80. @asyncio.coroutine
  81. def on_ready():
  82. print('Logged in as')
  83. print(client.user.name)
  84. print(client.user.id)
  85. print('------')
  86.  
  87. # server = client.get_server("74287487459004416")
  88. # me = server.get_member_named("Vatyx")
  89.  
  90. while True:
  91. print("Checking modmail")
  92. modmails = getModmail()
  93.  
  94. for modmail in modmails:
  95. embed = discord.Embed()
  96.  
  97. if modmail["kind"] == "new":
  98. embed.add_field(name = "New Modmail", value="[" + modmail["title"] + "](" + modmail["url"] + ") from [/u/" + modmail["author"] + "](https://reddit.com/u/" + modmail["author"] + ")", inline=False)
  99. embed.add_field(name="Body", value=modmail["body"], inline=False)
  100.  
  101.  
  102. elif modmail["kind"] == "inprogress":
  103. embed.add_field(name = "New Modmail Reply", value="[" + modmail["title"] + "](" + modmail["url"] + ") from [/u/" + modmail["author"] + "](https://reddit.com/u/" + modmail["author"] + ")", inline=False)
  104.  
  105. embed.add_field(name = "Previous Reply by " + modmail["prevAuthor"], value=modmail["prevBody"], inline=False)
  106. embed.add_field(name = "New Reply", value=modmail["body"], inline=False)
  107.  
  108. yield from client.send_message(client.get_channel("140254555580530688"), embed=embed)
  109. # yield from client.send_message(me, embed=embed)
  110.  
  111. time.sleep(20)
  112.  
  113. client.run(config.get("config", "DISCORD_TOKEN"))
Add Comment
Please, Sign In to add comment