fix(db): ⚡ Add a messages flushing queue.

This commit is contained in:
Nyeki.py
2024-10-18 19:44:12 -03:00
parent db1dbb4349
commit e7b553662d
8 changed files with 158 additions and 90 deletions
+11 -14
View File
@@ -9,16 +9,13 @@ from discord.ext.commands import errors
from tortoise import expressions, transactions, exceptions as tortoise_exc
import redis.asyncio as aredis
from payphone.db import setup, models
from payphone.core import config, constants, exceptions, formatting, help_command
from payphone.core import constants, exceptions, formatting, help_command
from payphone.core.redis import redis
# TODO: Make it an AutoShardedBot on production.
class Payphone(commands.AutoShardedBot):
redis = aredis.Redis(host="localhost" if config.IS_DEBUG else "redis")
def __init__(self) -> None:
intents = discord.Intents.default()
intents.message_content = True
@@ -43,9 +40,9 @@ class Payphone(commands.AutoShardedBot):
await setup.init()
await self.redis.ping()
await redis.ping()
await self.redis.delete(
await redis.delete(
constants.CHANNELS_ONCALL_SET, constants.CHANNELS_SEARCH_QUEUE
)
@@ -125,7 +122,7 @@ class Payphone(commands.AutoShardedBot):
user_id = user if isinstance(user, int) else user.id
key = constants.BAN_TEMPLATE % user_id
is_banned = await self.redis.get(key)
is_banned = await redis.get(key)
if is_banned is not None:
return bool(int(is_banned))
@@ -139,7 +136,7 @@ class Payphone(commands.AutoShardedBot):
),
).exists()
await self.redis.set(key, int(is_banned), ex=300)
await redis.set(key, int(is_banned), ex=300)
return is_banned
@@ -204,7 +201,7 @@ class Payphone(commands.AutoShardedBot):
banned_by_id=mod_id,
)
await self.redis.set(key, "1", ex=300)
await redis.set(key, "1", ex=300)
return row
@@ -231,7 +228,7 @@ class Payphone(commands.AutoShardedBot):
user_id=user_id,
).update(expires_at=datetime.now(timezone.utc))
await self.redis.set(key, "0", ex=300)
await redis.set(key, "0", ex=300)
async def get_prefix(
self, message: discord.Message | None = None, server_id: int | None = None
@@ -274,7 +271,7 @@ class Payphone(commands.AutoShardedBot):
key = constants.PREFIX_TEMPLATE % server_id
prefix: bytes | None = await self.redis.get(key)
prefix: bytes | None = await redis.get(key)
if prefix is not None:
prefix = prefix.decode()
@@ -282,7 +279,7 @@ class Payphone(commands.AutoShardedBot):
else:
server, _ = await models.Server.get_or_create(id=server_id)
await self.redis.set(key, server.prefix, ex=60 * 60)
await redis.set(key, server.prefix, ex=60 * 60)
prefix = server.prefix
@@ -314,7 +311,7 @@ class Payphone(commands.AutoShardedBot):
await models.Server.filter(id=server_id).update(prefix=prefix)
await self.redis.set(constants.PREFIX_TEMPLATE % server_id, prefix, ex=60 * 60)
await redis.set(constants.PREFIX_TEMPLATE % server_id, prefix, ex=60 * 60)
async def on_command_error(
self, context: commands.Context, exception: commands.CommandError