More easter eggs

This commit is contained in:
stijndcl 2022-09-20 01:40:05 +02:00
parent 1fe04b3687
commit 7517f844d8
3 changed files with 90 additions and 13 deletions

View file

@ -1,9 +1,11 @@
import random
import re
from typing import Optional
import discord
from discord.ext import commands
import settings
from database.utils.caches import EasterEggCache
from didier.utils.discord.prefix import match_prefix
@ -11,11 +13,8 @@ __all__ = ["detect_easter_egg"]
def _roll_easter_egg(response: str) -> Optional[str]:
"""Roll a random chance for an easter egg to be responded with
The chance for an easter egg to be used is 33%
"""
rolled = random.randint(0, 100) < 33
"""Roll a random chance for an easter egg to be responded with"""
rolled = random.randint(0, 100) < settings.EASTER_EGG_CHANCE
return response if rolled else None
@ -23,7 +22,8 @@ async def detect_easter_egg(client: commands.Bot, message: discord.Message, cach
"""Try to detect an easter egg in a message"""
prefix = match_prefix(client, message)
content = message.content.strip().lower()
# Remove markdown and whitespace for better matches
content = message.content.strip().strip("_* \t\n").lower()
# Message calls Didier
if prefix is not None:
@ -37,12 +37,17 @@ async def detect_easter_egg(client: commands.Bot, message: discord.Message, cach
return None
for easter_egg in cache.easter_eggs:
# Exact matches
if easter_egg.exact and easter_egg.match == content:
return _roll_easter_egg(easter_egg.response)
pattern = easter_egg.match
# Matches that start with a certain term
if easter_egg.startswith and content.startswith(easter_egg.match):
# Use regular expressions to easily allow slight variations
if easter_egg.exact:
pattern = rf"^{pattern}$"
elif easter_egg.startswith:
pattern = rf"^{pattern}"
matched = re.search(pattern, content)
if matched is not None:
return _roll_easter_egg(easter_egg.response)
return None