dice.py 611 B

12345678910111213141516171819202122
  1. from nio.events.room_events import RoomMessageText
  2. from nio.rooms import MatrixRoom
  3. from random import SystemRandom
  4. async def command_dice(room: MatrixRoom, event: RoomMessageText, bot):
  5. bot.logger.log("Rolling a dice...")
  6. try:
  7. sides = int(event.body.split()[2])
  8. except ValueError:
  9. sides = 6
  10. if sides < 2:
  11. await bot.send_message(room, f"A dice with {sides} sides? How would that work?", True)
  12. else:
  13. result = SystemRandom().randint(1, sides)
  14. body = f"Rolling a {sides}-sided dice... It's a {result}!"
  15. await bot.send_message(room, body, True)