feat: basic commands + prototype of roll parser

This commit is contained in:
RatCornu 2026-03-24 00:24:58 +01:00
parent 4b47899a3d
commit 74f991af40
Signed by: ratcornu
GPG key ID: B3BE02E379E6E8E2
4 changed files with 84 additions and 1 deletions

34
dice.py Normal file
View file

@ -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())))
)

View file

@ -73,6 +73,7 @@
(python.withPackages ( (python.withPackages (
py: with py; [ py: with py; [
mautrix mautrix
pyparsing
python-cryptography-fernet-wrapper python-cryptography-fernet-wrapper
python-dotenv python-dotenv
python-lsp-server python-lsp-server

33
main.py
View file

@ -1,6 +1,11 @@
from datetime import datetime
import simplematrixbotlib as matrix import simplematrixbotlib as matrix
from dotenv import dotenv_values from dotenv import dotenv_values
import dice
import misc
DOTENV = dotenv_values(".env") DOTENV = dotenv_values(".env")
PREFIX = "!" PREFIX = "!"
@ -14,13 +19,39 @@ creds = matrix.Creds(
) )
bot = matrix.bot.Bot(creds, config) 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 @bot.listener.on_message_event
async def echo(room, message): async def echo(room, message):
mtch = matrix.match.MessageMatch(room, message, bot, PREFIX) mtch = matrix.match.MessageMatch(room, message, bot, PREFIX)
if mtch.is_not_from_this_bot and mtch.prefix and mtch.command("echo"): 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() bot.run()

17
misc.py Normal file
View file

@ -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).",
)