Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. import ast
  2. import random
  3. import re
  4. import sys
  5.  
  6. import discord
  7. from PyQt5 import QtWidgets, Qt, QtCore
  8.  
  9.  
  10. def calc_random(dice, dice_type):
  11. sum_var = 0
  12. arr = []
  13. for i in range(0, dice):
  14. roll = random.SystemRandom().randrange(0, dice_type) + 1
  15. arr.append(roll)
  16. sum_var += roll
  17. return sum_var, dice, dice_type, arr
  18.  
  19.  
  20. class DiscordClient(discord.Client):
  21. def __init__(self, **kwargs):
  22. super(DiscordClient, self).__init__(**kwargs)
  23.  
  24. def handle_message(self, message):
  25. for mention in message.mentions:
  26. if mention.id == self.user.id:
  27. matches = re.findall('(\d+)d(\d+)', message.content)
  28. out = re.sub('<@\w+>', '', message.content)
  29. results = ''
  30. for match in matches:
  31. dsum, dice, d_type, res = calc_random(int(match[0]), int(match[1]))
  32. results += str(dice) + 'd' + str(d_type) + ' -> ' + str(res) + '\n'
  33. out = out.replace(str(dice) + 'd' + str(d_type), str(dsum), 1)
  34. out = re.sub('^\s+', '', out)
  35. out2 = ''
  36. try:
  37. out2 = results + out + ' = ' + str(ast.literal_eval(out))
  38. except:
  39. out2 = results + out
  40.  
  41. self.send_message(message.channel, out2)
  42. return
  43.  
  44. client = DiscordClient()
  45. app = None
  46.  
  47.  
  48. class CloseApp(QtWidgets.QWidget):
  49. def __init__(self):
  50. super(CloseApp, self).__init__()
  51. self.setWindowTitle('Discord Bot')
  52. self.setVisible(False)
  53. self.quit = QtWidgets.QPushButton()
  54. self.quit.setText('Exit')
  55. self.quit.clicked.connect(app.quit)
  56. self.l = QtWidgets.QVBoxLayout()
  57. self.l.addWidget(self.quit)
  58. self.setLayout(self.l)
  59.  
  60. def logged_in(self):
  61. self.setVisible(True)
  62. self.show()
  63.  
  64.  
  65. class LoginWindow(QtWidgets.QWidget):
  66. started = QtCore.pyqtSignal()
  67. failure = QtCore.pyqtSignal()
  68.  
  69. def __init__(self):
  70. super(LoginWindow, self).__init__()
  71. self.setWindowTitle('Discord Bot')
  72. self.l = QtWidgets.QFormLayout()
  73. self.username = QtWidgets.QLineEdit()
  74. self.password = QtWidgets.QLineEdit()
  75. self.password.setEchoMode(QtWidgets.QLineEdit.Password)
  76. self.login = QtWidgets.QPushButton('Login')
  77. self.l.addRow('Email:', self.username)
  78. self.l.addRow('Password:', self.password)
  79. self.l.addRow('', self.login)
  80. self.setLayout(self.l)
  81. self.password.returnPressed.connect(self.login.click)
  82. self.login.clicked.connect(self.do_login)
  83. self.failure.connect(self.reset_and_show)
  84.  
  85. def reset_and_show(self):
  86. self.password.setText('')
  87. self.show()
  88.  
  89. def do_login(self):
  90. try:
  91. self.close()
  92. client.login(self.username.text(), self.password.text())
  93. discordRunner.start()
  94. self.started.emit()
  95. except discord.LoginFailure as e:
  96. msg = QtWidgets.QMessageBox()
  97. msg.setModal(True)
  98. msg.setText('Cannot Login. Are username and password correct?')
  99. msg.setStandardButtons(QtWidgets.QMessageBox.Ok)
  100. msg.exec_()
  101. print(e)
  102. self.failure.emit()
  103.  
  104.  
  105. class DiscordRunner(QtCore.QThread):
  106. def __init__(self):
  107. QtCore.QThread.__init__(self)
  108.  
  109. def __del__(self):
  110. if client.is_logged_in:
  111. client.logout()
  112. self.wait()
  113.  
  114. def run(self):
  115. client.run()
  116.  
  117.  
  118. discordRunner = DiscordRunner()
  119. trayIcon = None
  120.  
  121. if __name__ == "__main__":
  122. app = Qt.QApplication(sys.argv)
  123. trayIcon = CloseApp()
  124. ui = LoginWindow()
  125. ui.started.connect(trayIcon.logged_in)
  126. ui.show()
  127. app.setQuitOnLastWindowClosed(False)
  128. app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement