2024-09-19 01:37:21 +01:00
|
|
|
import re
|
|
|
|
import logging
|
|
|
|
import niobot
|
|
|
|
import typing
|
|
|
|
|
|
|
|
|
|
|
|
if typing.TYPE_CHECKING:
|
|
|
|
from ..main import NonsenseBot
|
|
|
|
|
|
|
|
|
2024-09-19 01:40:52 +01:00
|
|
|
class MemeteraCounter(niobot.Module):
|
2024-09-19 01:37:21 +01:00
|
|
|
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 = {
|
2024-09-19 01:55:32 +01:00
|
|
|
"memetera": re.compile(r"^memetera$", re.IGNORECASE),
|
2024-09-19 17:28:25 +01:00
|
|
|
"comma": re.compile(r","),
|
|
|
|
"nyo": re.compile(r"nyo", re.IGNORECASE),
|
2024-09-20 01:13:08 +01:00
|
|
|
"bite": re.compile(r"bite", re.IGNORECASE),
|
|
|
|
"neocat": re.compile(r"neocat", re.IGNORECASE)
|
2024-09-19 01:37:21 +01:00
|
|
|
}
|
|
|
|
bot: "NonsenseBot"
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
@niobot.event("message")
|
|
|
|
async def on_message(self, room: niobot.MatrixRoom, event: niobot.RoomMessage):
|
2024-09-19 02:02:31 +01:00
|
|
|
if not isinstance(event, niobot.RoomMessageFormatted):
|
|
|
|
return
|
2024-09-19 01:37:21 +01:00
|
|
|
if self.bot.is_old(event):
|
2024-09-19 01:48:26 +01:00
|
|
|
self.log.debug("Ignoring old event")
|
2024-09-19 01:37:21 +01:00
|
|
|
return
|
|
|
|
if room.room_id not in self.ROOM_IDS:
|
2024-09-19 01:48:26 +01:00
|
|
|
self.log.debug("Ignoring non-interesting event in %s", room.room_id)
|
2024-09-19 01:37:21 +01:00
|
|
|
return
|
|
|
|
if event.sender not in self.USER_IDS:
|
2024-09-19 01:48:26 +01:00
|
|
|
self.log.debug("Ignoring uninteresting event by %s in %s", event.sender, room.room_id)
|
2024-09-19 01:37:21 +01:00
|
|
|
return
|
|
|
|
if not self.bot.redis:
|
2024-09-19 01:48:26 +01:00
|
|
|
self.log.warning("Redis is not configured!")
|
2024-09-19 01:37:21 +01:00
|
|
|
return
|
|
|
|
|
2024-09-19 02:02:31 +01:00
|
|
|
self.log.debug("Interested in event %r in %r by %r", event.event_id, room.room_id, event.sender)
|
|
|
|
|
2024-09-19 01:37:21 +01:00
|
|
|
counts = {}
|
2024-09-19 02:02:31 +01:00
|
|
|
lower_body = event.body.casefold()
|
2024-09-19 01:37:21 +01:00
|
|
|
for word, regex in self.WORDS.items():
|
|
|
|
word_count = len(regex.findall(lower_body))
|
2024-09-19 02:02:31 +01:00
|
|
|
self.log.debug("%d results for %r", word_count, word)
|
2024-09-19 01:37:21 +01:00
|
|
|
if word_count:
|
2024-09-19 01:48:26 +01:00
|
|
|
self.log.debug("June said %r %d times in %s!", word, word_count, event.event_id)
|
2024-09-19 01:37:21 +01:00
|
|
|
counts.setdefault(word, 0)
|
|
|
|
counts[word] += word_count
|
|
|
|
|
|
|
|
for word, count in counts.items():
|
|
|
|
key = "june_wordcount_%s" % word
|
2024-09-19 01:48:26 +01:00
|
|
|
self.log.debug("Fetching redis key %r", key)
|
2024-09-19 01:37:21 +01:00
|
|
|
stored = await self.bot.redis.get(key)
|
2024-09-19 01:52:32 +01:00
|
|
|
stored = int(stored) if stored else 0
|
2024-09-19 01:37:21 +01:00
|
|
|
stored += count
|
|
|
|
await self.bot.redis.set(key, count)
|
2024-09-19 01:48:26 +01:00
|
|
|
self.log.debug("June has now said %r %d times", word, stored)
|
2024-09-19 01:37:21 +01:00
|
|
|
|
|
|
|
@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_*"):
|
2024-09-19 01:54:06 +01:00
|
|
|
key = key.decode()
|
2024-09-19 01:37:21 +01:00
|
|
|
value = await self.bot.redis.get(key)
|
2024-09-19 01:52:32 +01:00
|
|
|
value = int(value)
|
2024-09-19 01:37:21 +01:00
|
|
|
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```"
|
2024-09-19 01:53:19 +01:00
|
|
|
return await ctx.respond(text)
|