Skip to content

Commit

Permalink
Merge pull request PyDrocsid#123 from Scriptim/reddit-nsfw-filter
Browse files Browse the repository at this point in the history
Filter NSFW posts in Reddit integration
  • Loading branch information
Defelo authored Apr 3, 2022
2 parents 6a4fe78 + aca37eb commit 234310b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
24 changes: 24 additions & 0 deletions integrations/reddit/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ async def fetch_reddit_posts(subreddit: str, limit: int) -> Optional[List[dict]]

data = (await response.json())["data"]

filter_nsfw = await RedditSettings.filter_nsfw.get()
posts: List[dict] = []
for post in data["children"]:
# t3 = link
if post["kind"] == "t3" and post["data"].get("post_hint") == "image":
if post["data"]["over_18"] and filter_nsfw:
continue

posts.append(
{
"id": post["data"]["id"],
Expand Down Expand Up @@ -162,6 +166,9 @@ async def reddit(self, ctx: Context):
limit = await RedditSettings.limit.get()
embed.add_field(name=t.limit, value=str(limit))

filter_nsfw = await RedditSettings.filter_nsfw.get()
embed.add_field(name=t.nsfw_filter, value=tg.enabled if filter_nsfw else tg.disabled, inline=False)

out = []
async for reddit_channel in await db.stream(select(RedditChannel)): # type: RedditChannel
text_channel: Optional[TextChannel] = self.bot.get_channel(reddit_channel.channel)
Expand Down Expand Up @@ -242,6 +249,23 @@ async def reddit_limit(self, ctx: Context, limit: int):
await reply(ctx, embed=embed)
await send_to_changelog(ctx.guild, t.log_reddit_limit_set(limit))

@reddit.command(name="nsfw_filter", aliases=["nsfw"])
@RedditPermission.write.check
async def reddit_nsfw_filter(self, ctx: Context, enabled: bool):
"""
enable/disable nsfw filter for posts
"""

embed = Embed(title=t.reddit, colour=Colors.Reddit)
await RedditSettings.filter_nsfw.set(enabled)
if enabled:
embed.description = t.nsfw_filter_now_enabled
await send_to_changelog(ctx.guild, t.log_nsfw_filter_now_enabled)
else:
embed.description = t.nsfw_filter_now_disabled
await send_to_changelog(ctx.guild, t.log_nsfw_filter_now_disabled)
await reply(ctx, embed=embed)

@reddit.command(name="trigger", aliases=["t"])
@RedditPermission.trigger.check
async def reddit_trigger(self, ctx: Context):
Expand Down
1 change: 1 addition & 0 deletions integrations/reddit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
class RedditSettings(Settings):
interval = 4
limit = 4
filter_nsfw = True
5 changes: 5 additions & 0 deletions integrations/reddit/translations/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ reddit_limit_set: "Reddit limit has been updated. :white_check_mark:"
log_reddit_limit_set: "**Reddit limit** has been **set** to {}."
could_not_fetch: Could not fetch reddit posts of r/{}.
cannot_send: Reddit integration cannot send messages to {}.
nsfw_filter: "NSFW filter"
nsfw_filter_now_enabled: "Reddit NSFW filter has been enabled."
log_nsfw_filter_now_enabled: "**Reddit NSFW filter** has been **enabled**."
nsfw_filter_now_disabled: "Reddit NSFW filter has been disabled."
log_nsfw_filter_now_disabled: "**Reddit NSFW filter** has been **disabled**."

0 comments on commit 234310b

Please sign in to comment.