Browse Source

fixed nsfw checking for dm's on NSFW cog. Subreddit cog should work just fine as it deals with it in a different way.

fixed nsfw checking in nsfw and refactored a function in reddit cog.
tags/v2.0.0
Roxie Gibson 5 years ago
parent
commit
276647c3d2
3 changed files with 19 additions and 11 deletions
  1. +10
    -0
      roxbot/checks.py
  2. +6
    -5
      roxbot/cogs/nsfw.py
  3. +3
    -6
      roxbot/cogs/reddit.py

+ 10
- 0
roxbot/checks.py View File

@@ -24,6 +24,8 @@


import roxbot

import discord
from discord.ext import commands


@@ -34,3 +36,11 @@ def has_permission_or_owner(**perms):
return commands.has_permissions(**perms)
return commands.check(predicate)


def is_nsfw():
"""A :func:`.check` that checks if the channel is a NSFW channel or a DM channel."""
def pred(ctx):
is_dm_channel = bool(isinstance(ctx.channel, discord.DMChannel))
is_nsfw_guild_channel = bool(isinstance(ctx.channel, discord.TextChannel) and ctx.channel.is_nsfw())
return bool(is_nsfw_guild_channel or is_dm_channel)
return commands.check(pred)

+ 6
- 5
roxbot/cogs/nsfw.py View File

@@ -51,11 +51,12 @@ class NFSW():
}
}

@commands.is_nsfw()
@roxbot.checks.is_nsfw()
@commands.command(hidden=True)
async def gelbooru_clone(self, ctx, base_url, post_url, tags):
limit = 150
tags = tags + tag_blacklist(ctx.guild)
if isinstance(ctx.channel, discord.TextChannel):
tags = tags + tag_blacklist(ctx.guild)
page = random.randrange(20)
url = base_url + tags + '&limit=' + str(limit) + '%pid=' + str(page)
if isinstance(ctx.channel, discord.DMChannel):
@@ -89,7 +90,7 @@ class NFSW():
output = await ctx.send(url)
await roxbot.utils.delete_option(self.bot, ctx, output, self.bot.get_emoji(444410658101002261) or "❌")

@commands.is_nsfw()
@roxbot.checks.is_nsfw()
@commands.command()
async def e621(self, ctx, *, tags=""):
"""
@@ -98,7 +99,7 @@ class NFSW():
base_url = "https://e621.net/post/index.json?tags="
return await ctx.invoke(self.gelbooru_clone, base_url=base_url, post_url="", tags=tags)

@commands.is_nsfw()
@roxbot.checks.is_nsfw()
@commands.command()
async def rule34(self, ctx, *, tags=""):
"""
@@ -108,7 +109,7 @@ class NFSW():
post_url = "https://img.rule34.xxx/images/"
return await ctx.invoke(self.gelbooru_clone, base_url=base_url, post_url=post_url, tags=tags)

@commands.is_nsfw()
@roxbot.checks.is_nsfw()
@commands.command()
async def gelbooru(self, ctx, *, tags=""):
"""

+ 3
- 6
roxbot/cogs/reddit.py View File

@@ -192,8 +192,10 @@ class Reddit:
subreddit = subreddit.lower()
if isinstance(ctx.channel, discord.DMChannel):
cache_id = ctx.author.id
else:
nsfw_allowed = True
else: # Is text channel in guild
cache_id = ctx.guild.id
nsfw_allowed = ctx.channel.is_nsfw()

self.scrapper.cache_refresh(cache_id)
posts = await self.scrapper.sub_request(subreddit)
@@ -201,11 +203,6 @@ class Reddit:
if not posts:
raise roxbot.UserError(self.SUB_NOT_FOUND)

if isinstance(ctx.channel, discord.TextChannel):
nsfw_allowed = ctx.channel.is_nsfw()
else:
nsfw_allowed = True

choice = await self.scrapper.random(posts["children"], cache_id, nsfw_allowed)

if not choice:

Loading…
Cancel
Save