diff --git a/.env.example b/.env.example index a86a1b4..6940d2f 100644 --- a/.env.example +++ b/.env.example @@ -19,4 +19,6 @@ DE_MEETUP_CHANNEL_ID= AT_MEETUP_CHANNEL_ID= CH_MEETUP_CHANNEL_ID= US_MEETUP_CHANNEL_ID= -UK_MEETUP_CHANNEL_ID= \ No newline at end of file +UK_MEETUP_CHANNEL_ID= + +COUNTING_CHANNEL_ID= \ No newline at end of file diff --git a/cogs/database.py b/cogs/database.py index 21254e7..27cad15 100644 --- a/cogs/database.py +++ b/cogs/database.py @@ -31,6 +31,11 @@ class Database: claimed_at TEXT DEFAULT NULL, is_claimed INTEGER DEFAULT 0 CHECK (is_claimed IN (0, 1)) ); + + CREATE TABLE IF NOT EXISTS count ( + count INTEGER PRIMARY KEY AUTOINCREMENT, + user_id TEXT NOT NULL + ); """) await self.conn.commit() diff --git a/extensions/counting.py b/extensions/counting.py new file mode 100644 index 0000000..1e06d87 --- /dev/null +++ b/extensions/counting.py @@ -0,0 +1,102 @@ +import os +import re +import discord +from discord.ext import commands +from dotenv import load_dotenv +from simpleeval import simple_eval, InvalidExpression + +load_dotenv() + +COUNTING_CHANNEL_ID = int(os.getenv("COUNTING_CHANNEL_ID")) + +_MATH_CHARS = re.compile(r"[^\d\+\-\*\/\(\)\^\%\.\s]") + +def solve_string(content: str) -> int | None: + expr = _MATH_CHARS.sub("", content) + expr = expr.strip() + + if not expr or not any(ch.isdigit() for ch in expr): + return None + + try: + value = simple_eval(expr) + except (InvalidExpression, ZeroDivisionError, ValueError, SyntaxError, TypeError, KeyError): + return None + + if isinstance(value, bool): + return None + + try: + return int(value) + except (TypeError, ValueError): + return None + +class Counting(commands.Cog): + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.Cog.listener() + async def on_message(self, message: discord.Message): + if not message or not message.author or message.webhook_id is not None: + return + + if message.author.bot: + return + + if message.channel.id != COUNTING_CHANNEL_ID: + return + + db = self.bot.database.conn + if db is None: + try: + await message.delete() + except discord.NotFound: + pass + await message.channel.send(f"DB is Down :c <@1073620716152434830>") + return + + try: + async with db.execute( + "SELECT count, user_id FROM count ORDER BY count DESC LIMIT 1" + ) as cursor: + result = await cursor.fetchone() + + if result is None: + expected = 1 + previous_user_id = None + else: + expected = result[0] + 1 + previous_user_id = result[1] + + parsed = solve_string(message.content) + is_correct = ( + parsed is not None + and parsed == expected + and previous_user_id != str(message.author.id) + ) + + if not is_correct: + try: + await message.delete() + except discord.NotFound: + pass + return + + await db.execute( + "INSERT INTO count (user_id) VALUES (?)", + (str(message.author.id),) + ) + await db.commit() + + await message.add_reaction("✅") + + except Exception as e: + print(f"Database error: {e}") + try: + await message.delete() + except discord.NotFound: + pass + + +async def setup(bot: commands.Bot): + await bot.add_cog(Counting(bot)) diff --git a/dev/ticket.py b/extensions/ticket.py similarity index 100% rename from dev/ticket.py rename to extensions/ticket.py diff --git a/requirements.txt b/requirements.txt index 571e5aa..6873a4c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ discord.py aiosqlite geopy -python-dotenv \ No newline at end of file +python-dotenv +simpleeval \ No newline at end of file