Moontalk server and client (provided by many parties)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
2.1KB

  1. # TODO: add a fucking scoreboard or something? this is a copy of a Espernet bot.
  2. from moonchat import *
  3. import sys
  4. import random
  5. import io
  6. class Bot:
  7. def __init__(self, chat: Moonchat, words: list[str]):
  8. self.chat = chat
  9. self.words = words
  10. async def next_winner(self, word: str, limit: float):
  11. try:
  12. async with asyncio.timeout(limit):
  13. async for message in self.chat.messages():
  14. if word in message.content.lower():
  15. return message
  16. except TimeoutError:
  17. return None
  18. async def handle_incoming(self):
  19. limit = 60
  20. async for message in self.chat.messages():
  21. if message.nickname == 'Server':
  22. continue # ignore the server
  23. if "!scramble" not in message.content:
  24. continue
  25. print(f"GAME REQUESTED: {message=}")
  26. selected_word = random.choice(self.words)
  27. scrambled_word = ''.join(random.sample(selected_word, len(selected_word)))
  28. print(f"GAME START: {scrambled_word} is {selected_word}")
  29. await self.chat.send_message(f"Unscramble in {limit} seconds to win! The word is: {scrambled_word}.")
  30. winner = await self.next_winner(selected_word, limit)
  31. print(f"GAME OVER: {winner=}")
  32. if winner:
  33. await self.chat.send_message(f"The word was {selected_word}. {winner.nickname} wins!")
  34. else:
  35. await self.chat.send_message(f"Time's up! The word was {selected_word}. No one wins.")
  36. async def main(words: list[str]):
  37. chat = await Moonchat.connect("7ks473deh6ggtwqsvbqdurepv5i6iblpbkx33b6cydon3ajph73sssad.onion", 50000)
  38. bot = Bot(chat, words)
  39. await chat.send_message("To play scramble say: !scramble")
  40. await bot.handle_incoming()
  41. def load_words(file: io.TextIOBase):
  42. for line in file:
  43. line = line.strip().lower()
  44. if "'" not in line and len(line) == 5:
  45. yield line
  46. if __name__ == "__main__":
  47. import asyncio
  48. words = list(load_words(sys.stdin))
  49. print(f"Loaded {len(words)} words")
  50. asyncio.run(main(words))