Advertisement
Guest User

Untitled

a guest
Apr 12th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import time
  4. import discord
  5. import asyncio
  6. import random
  7. import threading
  8. import re as _re
  9.  
  10. import common.credentials.discord as _discord
  11. from parser import parse_message
  12.  
  13. def start_discord(logger, discord_queue, credentials):
  14.  
  15. username = credentials['username']
  16. password = credentials['password']
  17. covername = credentials['covername']
  18.  
  19. # set the thread name to the spy cover, for logging purposes
  20. thread = threading.current_thread()
  21. thread.setName(covername)
  22.  
  23. wait = random.randint(1,90)
  24.  
  25. msg = 'waiting {0} seconds before connecting'.format(wait)
  26. logger.info(msg)
  27.  
  28. loop = asyncio.new_event_loop()
  29. asyncio.set_event_loop(loop)
  30. client = discord.Client(loop=loop, cache_auth=False)
  31.  
  32. @client.event
  33. async def on_ready():
  34. msg = 'discord ready'
  35. logger.info(msg)
  36.  
  37. loginmsg = '```diff' + '\n' + '+ {0} online```'.format(covername)
  38. client.change_presence(game=None, status='invisible', afk=False)
  39. discord_queue.put(loginmsg)
  40.  
  41. @client.event
  42. async def on_server_remove():
  43. msg = 'discord disconnected'
  44. logger.error(msg)
  45.  
  46. loginmsg = '```diff' + '\n' + '+ {0} offline [remove]```'.format(covername)
  47. discord_queue.put(loginmsg)
  48.  
  49. @client.event
  50. async def on_server_unavailable():
  51. msg = 'discord user unavailable'
  52. logger.error(msg)
  53.  
  54. loginmsg = '```diff' + '\n' + '+ {0} offline [unavailable]```'.format(covername)
  55. discord_queue.put(loginmsg)
  56.  
  57. @client.event
  58. async def on_message(message):
  59.  
  60. if message.mention_everyone:
  61. msg = 'discord ping'
  62. logger.info(msg)
  63.  
  64. author_clean = message.author
  65. from_channel = message.channel
  66. detail = message.server
  67.  
  68. message_parsed = parse_message(covername, message.content, from_channel, author_clean, detail)
  69. discord_queue.put(message_parsed)
  70.  
  71. attempt = 1
  72.  
  73. while attempt < 5:
  74.  
  75. if attempt < 5:
  76. msg = "connection attempt {0} for {1}".format(attempt, covername)
  77.  
  78. try:
  79. client.run(username, password)
  80. attempt = 6
  81. except Exception as error:
  82. msg = 'discord connection error on user {0}: {1}'.format(username, error)
  83. logger.critical(msg)
  84. loginmsg = '```diff' + '\n' + '- {0} offline [unable to connect]```'.format(covername)
  85. discord_queue.put(loginmsg)
  86. attempt += 1
  87.  
  88. time.sleep(10)
  89. loop = asyncio.new_event_loop()
  90. asyncio.set_event_loop(loop)
  91. client = discord.Client(loop=loop, cache_auth=False)
  92.  
  93. # clean up
  94. try:
  95. client.logout()
  96. except Exception as e:
  97. print('htf? '.format(e))
  98. # thanks https://stackoverflow.com/questions/37278647/fire-and-forget-python-async-await/37345564#37345564
  99. pending = asyncio.Task.all_tasks()
  100. for task in pending:
  101. task.cancel()
  102. # Now we should await task to execute it's cancellation.
  103. # Cancelled task raises asyncio.CancelledError that we can suppress:
  104. with suppress(asyncio.CancelledError):
  105. loop.run_until_complete(task)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement