Skip to content

Commit

Permalink
Fix block links command
Browse files Browse the repository at this point in the history
- Fix block links commands error
- Improve allowed links logic
- Allow console error when is not production
  • Loading branch information
rukasudev committed Jul 27, 2024
1 parent 97e0ca2 commit 91d5ab6
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ celerybeat.pid
*.sage.py

# Environments
.env
.env*
.venv
env/
venv/
Expand Down
6 changes: 2 additions & 4 deletions app/cogs/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Errors(Cog, name="errors"):
def __init__(self, bot: DiscordBot) -> None:
self.bot = bot
super().__init__()
bot.tree.on_error = self.on_app_command_error
if self.bot.config.is_prod():
bot.tree.on_error = self.on_app_command_error

async def on_app_command_error(
self,
Expand All @@ -26,9 +27,6 @@ async def on_app_command_error(
) -> None:
await self.send_default_error_message(interaction)

if str(interaction.user.id) == self.bot.owner_id:
return

command_name = logconstants.UNKNOWN_COMMAND
if interaction.command:
command_name = interaction.command.qualified_name
Expand Down
3 changes: 2 additions & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(self):
load_dotenv(dotenv_path, override=True)

self.ENVIRONMENT = os.getenv("APPLICATION_ENVIRONMENT")
self.DEBUG = os.getenv("DEBUG")
self.get_ssm_configs() if self.is_prod() else self.get_local_configs()

def get_local_configs(self):
Expand Down Expand Up @@ -92,7 +93,7 @@ def is_prod(self) -> bool:
return self.ENVIRONMENT.upper() == "PROD"

def is_debug(self) -> bool:
return self.ENVIRONMENT.upper() == "TEST"
return self.DEBUG.lower() == "true"

def run_local_webhook_api(self) -> bool:
return self.RUN_LOCAL_WEBHOOK_API.lower() == "true"
12 changes: 12 additions & 0 deletions app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,15 @@ class LogTypes:
class Emojis:
FRISBEE_EMOJI: Final[str] = ":flying_disc:"
EDIT_EMOJI: Final[str] = ":pencil:"

default_allowed_links: Final[Dict[str, str]] = {
"facebook": "https://facebook.com",
"instagram": "https://instagram.com",
"twitter": "https://twitter.com",
"twitch": "https://twitch.tv",
"youtube": "https://youtube.com",
"discord": "https://discord.gg",
"spotify": "https://open.spotify.com",
"tiktok": "https://tiktok.com",
"reddit": "https://reddit.com",
}
1 change: 1 addition & 0 deletions app/languages/form/block_links.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ steps:
- "Twitch"
- "Tiktok"
- "Reddit"
- "Discord"

- action: modal
key: answer
Expand Down
35 changes: 31 additions & 4 deletions app/services/block_links.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
from typing import List

import discord

from app.constants import Commands as constants
from app.constants import default_allowed_links
from app.services import cache
from app.services.moderations import (
send_command_form_message,
send_command_manager_message,
)

from .utils import check_message_has_link, check_two_lists_intersection, list_roles_id
from .utils import check_two_lists_intersection, get_message_links, list_roles_id


def remove_allowed_links(links: List[str], allowed_links: List[str]) -> List[str]:
allowed_link_list = []

for allowed_link in allowed_links:
allowed_link_list.append(default_allowed_links[allowed_link.lower()])

for link in links:
link = link.replace("www.", "")

if link.endswith("/"):
link = link[:-1]

if link in allowed_link_list:
links.remove(link)

return links


async def check_message(guild_id: str, message: discord.Message) -> None:
Expand All @@ -27,19 +48,25 @@ async def check_message(guild_id: str, message: discord.Message) -> None:

allowed_chats = cogs[constants.BLOCK_LINKS_ALLOWED_CHATS_KEY].get("values")
allowed_links = cogs[constants.BLOCK_LINKS_ALLOWED_LINKS_KEY]

if isinstance(allowed_links, str):
allowed_links = [allowed_links]

message_has_link = check_message_has_link(message, allowed_links)
message_links = get_message_links(message.content)
parsed_links = remove_allowed_links(message_links, allowed_links)
message_chat = str(message.channel.id)

if message_has_link and message_chat not in allowed_chats:
if parsed_links and message_chat not in allowed_chats:
await message.delete()
await message.channel.send(
cogs[constants.BLOCK_LINKS_ANSWER_KEY], delete_after=5
)


async def manager(interaction: discord.Interaction, guild_id: str) -> None:
cogs = cache.get_cog_data_or_populate(guild_id, constants.BLOCK_LINKS_KEY, manager=True)
cogs = cache.get_cog_data_or_populate(
guild_id, constants.BLOCK_LINKS_KEY, manager=True
)

if cogs == None:
return await send_command_form_message(interaction, constants.BLOCK_LINKS_KEY)
Expand Down
27 changes: 11 additions & 16 deletions app/services/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,8 @@
from app.constants import LogTypes as logconstants


def check_message_has_link(message: str, allowed_links: List[str]) -> List[str]:
links = [
link
for link in findall(
"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+",
message.content.lower(),
)
if not any(allowed.lower() in link.lower() for allowed in allowed_links)
]
def get_message_links(message: str) -> List[str]:
links = findall(r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", message.lower())

return links

Expand Down Expand Up @@ -141,17 +134,19 @@ async def wrapper(self, interaction: discord.Interaction, *args, **kwargs):
return decorator


def parse_settings_with_database_values(cog_data: List[Dict[str, str]], form_steps: Dict[str, str], locale: str) -> List[Dict[str, str]]:
def parse_settings_with_database_values(cog_data: Dict[str, str], form_steps: Dict[str, str], locale: str) -> List[Dict[str, str]]:
response = []
cogs_title = parse_form_steps_titles(form_steps, locale)

for cog_key, value in cog_data.items():
if cogs_title.get(cog_key):
if value["style"] == "composition":
value = parse_settings_with_database_values_composition(form_steps, locale, value["values"])
response.append({"title": cogs_title[cog_key], "value": value, "style": "composition"})
else:
response.append({"title": cogs_title[cog_key], "value": value})
if not cogs_title.get(cog_key):
continue

if isinstance(value, dict) and value.get("style") == "composition":
value = parse_settings_with_database_values_composition(form_steps, locale, value["values"])
response.append({"title": cogs_title[cog_key], "value": value, "style": "composition"})
else:
response.append({"title": cogs_title[cog_key], "value": value})

return response

Expand Down

0 comments on commit 91d5ab6

Please sign in to comment.