feat(shard): Lots of code.

This commit is contained in:
Nyeki.py
2024-11-15 23:24:11 -03:00
parent d9d752c730
commit 6377cb66c2
35 changed files with 2546 additions and 105 deletions
+159 -44
View File
@@ -9,10 +9,11 @@ import aiohttp
from payphone.db import models
from payphone.bot import Payphone
from payphone.core import constants, censorship, messages
from payphone.cogs import base
from payphone.core import constants, censorship, messages, masks, checks
class Calls(commands.Cog):
class Calls(base.Cog):
"""Calls in Payphone."""
def __init__(self, bot: Payphone) -> None:
@@ -24,7 +25,8 @@ class Calls(commands.Cog):
self.bot = bot
@commands.command()
@commands.command(aliases=["c"])
@checks.is_not_banned()
async def call(self, ctx: commands.Context) -> None:
"""
Call and connect with other people.
@@ -49,7 +51,8 @@ class Calls(commands.Cog):
)
)
@commands.command()
@commands.command(aliases=["s"])
@checks.is_not_banned()
async def skip(self, ctx: commands.Context) -> None:
"""
Skip the current call and search for another one.
@@ -76,7 +79,8 @@ class Calls(commands.Cog):
f"☎️ {random.choice(messages.OTHER_PARTY_HANGUP)}"
)
@commands.command()
@commands.command(aliases=["h"])
@checks.is_not_banned()
async def hangup(self, ctx: commands.Context) -> None:
"""
Hang up the call.
@@ -101,13 +105,14 @@ class Calls(commands.Cog):
),
)
@commands.command()
@commands.command(aliases=["fr", "friend"])
@checks.is_not_banned()
async def friendreq(self, ctx: commands.Context) -> None:
"""
Send a friend request to the other party.
"""
call = await self.bot.call_handler.get_call(ctx.channel.id)
call = await self.bot.call_handler.get_call_by_channel_id(ctx.channel.id)
if call is None:
return await ctx.send(
@@ -125,26 +130,63 @@ class Calls(commands.Cog):
ctx.send("📬 Your friend request has been sent!"),
)
@commands.command(aliases=["m"])
@checks.is_not_banned()
async def mask(self, ctx: commands.Context, toggle: bool | None = None) -> None:
"""
Toggle your mask.
Options:
- `[toggle]`: Whether to toggle your mask on or off. If no toggle is specified, your mask will be displayed.
"""
call = await self.bot.call_handler.get_call_by_channel_id(ctx.channel.id)
user = await models.User.get_cache_or_db(self.bot.redis, ctx.author.id)
if toggle is not None:
user.is_masked = toggle
await user.save(force_update=True)
await user.set_cache(self.bot.redis)
message = f"🎭 Your mask {"is" if toggle is None else "was set to"} {"`on`" if user.is_masked else "`off`"}."
if call is not None:
participant = await call.get_participant(ctx.author.id)
if user.is_masked:
message += f" You're currently being displayed as **{masks.get_mask_emoji(participant.mask)} {masks.get_mask_name(participant.mask)}**."
message += (
" You can toggle your mask on or off using `p.mask on` or `p.mask off`."
)
return await ctx.send(message)
@commands.Cog.listener()
async def on_message(self, message: discord.Message) -> None:
"""
Handle messages in calls.
"""
if message.author.bot or (await self.bot.get_context(message)).valid:
ctx = await self.bot.get_context(message)
if message.author.bot or ctx.valid:
return
call = await self.bot.call_handler.get_call(message.channel.id)
if await self.bot.is_banned(message.author.id):
return
call = await self.bot.call_handler.get_call_by_channel_id(message.channel.id)
if call is None:
return
is_new_participant = not bool(await call.get_participant(message.author.id))
await call.set_participant(
participant, was_mask_toggled = await call.set_participant(
message.author.id,
message.author.display_name,
message.author.display_avatar.url,
message.channel.id,
)
other_channel = (
@@ -153,9 +195,14 @@ class Calls(commands.Cog):
else call.channels[1]
)
user_flag = await self.bot.get_user_flag(message.author.id)
blocked_section = censorship.censor(message.content)
blocked_section = (
censorship.censor(
message.content,
participant.user.flag & models.UserFlag.SUPPORTER,
)
if not (participant.user.flag & models.UserFlag.MODERATOR)
else None
)
if blocked_section:
return await asyncio.gather(
@@ -178,18 +225,19 @@ class Calls(commands.Cog):
message_content = self.bot.clean_message(message, call)
if message.stickers:
if user_flag & (models.UserFlag.SUPPORTER | models.UserFlag.MODERATOR):
if participant.user.flag & (
models.UserFlag.SUPPORTER | models.UserFlag.MODERATOR
):
sticker = message.stickers[0]
if sticker.format != discord.StickerFormatType.lottie:
message_content = f"[Sticker: {sticker.name}]({sticker.url})"
else:
await message.channel.send(
"💖 Stickers are only allowed to donators! You can become one at [our Ko-Fi "
"page](https://ko-fi.com/payphonedev)."
return await message.channel.send(
"💖 Stickers and GIFs are only allowed to donators! You can become one at [our Ko-Fi "
"page](<https://ko-fi.com/payphonedev>)."
)
return
embeds = []
@@ -202,38 +250,91 @@ class Calls(commands.Cog):
message.reference.message_id
)
replied_message_content = self.bot.clean_message(replied_message, call)
embeds.append(
discord.Embed(
color=constants.PRIMARY_COLOR,
description=replied_message_content,
author=discord.EmbedAuthor(
name=replied_message.author.display_name,
icon_url=replied_message.author.display_avatar.url,
),
original_message_id = (
await self.bot.call_handler.get_original_message_id(
replied_message.id
)
)
if (
original_message_id is not None
and original_message_id != replied_message.id
):
replied_message = await self.bot.get_partial_messageable(
other_channel
).fetch_message(original_message_id)
replied_message_content = self.bot.clean_message(replied_message, call)
replied_participant = await call.get_participant(
replied_message.author.id
)
if replied_participant is None:
message_content += (
"\n-# ⓘ In reply to a message from outside this call."
)
else:
embeds.append(
discord.Embed(
color=constants.PRIMARY_COLOR,
description=replied_message_content,
author=discord.EmbedAuthor(
name=(
replied_participant.display_name
if replied_participant.channel_id
!= message.channel.id
else replied_participant.name
),
icon_url=(
replied_participant.display_avatar_url
if replied_participant.channel_id
!= message.channel.id
else replied_participant.avatar_url
),
),
)
)
else:
message_content += (
"\n-# ⓘ In reply to a message from outside this call."
)
if is_new_participant:
if user_flag & models.UserFlag.SUPPORTER:
embeds.append(
discord.Embed(
color=constants.SUPPORTER_COLOR,
description=f"🌟 **{message.author.display_name}** is a Payphone supporter!",
if was_mask_toggled:
if not participant.is_masked:
if participant.user.flag & models.UserFlag.SUPPORTER:
embeds.append(
discord.Embed(
color=constants.SUPPORTER_COLOR,
description=f"🌟 **{participant.display_name}** is a Payphone supporter!",
footer=random.choice(
[
discord.EmbedFooter(
text="You can also become a supporter at our Ko-Fi page."
)
]
+ [None] * 3
),
)
)
)
if user_flag & models.UserFlag.MODERATOR:
if participant.user.flag & models.UserFlag.MODERATOR:
embeds.append(
discord.Embed(
color=constants.MODERATOR_COLOR,
description=f"🛡️ **{participant.display_name}** is a Payphone moderator!",
)
)
else:
embeds.append(
discord.Embed(
color=constants.MODERATOR_COLOR,
description=f"🛡️ **{message.author.display_name}** is a Payphone moderator!",
color=constants.MASK_COLOR,
description=f"🎭 **{participant.display_name}** is wearing a mask! Their real identity is hidden.",
footer=discord.EmbedFooter(
text="You can set your mask using p.mask on."
),
)
)
@@ -246,8 +347,8 @@ class Calls(commands.Cog):
propagated_msg = await webhook.send(
content=message_content,
username=message.author.display_name,
avatar_url=message.author.display_avatar.url,
username=participant.display_name,
avatar_url=participant.display_avatar_url,
embeds=embeds,
wait=True,
)
@@ -267,7 +368,7 @@ class Calls(commands.Cog):
Handle deleted messages in calls.
"""
call = await self.bot.call_handler.get_call(payload.channel_id)
call = await self.bot.call_handler.get_call_by_channel_id(payload.channel_id)
if call is None:
return
@@ -296,7 +397,7 @@ class Calls(commands.Cog):
Handle edited messages in calls.
"""
call = await self.bot.call_handler.get_call(payload.channel_id)
call = await self.bot.call_handler.get_call_by_channel_id(payload.channel_id)
if call is None:
return
@@ -342,6 +443,11 @@ class Calls(commands.Cog):
Report a message in a call.
"""
if self.bot.is_banned(ctx.author.id):
return await ctx.send_response(
"❌ You're banned from using Payphone.", ephemeral=True
)
author_id = await self.bot.call_handler.get_message_author_id(message.id)
if author_id is None:
@@ -363,11 +469,20 @@ class Calls(commands.Cog):
else:
message_id = message.id
if message_id != message.id:
cached_message = await self.bot.call_handler.get_message(message_id)
message = await self.bot.get_partial_messageable(
cached_message.channel_id
).fetch_message(message_id)
await models.Report.update_or_create(
message_id=message_id,
defaults={
"channel_id": message.channel.id,
"user_id": author_id,
"user_name": message.author.display_name,
"user_avatar_url": message.author.display_avatar.url,
"message_content": message.content,
},
)