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.

74 lines
2.6KB

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