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.

64 lines
2.3KB

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