Moontalk server and client (provided by many parties)
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

83 lignes
3.1KB

  1. import re
  2. from datetime import datetime, timedelta
  3. from moonchat import *
  4. class Bot:
  5. def __init__(self, chat: Moonchat, command_matcher: re.Pattern):
  6. self.chat = chat
  7. self.command_matcher = command_matcher
  8. self.commands = dict()
  9. self.last_annoyed = datetime.now()
  10. self.seen = dict()
  11. async def handle_incoming(self):
  12. async for message in self.chat.messages():
  13. now = datetime.now()
  14. if message.nickname == 'Server':
  15. continue # ignore the server
  16. last_seen = self.seen.get(message.nickname, None)
  17. if last_seen:
  18. seen_delta = now - last_seen
  19. if seen_delta > timedelta(hours=2):
  20. last_seen = None
  21. if not last_seen:
  22. if (now - self.last_annoyed) > timedelta(minutes=10):
  23. await self.chat.send_message(f"hello {message.nickname}! i am a robot. say [help]")
  24. self.last_annoyed = now
  25. self.seen[message.nickname] = datetime.now()
  26. match = self.command_matcher.search(message.content)
  27. if not match:
  28. continue # ignore not our messages
  29. command = match.groupdict().get('command', None)
  30. if not command:
  31. continue # ????
  32. split = command.split()
  33. if not len(split):
  34. continue # ????????????
  35. exector = split[0]
  36. command_function = self.commands.get(exector, None)
  37. if command_function:
  38. await command_function(self, message, split)
  39. else:
  40. await self.chat.send_message(f"{message.nickname}: sorry that's not a valid command")
  41. async def who_command(bot: Bot, message: MoonchatMessage, args):
  42. """See recent users"""
  43. now = datetime.now()
  44. result = "Users from last 1hour: "
  45. for username, last_seen in bot.seen.items():
  46. delta: timedelta = (now - last_seen)
  47. if delta < timedelta(hours=1):
  48. minutes, seconds = divmod(delta.seconds, 60)
  49. result += f"{username}({minutes}m{seconds}s), "
  50. await bot.chat.send_message(result)
  51. async def whoami(bot: Bot, message: MoonchatMessage, args):
  52. """Print your nickname"""
  53. await bot.chat.send_message(message.nickname)
  54. async def help(bot: Bot, message: MoonchatMessage, args):
  55. command = args[1] if len(args) > 1 else None
  56. command_function = bot.commands.get(command, None)
  57. if command_function:
  58. await bot.chat.send_message(f"{command}: {command_function.__doc__}")
  59. return
  60. command_list = ', '.join(bot.commands.keys())
  61. await bot.chat.send_message(f"Commands available: {command_list}")
  62. matcher = re.compile(r"\[(?P<command>[\w\s]+)\]")
  63. async def main():
  64. chat = await Moonchat.connect("7ks473deh6ggtwqsvbqdurepv5i6iblpbkx33b6cydon3ajph73sssad.onion", 50000)
  65. bot = Bot(chat, matcher)
  66. bot.commands["help"] = help
  67. bot.commands['who'] = who_command
  68. bot.commands['whoami'] = whoami
  69. await chat.send_message("i am a robot! do [help]")
  70. await bot.handle_incoming()
  71. if __name__ == "__main__":
  72. import asyncio
  73. asyncio.run(main())