Add june word counter

This commit is contained in:
Nexus 2024-09-19 01:37:21 +01:00
parent 938334e762
commit 5f313c3f1d

View file

@ -0,0 +1,82 @@
import re
import logging
import niobot
import typing
if typing.TYPE_CHECKING:
from ..main import NonsenseBot
class MemeteraCounter(niobot.NioBot):
ROOM_IDS = {
"!gJtz8vGvObJTcs8eYT:puppygock.gay",
"!n8DKU1BeeJilOJXDPr:seirdy.one",
"!CvZqWrEc2aKtoCoZo4:puppygock.gay"
}
USER_IDS = {
"@june:awawawawawawawawawawawawawawawawawawawawawawawawawawawawawawaw.gay",
"@june:constellatory.net",
"@june:girlboss.ceo",
"@june:itzzen.net",
"@strawberry:transfem.dev",
"@strawberry:puppygock.gay",
"@girlbossceo:matrix.org",
"@strawberry:sulian.eu"
}
WORDS = {
"memetera": re.compile(r"^memetera$", re.IGNORECASE)
}
bot: "NonsenseBot"
log = logging.getLogger(__name__)
@niobot.event("message")
async def on_message(self, room: niobot.MatrixRoom, event: niobot.RoomMessage):
if self.bot.is_old(event):
return
if room.room_id not in self.ROOM_IDS:
return
if event.sender not in self.USER_IDS:
return
if not self.bot.redis:
return
counts = {}
lower_body = event.source.get("body", "").casefold()
for word, regex in self.WORDS.items():
word_count = len(regex.findall(lower_body))
if word_count:
counts.setdefault(word, 0)
counts[word] += word_count
for word, count in counts.items():
key = "june_wordcount_%s" % word
stored = await self.bot.redis.get(key)
stored = stored or 0
stored += count
await self.bot.redis.set(key, count)
@niobot.command(name="june-word-count", hidden=True)
async def june_word_count(
self,
ctx: niobot.Context,
raw: bool = False
):
keys = {}
lines = []
if raw:
lines.append("```md")
for key in await self.bot.redis.keys("june_wordcount_*"):
value = await self.bot.redis.get(key)
if raw:
lines.append(f"* {key}: {value}")
else:
keys[key[15:]] = value
for key, count in keys.items():
lines.append(f"* {key}: {count:,}")
text = "\n".join(lines)
if raw:
text += "\n```"
return await ctx.respond(raw)