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.

161 lines
6.4KB

  1. # -*- coding: utf-8 -*-
  2. # MIT License
  3. #
  4. # Copyright (c) 2017-2018 Roxanne Gibson
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in all
  14. # copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. import discord
  24. from discord.ext import commands
  25. import roxbot
  26. from roxbot.db import *
  27. class NSFWSingle(db.Entity):
  28. enabled = Required(bool, default=False)
  29. blacklisted_tags = Optional(StrArray)
  30. guild_id = Required(int, unique=True, size=64)
  31. class NSFW(commands.Cog):
  32. """The NSFW cog is a collection of commands that post images from popular NSFW sites. """
  33. def __init__(self, bot_client):
  34. self.bot = bot_client
  35. self.cache = {}
  36. self.autogen_db = NSFWSingle
  37. @db_session
  38. def tag_blacklist(self, guild):
  39. blacklist = ""
  40. blacklist_db = NSFWSingle.get(guild_id=guild.id).blacklisted_tags
  41. for tag in blacklist_db:
  42. blacklist += " -{}".format(tag)
  43. return blacklist
  44. async def gelbooru_clone(self, ctx, base_url, endpoint_url, tags):
  45. if isinstance(ctx.channel, discord.TextChannel):
  46. banned_tags = self.tag_blacklist(ctx.guild)
  47. else:
  48. banned_tags = ""
  49. post = await roxbot.utils.danbooru_clone_api_req(
  50. ctx.channel,
  51. base_url,
  52. endpoint_url,
  53. tags=tags,
  54. banned_tags=banned_tags,
  55. cache=self.cache
  56. )
  57. if not post:
  58. return await ctx.send("Nothing was found. *psst, check the tags you gave me.*")
  59. else:
  60. output = await ctx.send(post)
  61. await self.bot.delete_option(output, self.bot.get_emoji(444410658101002261))
  62. @roxbot.checks.is_nsfw()
  63. @commands.command()
  64. async def e621(self, ctx, *, tags=""):
  65. """Posts a random image from https://e621.net using the tags you provide. Tags can be anything you would use to search the site normally like author and ratings.
  66. https://e621.net limits searches to 6 tags via the API. Blacklisting a lot of tags may break this command.
  67. Examples:
  68. # Post a random image
  69. ;e621
  70. # Post a random image with the tag "test"
  71. ;e621 test
  72. """
  73. base_url = "https://e621.net/post/index.json?tags="
  74. return await self.gelbooru_clone(ctx, base_url, "", tags)
  75. @roxbot.checks.is_nsfw()
  76. @commands.command()
  77. async def rule34(self, ctx, *, tags=""):
  78. """Posts a random image from https://rule34.xxx/ using the tags you provide. Tags can be anything you would use to search the site normally like author and ratings.
  79. Examples:
  80. # Post a random image
  81. ;rule34
  82. # Post a random image with the tag "test"
  83. ;rule34 test
  84. """
  85. base_url = "https://rule34.xxx/index.php?page=dapi&s=post&q=index&json=1&tags="
  86. endpoint_url = "https://img.rule34.xxx/images/"
  87. return await self.gelbooru_clone(ctx, base_url, endpoint_url, tags)
  88. @roxbot.checks.is_nsfw()
  89. @commands.command()
  90. async def gelbooru(self, ctx, *, tags=""):
  91. """Posts a random image from https://gelbooru.com using the tags you provide. Tags can be anything you would use to search the site normally like author and ratings.
  92. Examples:
  93. # Post a random image
  94. ;gelbooru
  95. # Post a random image with the tag "test"
  96. ;gelbooru test
  97. """
  98. base_url = "https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&tags="
  99. endpoint_url = "https://simg3.gelbooru.com/images/"
  100. return await self.gelbooru_clone(ctx, base_url, endpoint_url, tags)
  101. @commands.guild_only()
  102. @commands.has_permissions(manage_channels=True)
  103. @commands.command()
  104. async def nsfw(self, ctx, setting, *, changes=None):
  105. """Edits settings for the nsfw cog and other nsfw commands.
  106. Options:
  107. enable/disable: Enable/disables nsfw commands.
  108. addbadtag/removebadtag: Add/Removes blacklisted tags so that you can avoid em with the commands.
  109. Examples:
  110. # Enabled NSFW commands
  111. ;nsfw enable
  112. # Add "test" as a blacklisted tag
  113. ;nsfw addbadtag test
  114. # Remove "Roxbot" as a blacklisted tag
  115. ;nsfw removebadtag Roxbot
  116. """
  117. with db_session:
  118. nsfw_settings = NSFWSingle.get(guild_id=ctx.guild.id)
  119. if setting == "enable":
  120. nsfw_settings.enabled = True
  121. await ctx.send("'nsfw' was enabled!")
  122. elif setting == "disable":
  123. nsfw_settings.enabled = False
  124. await ctx.send("'nsfw' was disabled :cry:")
  125. elif setting == "addbadtag":
  126. if changes not in nsfw_settings.blacklisted_tags:
  127. nsfw_settings.blacklisted_tags.append(changes)
  128. await ctx.send("'{}' has been added to the blacklisted tag list.".format(changes))
  129. else:
  130. return await ctx.send("'{}' is already in the list.".format(changes))
  131. elif setting == "removebadtag":
  132. try:
  133. nsfw_settings.blacklisted_tags.remove(changes)
  134. await ctx.send("'{}' has been removed from the blacklisted tag list.".format(changes))
  135. except ValueError:
  136. return await ctx.send("That tag was not in the blacklisted tag list.")
  137. else:
  138. return await ctx.send("No valid option given.")
  139. def setup(bot_client):
  140. bot_client.add_cog(NSFW(bot_client))