counting
This commit is contained in:
1 parent
bdf5088650
commit
3ff4f0bc9b
5 files changed
+110
No files matched your search
@@ -20,3 +20,5 @@ AT_MEETUP_CHANNEL_ID=
|
|||||||
CH_MEETUP_CHANNEL_ID=
|
CH_MEETUP_CHANNEL_ID=
|
||||||
US_MEETUP_CHANNEL_ID=
|
US_MEETUP_CHANNEL_ID=
|
||||||
UK_MEETUP_CHANNEL_ID=
|
UK_MEETUP_CHANNEL_ID=
|
||||||
|
|
||||||
|
COUNTING_CHANNEL_ID=
|
||||||
@@ -31,6 +31,11 @@ class Database:
|
|||||||
claimed_at TEXT DEFAULT NULL,
|
claimed_at TEXT DEFAULT NULL,
|
||||||
is_claimed INTEGER DEFAULT 0 CHECK (is_claimed IN (0, 1))
|
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()
|
await self.conn.commit()
|
||||||
|
|||||||
@@ -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))
|
||||||
File renamed without changes.
@@ -2,3 +2,4 @@ discord.py
|
|||||||
aiosqlite
|
aiosqlite
|
||||||
geopy
|
geopy
|
||||||
python-dotenv
|
python-dotenv
|
||||||
|
simpleeval
|
||||||
Reference in new issue
Block a user