Skip to content

Commit

Permalink
NSFW check
Browse files Browse the repository at this point in the history
  • Loading branch information
stekc committed Sep 4, 2024
1 parent 194be11 commit 1a26d60
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions cogs/socials.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ async def fetch_image(url):
output_image.seek(0)
return output_image

@cached(ttl=86400)
async def is_nsfw_reddit(self, link: str):
try:
async with aiohttp.ClientSession() as session:
link = await self.get_url_redirect(link)
async with session.get(link + ".json", timeout=5) as response:
if response.status == 200:
json_data = await response.json()
return json_data[0]["data"]["children"][0]["data"].get(
"over_18", False
)
return False
except (aiohttp.ClientError, asyncio.TimeoutError):
return False

async def build_reddit_embed(self, link: str):
if not self.config["reddit"]["build-embeds"]:
return None, None
Expand Down Expand Up @@ -521,6 +536,29 @@ async def fix_reddit(
f"||{link}" in message.content and message.content.count("||") >= 2
)

is_nsfw = await self.is_nsfw_reddit(link)

if message.guild:
if is_nsfw:
if not message.channel.is_nsfw():
embed = discord.Embed(
title="NSFW",
description="To use this feature you must be in a NSFW channel.",
color=discord.Color.red(),
)
if context:
await context.reply(
embed=embed, mention_author=False, delete_after=30
)
return
else:
await message.reply(
embed=embed,
mention_author=False,
delete_after=30,
)
return

if context:
embed, file = None, None
elif not spoiler:
Expand All @@ -546,6 +584,9 @@ async def fix_reddit(
return

if embed:
if is_nsfw:
footer = embed.footer.text
embed.set_footer(text=f"NSFW • {footer}")
if context:
await context.send(embed=embed, file=file, mention_author=False)
else:
Expand Down

0 comments on commit 1a26d60

Please sign in to comment.