Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import hackchat
  4. import subprocess
  5. import threading
  6.  
  7. # call "dc", eval the expression and return the result
  8. def eval(expr):
  9.     print(expr)
  10.     proc = subprocess.Popen(
  11.         ['dc'],stdout=subprocess.PIPE,
  12.         stdin=subprocess.PIPE,
  13.         stderr=subprocess.PIPE)
  14.     proc.stdin.write((expr + '\n').encode("iso-8859-1"))
  15.     proc.stdin.close()
  16.     result = proc.stderr.read().decode("iso-8859-1").strip()
  17.     result = result + "\n" + proc.stdout.read().decode("iso-8859-1").strip()
  18.     print(result)
  19.     return result
  20.  
  21. # called when a message was received
  22. def message_got(chat, message, sender):
  23.     print(message)
  24.     if message.split()[0]==".dc":
  25.         chat.send_message(eval(message[3:]))
  26.  
  27. def thread_function(name):
  28.     x = input()
  29.     chat.send_message(x)
  30.  
  31. x = threading.Thread(target=thread_function, args=(1,))
  32. x.start()
  33.    
  34. # create new bot
  35. chat = hackchat.HackChat("dcbot", "programming")
  36. chat.on_message += [message_got]
  37. chat.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement