Added user key-value pairs in bot database. v0.7.1
- Bumped patch number - Added user key-value pairs to the built-in database - Updated reaction_bot example to use key-value pairs storage - Database schema version is 3 now (added a table for user kv pairs)
This commit is contained in:
@@ -13,11 +13,21 @@ from mab import *
|
||||
|
||||
from _environment import check_environment
|
||||
|
||||
async def bump_count(bot: MatrixBot) -> int:
|
||||
"""Bump count of reactions processed and return new value. Uses bot's
|
||||
database for storage."""
|
||||
val = await bot.load_value("reactions_processed")
|
||||
val = 0 if val is None else int.from_bytes(val)
|
||||
val += 1
|
||||
await bot.save_value("reactions_processed", val.to_bytes(8))
|
||||
return val
|
||||
|
||||
async def on_specific_reaction(ctx: EventContext, vars: ContextVars) -> None:
|
||||
"""This callback is called when a specific reaction is added."""
|
||||
sender = vars[CTX_REACTION].sender
|
||||
reaction = vars[CTX_REACTION].reaction
|
||||
response = f"(Specific reaction) {sender}: {reaction}"
|
||||
count = await bump_count(ctx.bot)
|
||||
response = f"(Specific reaction) {sender}: {reaction}. Total reactions processed: {count}"
|
||||
await ctx.bot.send.text(
|
||||
ctx.room,
|
||||
response
|
||||
@@ -27,7 +37,8 @@ async def on_reaction(ctx: EventContext, vars: ContextVars) -> None:
|
||||
"""This callback is called when a reaction is added."""
|
||||
sender = vars[CTX_REACTION].sender
|
||||
reaction = vars[CTX_REACTION].reaction
|
||||
response = f"(Generic reaction) {sender}: {reaction}"
|
||||
count = await bump_count(ctx.bot)
|
||||
response = f"(Generic reaction) {sender}: {reaction}. Total reactions processed: {count}"
|
||||
await ctx.bot.send.text(
|
||||
ctx.room,
|
||||
response
|
||||
@@ -35,6 +46,7 @@ async def on_reaction(ctx: EventContext, vars: ContextVars) -> None:
|
||||
|
||||
async def on_message(ctx: EventContext, vars: ContextVars) -> None:
|
||||
"""This callback is called when a message is received."""
|
||||
await bump_count(ctx.bot)
|
||||
await ctx.bot.send.reaction(ctx.room, vars[CTX_ROOM_EVENT].event_id, "👀")
|
||||
|
||||
async def main() -> None:
|
||||
|
||||
Reference in New Issue
Block a user