You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.6KB

  1. import random
  2. import checks
  3. import requests
  4. from discord.ext.commands import bot
  5. from config.server_config import ServerConfig
  6. class NFSW():
  7. def __init__(self, bot_client):
  8. self.bot = bot_client
  9. self.servers = ServerConfig().load_config()
  10. def tag_blacklist(self, ctx):
  11. blacklist = ""
  12. for tag in self.servers[str(ctx.guild.id)]["nsfw"]["blacklist"]:
  13. blacklist += "-{} ".format(tag)
  14. return blacklist
  15. def gelbooru_clone(self, ctx, base_url, tags):
  16. # Maybe a page randomiser
  17. limit = 200
  18. tags = tags + self.tag_blacklist(ctx)
  19. print(tags)
  20. url = base_url + '/index.php?page=dapi&s=post&q=index&json=1&tags=' + tags + '&limit=' + str(limit)
  21. req = requests.get(url, headers={'User-agent': 'RoxBot Discord Bot'})
  22. 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.
  23. post = None
  24. return post
  25. post = random.choice(req.json())
  26. return post
  27. @bot.command()
  28. @checks.is_nfsw_enabled()
  29. async def e621(self, ctx, *, tags = ""):
  30. """
  31. Returns an image from e621.com and can use tags you provide.
  32. """
  33. tags = tags + self.tag_blacklist(ctx)
  34. print(tags)
  35. base_url = "https://e621.net/"
  36. limit = 150
  37. url = base_url + 'post/index.json?tags=' + tags + '&limit=' + str(limit)
  38. req = requests.get(url, headers = {'User-agent': 'RoxBot Discord Bot'})
  39. 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.
  40. return await ctx.send("Nothing was found. *psst, check the tags you gave me.*")
  41. post = random.choice(req.json())
  42. return await ctx.send(post["file_url"])
  43. @bot.command()
  44. @checks.is_nfsw_enabled()
  45. async def rule34(self, ctx, *, tags = ""):
  46. """
  47. Returns an image from rule34.xxx and can use tags you provide.
  48. """
  49. base_url = "https://rule34.xxx"
  50. post = self.gelbooru_clone(ctx, base_url, tags)
  51. if not post:
  52. return await ctx.send("Nothing was found. *psst, check the tags you gave me.*")
  53. url = "https://img.rule34.xxx/images/" + post["directory"] + "/" + post["image"]
  54. return await ctx.send(url)
  55. @bot.command()
  56. @checks.is_nfsw_enabled()
  57. async def gelbooru(self, ctx, *, tags = ""):
  58. """
  59. Returns an image from gelbooru.com and can use tags you provide.
  60. """
  61. base_url = "https://gelbooru.com"
  62. post = self.gelbooru_clone(ctx, base_url, tags)
  63. if not post:
  64. return await ctx.send("Nothing was found. *psst, check the tags you gave me.*")
  65. url = "https://simg3.gelbooru.com/images/" + ''.join(post["directory"].split("\\")) + "/" + post["image"]
  66. return await ctx.send(url)
  67. def setup(bot_client):
  68. bot_client.add_cog(NFSW(bot_client))