feat(bot): 🎉 Create a redis client from scratch.

This commit is contained in:
Nyeki.py
2024-11-04 22:11:22 -03:00
parent eaaea73ab1
commit cd84781640
9 changed files with 937 additions and 199 deletions
+3 -4
View File
@@ -11,7 +11,7 @@ from discord.ext.commands import errors
from tortoise import expressions, transactions, exceptions as tortoise_exc
import upstash_redis.asyncio as aredis
from redis import Redis
import asyncpg.exceptions
@@ -29,9 +29,8 @@ from payphone.core import (
class Payphone(commands.AutoShardedBot):
log = logging.logger
redis: aredis.Redis = aredis.Redis(
url="http://localhost:8079" if config.IS_DEBUG else "http://serverless-redis:8079",
token="payphone"
redis: Redis = Redis(
url="redis://localhost" if config.IS_DEBUG else "redis://redis",
)
def __init__(self) -> None:
+60 -54
View File
@@ -103,8 +103,8 @@ class Calls(base.Cog):
if last_calls_rss_channels_map[call_rs.call_id] != call_rs.channel_id:
last_calls_other_channels_ids.add(call_rs.channel_id)
other_channel_id: int = await self.bot.redis.spop(
constants.CHANNELS_SEARCH_QUEUE
other_channel_id: int = next(
iter(await self.bot.redis.spop(constants.CHANNELS_SEARCH_QUEUE)), None
)
i = 0
@@ -127,8 +127,9 @@ class Calls(base.Cog):
constants.CHANNELS_SEARCH_QUEUE, str(other_channel_id)
)
other_channel_id = await self.bot.redis.spop(
constants.CHANNELS_SEARCH_QUEUE
other_channel_id = next(
iter(await self.bot.redis.spop(constants.CHANNELS_SEARCH_QUEUE)),
None,
)
if other_channel_id is None:
@@ -293,8 +294,8 @@ class Calls(base.Cog):
if last_calls_rss_channels_map[call_rs.call_id] != call_rs.channel_id:
last_calls_other_channels_ids.add(call_rs.channel_id)
other_channel_id: int = await self.bot.redis.spop(
constants.CHANNELS_SEARCH_QUEUE
other_channel_id: int = next(
iter(await self.bot.redis.spop(constants.CHANNELS_SEARCH_QUEUE)), None
)
i = 0
@@ -317,8 +318,9 @@ class Calls(base.Cog):
constants.CHANNELS_SEARCH_QUEUE, str(other_channel_id)
)
other_channel_id = await self.bot.redis.spop(
constants.CHANNELS_SEARCH_QUEUE
other_channel_id = next(
iter(await self.bot.redis.spop(constants.CHANNELS_SEARCH_QUEUE)),
None,
)
if other_channel_id is None:
@@ -762,45 +764,50 @@ class Calls(base.Cog):
pipe.hset(
constants.CALL_MESSAGES_TEMPLATE % call.id,
message.id,
orjson.dumps(
{
"id": message.id,
"call_id": call.id,
"channel_id": channel.id,
"user_id": user.id,
"profile_id": profile.id,
"mask": call_rs.mask if call_rs.is_masked else None,
"propagation_id": webhook_message.id if webhook_message else None,
"propagation_channel_id": (
other_channel.id if webhook_message else None
),
"replying_to_id": replying_to_id,
"created_at": datetime.now(timezone.utc),
}
).decode(),
{
message.id: orjson.dumps(
{
"id": message.id,
"call_id": call.id,
"channel_id": channel.id,
"user_id": user.id,
"profile_id": profile.id,
"mask": call_rs.mask if call_rs.is_masked else None,
"propagation_id": (
webhook_message.id if webhook_message else None
),
"propagation_channel_id": (
other_channel.id if webhook_message else None
),
"replying_to_id": replying_to_id,
"created_at": datetime.now(timezone.utc),
}
).decode()
},
)
pipe.hset(
constants.CALL_MESSAGE_CONTENTS_TEMPLATE % call.id,
constants.CALL_MESSAGE_CONTENTS_HASH_TEMPLATE % (message.id, 1),
orjson.dumps(
{
"message_id": message.id,
"content": cleaned_content,
"was_propagated": not bool(blocked_word),
"created_at": datetime.now(timezone.utc),
}
).decode(),
{
constants.CALL_MESSAGE_CONTENTS_HASH_TEMPLATE
% (message.id, 1): orjson.dumps(
{
"message_id": message.id,
"content": cleaned_content,
"was_propagated": not bool(blocked_word),
"created_at": datetime.now(timezone.utc),
}
).decode()
},
)
pipe.hset(
constants.CALL_MESSAGE_CONTENT_ITERATIONS_TEMPLATE % call.id, message.id, 1
constants.CALL_MESSAGE_CONTENT_ITERATIONS_TEMPLATE % call.id,
{message.id: 1},
)
if webhook_message is not None:
pipe.hset(
constants.CALL_MESSAGE_FROM_PROPAGATION_TEMPLATE % call.id,
webhook_message.id,
message.id,
{webhook_message.id: message.id},
)
if replying_to_id is not None:
@@ -811,18 +818,16 @@ class Calls(base.Cog):
if other_replies is None:
pipe.hset(
constants.CALL_MESSAGE_REPLIES_TEMPLATE % call.id,
replying_to_id,
message.id,
{replying_to_id: message.id},
)
else:
pipe.hset(
constants.CALL_MESSAGE_REPLIES_TEMPLATE % call.id,
replying_to_id,
other_replies + f",{message.id}",
{replying_to_id: other_replies + f",{message.id}"},
)
await pipe.exec()
await pipe.execute()
@commands.Cog.listener()
@commands.guild_only()
@@ -862,8 +867,7 @@ class Calls(base.Cog):
await self.bot.redis.hset(
constants.CALL_MESSAGES_TEMPLATE % channel.call_id,
message.id,
orjson.dumps(message_dict).decode(),
{message.id: orjson.dumps(message_dict).decode()},
)
other_channel = await models.Channel.get_or_none(
@@ -975,18 +979,20 @@ class Calls(base.Cog):
)
pipe.hset(
constants.CALL_MESSAGE_CONTENTS_TEMPLATE % channel.call_id,
constants.CALL_MESSAGE_CONTENTS_HASH_TEMPLATE % (message.id, iteration + 1),
orjson.dumps(
{
"message_id": message.id,
"content": cleaned_content,
"was_propagated": not bool(blocked_word),
"created_at": datetime.now(timezone.utc),
}
).decode(),
{
constants.CALL_MESSAGE_CONTENTS_HASH_TEMPLATE
% (message.id, iteration + 1): orjson.dumps(
{
"message_id": message.id,
"content": cleaned_content,
"was_propagated": not bool(blocked_word),
"created_at": datetime.now(timezone.utc),
}
).decode()
},
)
await pipe.exec()
await pipe.execute()
async with aiohttp.ClientSession() as session:
webhook = other_channel.webhook(session)
+3 -3
View File
@@ -2,19 +2,19 @@ import discord
import orjson
import upstash_redis.asyncio as aredis
from redis import Redis
from payphone.db import models
from payphone.core import constants, formatting
async def ReplyEmbed(
redis: aredis.Redis, reference: discord.MessageReference, call_id: int
redis: Redis, reference: discord.MessageReference, call_id: int
) -> tuple[discord.Embed | None, models.Message | None]:
"""Generates the embed sent on calls to represent replies.
Args:
redis (redis.asyncio.Redis): An instance of a Redis connection handler.
redis (redis.Redis): An instance of a Redis connection handler.
reference (discord.MessageReference): The message reference.
call_id (int): The ID of the call the message was sent in.