Browse Source

nsfw tag blacklisting added and tested.

tags/v1.4.0
roxie 6 years ago
parent
commit
e2eddec7a7
3 changed files with 37 additions and 12 deletions
  1. +22
    -10
      cogs/nsfw.py
  2. +2
    -1
      config/server_config.py
  3. +13
    -1
      config/settings.py

+ 22
- 10
cogs/nsfw.py View File

@@ -2,14 +2,24 @@ import random
import checks
import requests
from discord.ext.commands import bot
from config.server_config import ServerConfig

class NFSW():
def __init__(self, bot_client):
self.bot = bot_client
self.servers = ServerConfig().load_config()

def gelbooru_clone(self, base_url, tags):
def tag_blacklist(self, ctx):
blacklist = ""
for tag in self.servers[str(ctx.guild.id)]["nsfw"]["blacklist"]:
blacklist += "-{} ".format(tag)
return blacklist

def gelbooru_clone(self, ctx, base_url, tags):
# Maybe a page randomiser
limit = 100
limit = 200
tags = tags + self.tag_blacklist(ctx)
print(tags)
url = base_url + '/index.php?page=dapi&s=post&q=index&json=1&tags=' + tags + '&limit=' + str(limit)
req = requests.get(url, headers={'User-agent': 'RoxBot Discord Bot'})
if str(req.content) == "b''": # This is to catch any errors if the tags don't return anything because I can't do my own error handling in commands.
@@ -18,12 +28,14 @@ class NFSW():
post = random.choice(req.json())
return post

@bot.command(pass_context=True)
@bot.command()
@checks.is_nfsw_enabled()
async def e621(self, ctx, *, tags):
async def e621(self, ctx, *, tags = ""):
"""
Returns an image from e621.com and can use tags you provide.
"""
tags = tags + self.tag_blacklist(ctx)
print(tags)
base_url = "https://e621.net/"
limit = 150
url = base_url + 'post/index.json?tags=' + tags + '&limit=' + str(limit)
@@ -33,27 +45,27 @@ class NFSW():
post = random.choice(req.json())
return await ctx.send(post["file_url"])

@bot.command(pass_context=True)
@bot.command()
@checks.is_nfsw_enabled()
async def rule34(self, ctx, *, tags):
async def rule34(self, ctx, *, tags = ""):
"""
Returns an image from rule34.xxx and can use tags you provide.
"""
base_url = "https://rule34.xxx"
post = self.gelbooru_clone(base_url, tags)
post = self.gelbooru_clone(ctx, base_url, tags)
if not post:
return await ctx.send("Nothing was found. *psst, check the tags you gave me.*")
url = "https://img.rule34.xxx/images/" + post["directory"] + "/" + post["image"]
return await ctx.send(url)

@bot.command(pass_context=True)
@bot.command()
@checks.is_nfsw_enabled()
async def gelbooru(self, ctx, *, tags):
async def gelbooru(self, ctx, *, tags = ""):
"""
Returns an image from gelbooru.com and can use tags you provide.
"""
base_url = "https://gelbooru.com"
post = self.gelbooru_clone(base_url, tags)
post = self.gelbooru_clone(ctx, base_url, tags)
if not post:
return await ctx.send("Nothing was found. *psst, check the tags you gave me.*")
url = "https://simg3.gelbooru.com/images/" + ''.join(post["directory"].split("\\")) + "/" + post["image"]

+ 2
- 1
config/server_config.py View File

@@ -30,7 +30,8 @@ class ServerConfig():
},
"nsfw": {
"enabled": 0,
"channels": []
"channels": [],
"blacklist": []
},
"perm_roles": {
"admin": [],

+ 13
- 1
config/settings.py View File

@@ -168,7 +168,7 @@ class Settings:
em = discord.Embed(colour=0xDEADBF)
em.set_author(name="{} settings for {}.".format(self.bot.user.name, ctx.message.guild.name), icon_url=self.bot.user.avatar_url)
for settings in config:
if settings != "custom_commands" and settings != "admin":
if settings != "custom_commands" and settings != "warnings":
settingcontent = ""
for x in config[settings].items():
settingcontent += str(x).strip("()") + "\n"
@@ -377,6 +377,18 @@ class Settings:
await ctx.send("'{}' has been removed from the nsfw channel list.".format(channel.name))
except ValueError:
return await ctx.send("That role was not in the list.")
elif selection == "addbadtag":
if changes not in self.serverconfig[ctx.guild.id]["nsfw"]["blacklist"]:
self.serverconfig[ctx.guild.id]["nsfw"]["blacklist"].append(changes)
await ctx.send("'{}' has been added to the blacklisted tag list.".format(changes))
else:
return await ctx.send("'{}' is already in the list.".format(changes))
elif selection == "removebadtag":
try:
self.serverconfig[ctx.guild.id]["nsfw"]["blacklist"].remove(changes)
await ctx.send("'{}' has been removed from the blacklisted tag list.".format(changes))
except ValueError:
return await ctx.send("That tag was not in the blacklisted tag list.")
else:
return await ctx.send("No valid option given.")
return self.con.update_config(self.serverconfig)

Loading…
Cancel
Save