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.

66 lines
2.0KB

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