diff --git a/dice.py b/dice.py new file mode 100644 index 0000000..ca57446 --- /dev/null +++ b/dice.py @@ -0,0 +1,34 @@ +from random import randint + +from pyparsing import Combine, OpAssoc, Word, infix_notation, nums, one_of + +SEPARATOR = "|" + + +class Dice: + def __init__(self, tokens): + split_tokens = tokens[0].split(SEPARATOR) + self.dice, self.faces = split_tokens[0], split_tokens[2] + + +def roll_parse(roll): + integer = Word(nums) + dice = Combine(integer + "d" + integer, join_string="|") + dice.set_parse_action(Dice) + + expr = infix_notation( + dice | integer, + [ + ("-", 1, OpAssoc.RIGHT), + (one_of("* /"), 2, OpAssoc.LEFT), + (one_of("+ -"), 2, OpAssoc.LEFT), + ], + ) + + return expr.parse_string(roll, parse_all=True) + + +async def roll(bot, mtch, room, message): + await bot.api.send_text_message( + room.room_id, str(roll_parse(" ".join(mtch.args()))) + ) diff --git a/flake.nix b/flake.nix index 9e055e5..7105a8b 100644 --- a/flake.nix +++ b/flake.nix @@ -73,6 +73,7 @@ (python.withPackages ( py: with py; [ mautrix + pyparsing python-cryptography-fernet-wrapper python-dotenv python-lsp-server diff --git a/main.py b/main.py index 3fc2214..9f096b8 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,11 @@ +from datetime import datetime + import simplematrixbotlib as matrix from dotenv import dotenv_values +import dice +import misc + DOTENV = dotenv_values(".env") PREFIX = "!" @@ -14,13 +19,39 @@ creds = matrix.Creds( ) bot = matrix.bot.Bot(creds, config) +bot.start_time = datetime.now() + + +@bot.listener.on_message_event +async def ping(room, message): + mtch = matrix.match.MessageMatch(room, message, bot, PREFIX) + if mtch.is_not_from_this_bot and mtch.prefix and mtch.command("ping"): + await misc.ping(bot, mtch, room, message) @bot.listener.on_message_event async def echo(room, message): mtch = matrix.match.MessageMatch(room, message, bot, PREFIX) if mtch.is_not_from_this_bot and mtch.prefix and mtch.command("echo"): - await bot.api.send_text_message(room.room_id, " ".join(mtch.args())) + await misc.echo(bot, mtch, room, message) + + +@bot.listener.on_message_event +async def uptime(room, message): + mtch = matrix.match.MessageMatch(room, message, bot, PREFIX) + if mtch.is_not_from_this_bot and mtch.prefix and mtch.command("uptime"): + await misc.uptime(bot, mtch, room, message) + + +@bot.listener.on_message_event +async def roll(room, message): + mtch = matrix.match.MessageMatch(room, message, bot, PREFIX) + if ( + mtch.is_not_from_this_bot + and mtch.prefix + and (mtch.command("roll") or mtch.command("r")) + ): + await dice.roll(bot, mtch, room, message) bot.run() diff --git a/misc.py b/misc.py new file mode 100644 index 0000000..f077fce --- /dev/null +++ b/misc.py @@ -0,0 +1,17 @@ +from datetime import datetime + + +async def ping(bot, mtch, room, message): + await bot.api.send_text_message(room.room_id, "Pong") + + +async def echo(bot, mtch, room, message): + await bot.api.send_text_message(room.room_id, " ".join(mtch.args())) + + +async def uptime(bot, mtch, room, message): + delta = datetime.now() - bot.start_time + await bot.api.send_text_message( + room.room_id, + f"Le bot est actif depuis {int(delta.total_seconds())} secondes (en gros {int(delta.total_seconds() / 86400)} jours).", + )