avoid5bot.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. """
  13. from matrix_bot_api.matrix_bot_api import MatrixBotAPI
  14. from matrix_bot_api.mregex_handler import MRegexHandler
  15. from matrix_bot_api.mcommand_handler import MCommandHandler
  16. import time
  17. import sys
  18. import subprocess
  19. USERNAME = "avoid5bot" # Bot's username
  20. PASSWORD = subprocess.getoutput("pass show matrix/matrix.org/avoid5bot") # Bot's password
  21. SERVER = "https://matrix.org" # Matrix server URL
  22. def check_e(room, event):
  23. try:
  24. print("got a fifthglyph!")
  25. r = event["content"]["body"].replace("e","_").replace("E","_")
  26. if r.replace("_","") == "":
  27. room.send_text("{}, that wasn't funny.".format(event["sender"]))
  28. else:
  29. room.send_text("{}: Your last post contains fifthglyphs: \"{}\"".format(event["sender"], r))
  30. except Exception as e:
  31. print(e.message)
  32. pass
  33. def main():
  34. print("Connecting...")
  35. bot = MatrixBotAPI(USERNAME, PASSWORD, SERVER)
  36. e_handler = MRegexHandler("(?i)e", check_e)
  37. bot.add_handler(e_handler)
  38. print("Starting polling...")
  39. bot.start_polling()
  40. while True:
  41. time.sleep(1)
  42. if __name__ == "__main__":
  43. main()