Reaction-action bot - example
Reactions it is a emojies or text under message. With mxbt you easily can work with reactions.
For now we make a simple Reaction-action bot, which send specific message on specific reaction.
bot.py structure
from mxbt import Bot, Context, Creds, Listener
import asyncio
import os
bot = Bot(
prefix="!", # Standart command prefix, commands can setup it own prefix
creds=Creds.from_json_file(
"credits.json" # You can also make Credits object via providing Dict with same structure
)
)
lr = Listener(bot)
# Mount all modules in modules dir
current_dir = os.path.dirname(__file__) + "/"
for filename in os.listdir(current_dir + "modules"):
# find *.py files in modules dir and mount it
if filename.endswith(".py"):
lr.mount_module(f"modules.{filename[:-3]}")
if __name__ == "__main__":
asyncio.run(lr.start_polling()) # Run bot event loop
modules/help.py structure
from mxbt.module import *
from mxbt import MatrixRoom, ReactionEvent
class Reactor(Module): # Instantiate from Module parent class
# bot.on_command replaced with command decorator from mxbt.module
@event(event_type=Event.onReaction) # For no-command events you can use event decorator
# Because it is a event we can't use context, so we should accept room, event and key (emoji) arguments
async def on_reaction(self, room: MatrixRoom, event: ReactionEvent, key: str) -> None:
if key == '❤️':
await self.bot.api.reply(
room.room_id,
f"Thank you for like!",
event.reacts_to,
)
elif key == '💩':
await self.bot.api.reply(
room.room_id,
f"How dare you!",
event.reacts_to,
)