diff --git a/lynxie/__main__.py b/lynxie/__main__.py index b00f89b..6f69cec 100644 --- a/lynxie/__main__.py +++ b/lynxie/__main__.py @@ -4,10 +4,10 @@ import discord from discord.ext import commands from discord.gateway import DiscordWebSocket -from lynxie.config import DISCORD_TOKEN, LYNXIE_PREFIX -from lynxie.commands import Help, Ping, Hello, Music, Animals, Img +from lynxie.config import DISCORD_TOKEN, LYNXIE_PREFIX, E621_BLACKLIST from lynxie.database import CommandHistory, Database from lynxie.utils import mobile_status +from lynxie.commands import Help, Ping, Hello, Music, Animals, Img, E621 db = Database() @@ -22,9 +22,32 @@ lynxie = commands.Bot( @lynxie.event async def on_ready(): print(f"Logged in as {lynxie.user} (ID: {lynxie.user.id})") - print("------ Stats ------") - print(f"Guilds: {len(lynxie.guilds)}") - print(f"Users: {db.session.query(CommandHistory.user).distinct().count()}") + + in_guilds = "In Guilds: " + str(len(lynxie.guilds)) + commands_used = "Commands called: " + str( + db.session.query(CommandHistory.user).count() + ) + unique_users = "Unique Users: " + str( + db.session.query(CommandHistory.user).distinct().count() + ) + blacklisted_words = "Blacklisted Words: " + str(len(E621_BLACKLIST)) + + bar_len = ( + max( + len(in_guilds), + len(commands_used), + len(unique_users), + len(blacklisted_words), + ) + + 4 + ) + + print("---- Stats " + "-" * (bar_len - 11)) + print(f"| {in_guilds}{' ' * (bar_len - len(in_guilds) - 3)}|") + print(f"| {commands_used}{' ' * (bar_len - len(commands_used) - 3)}|") + print(f"| {unique_users}{' ' * (bar_len - len(unique_users) - 3)}|") + print(f"| {blacklisted_words}{' ' * (bar_len - len(blacklisted_words) - 3)}|") + print("-" * bar_len) @lynxie.event @@ -62,6 +85,7 @@ async def run(): await lynxie.add_cog(Music(lynxie)) await lynxie.add_cog(Animals(lynxie)) await lynxie.add_cog(Img(lynxie)) + await lynxie.add_cog(E621(lynxie)) await lynxie.start(DISCORD_TOKEN) diff --git a/lynxie/assets/e621_blacklist.txt b/lynxie/assets/e621_blacklist.txt new file mode 100644 index 0000000..23e42bc --- /dev/null +++ b/lynxie/assets/e621_blacklist.txt @@ -0,0 +1,32 @@ +cub +young +teen +teenager +diaper +pregnant + +incest +father_and_son +father_and_daughter +mother_and_son +mother_and_daughter +brother_and_sister +sister_and_sister +brother_and_brother + +bestiality +feral_and_anthro + +gore +blood +vomit + +torture +rape +forced + +scat +watersports +urine +snuff +eating_feces diff --git a/lynxie/assets/bandicam.png b/lynxie/assets/overlays/bandicam.png similarity index 100% rename from lynxie/assets/bandicam.png rename to lynxie/assets/overlays/bandicam.png diff --git a/lynxie/assets/bubble.png b/lynxie/assets/overlays/bubble.png similarity index 100% rename from lynxie/assets/bubble.png rename to lynxie/assets/overlays/bubble.png diff --git a/lynxie/assets/gang.png b/lynxie/assets/overlays/gang.png similarity index 100% rename from lynxie/assets/gang.png rename to lynxie/assets/overlays/gang.png diff --git a/lynxie/assets/jerm-a.png b/lynxie/assets/overlays/jerm-a.png similarity index 100% rename from lynxie/assets/jerm-a.png rename to lynxie/assets/overlays/jerm-a.png diff --git a/lynxie/assets/jerma.png b/lynxie/assets/overlays/jerma.png similarity index 100% rename from lynxie/assets/jerma.png rename to lynxie/assets/overlays/jerma.png diff --git a/lynxie/assets/liveleak.png b/lynxie/assets/overlays/liveleak.png similarity index 100% rename from lynxie/assets/liveleak.png rename to lynxie/assets/overlays/liveleak.png diff --git a/lynxie/commands/__init__.py b/lynxie/commands/__init__.py index 98dcd98..18d7047 100644 --- a/lynxie/commands/__init__.py +++ b/lynxie/commands/__init__.py @@ -4,6 +4,7 @@ from .hello import Hello from .music import Music from .animals import Animals from .image import Img +from .e621 import E621 __all__ = [ "Help", @@ -12,4 +13,5 @@ __all__ = [ "Music", "Animals", "Img", + "E621", ] diff --git a/lynxie/commands/e621.py b/lynxie/commands/e621.py index d18803f..d9caa24 100644 --- a/lynxie/commands/e621.py +++ b/lynxie/commands/e621.py @@ -1,10 +1,100 @@ +import json +from base64 import b64encode +import requests + +import discord from discord.ext import commands +from lynxie.config import E621_API_KEY, E621_USERNAME, E621_BLACKLIST +from lynxie.utils import error_message + + +_E621_API_URL = "https://e621.net/" +_E621_AUTH = f"{E621_USERNAME}:{E621_API_KEY}".encode("utf-8") +_E621_API_HEADERS = { + "Accept": "application/json", + "Content-Type": "application/json", + "User-Agent": f"Lynxie/1.0 (by {E621_USERNAME} on e621)", + "Authorization": str(b"Basic " + b64encode(_E621_AUTH), "utf-8"), +} + class E621(commands.Cog): def __init__(self, bot): self.bot = bot @commands.command() - async def e621(self, ctx): - await ctx.reply(":3") + async def porb(self, ctx, *tags): + # Base url for the request + url = _E621_API_URL + "posts.json/?limit=1&tags=order:random+rating:e+" + caught_tags = [] + + for tag in tags: + tag = tag.lower() + url += tag + "+" + if tag in E621_BLACKLIST: + caught_tags.append(tag) + + for tag in E621_BLACKLIST: + url += f"-{tag}+" + + if caught_tags: + error = ( + "An error occurred while fetching the image! " + f"{', '.join(caught_tags)} is a blacklisted tag!" + ) + await ctx.reply(embed=error_message(error)) + return + + request = requests.get(url, headers=_E621_API_HEADERS) + response = json.loads(request.text) + + if request.status_code != 200: + error = ( + "An error occurred while fetching the image! " + f"(Error code: {str(request.status_code)})" + ) + await ctx.reply(embed=error_message(error)) + return + + if not response["posts"]: + error = "No results found for the given tags! " f"(Tags: {', '.join(tags)})" + await ctx.reply(embed=error_message(error)) + return + + embed = discord.Embed( + title="E621", + description=response["posts"][0]["description"] + or "No description provided.", + colour=discord.Colour.orange(), + ) + + embed.add_field( + name="Score", + value=f"^ {response['posts'][0]['score']['up']} | " + f"v {response['posts'][0]['score']['down']}", + ) + embed.add_field( + name="Favorites", + value=response["posts"][0]["fav_count"], + ) + + embed.add_field( + name="Source", + value=", ".join(response["posts"][0]["sources"]) or "No source provided.", + inline=False, + ) + embed.add_field( + name="Tags", + value=", ".join(response["posts"][0]["tags"]["general"]) or "No tags provided.", + inline=False, + ) + + embed.set_footer( + text=f"ID: {response['posts'][0]['id']} | " + f"Created: {response['posts'][0]['created_at']}" + ) + + embed.set_image(url=response["posts"][0]["file"]["url"]) + + await ctx.reply(embed=embed) diff --git a/lynxie/config.py b/lynxie/config.py index 62c1160..0b7b9ef 100644 --- a/lynxie/config.py +++ b/lynxie/config.py @@ -55,7 +55,7 @@ IMAGE_EXTENSIONS = ["png", "jpg", "jpeg", "webp"] IMAGE_OVERLAYS = { "bubble": { - "path": os.path.join(ASSETS_PATH, "bubble.png"), + "path": os.path.join(ASSETS_PATH, "overlays", "bubble.png"), "options": [ "default", # Positioned at top "bottom", # Positioned at bottom @@ -64,23 +64,37 @@ IMAGE_OVERLAYS = { ], }, "gang": { - "path": os.path.join(ASSETS_PATH, "gang.png"), + "path": os.path.join(ASSETS_PATH, "overlays", "gang.png"), "options": ["default"], }, "bandicam": { - "path": os.path.join(ASSETS_PATH, "bandicam.png"), + "path": os.path.join(ASSETS_PATH, "overlays", "bandicam.png"), "options": ["default"], }, "jerma": { - "path": os.path.join(ASSETS_PATH, "jerma.png"), + "path": os.path.join(ASSETS_PATH, "overlays", "jerma.png"), "options": ["default"], }, "jerm-a": { - "path": os.path.join(ASSETS_PATH, "jerm-a.png"), + "path": os.path.join(ASSETS_PATH, "overlays", "jerm-a.png"), "options": ["default"], }, "liveleak": { - "path": os.path.join(ASSETS_PATH, "liveleak.png"), + "path": os.path.join(ASSETS_PATH, "overlays", "liveleak.png"), "options": ["default"], }, } + +E621_API_KEY = ( + dotenv.dotenv_values(".env").get("E621_API_KEY") + or os.environ.get("E621_API_KEY") + or None +) +E621_USERNAME = ( + dotenv.dotenv_values(".env").get("E621_USERNAME") + or os.environ.get("E621_USERNAME") + or None +) +E621_BLACKLIST = set() +with open(os.path.join(ASSETS_PATH, "e621_blacklist.txt"), "r") as f: + [E621_BLACKLIST.add(line.strip()) for line in f.readlines() if line.strip()] diff --git a/lynxie/utils.py b/lynxie/utils.py index cbe58c1..59a5eeb 100644 --- a/lynxie/utils.py +++ b/lynxie/utils.py @@ -51,6 +51,9 @@ def error_message(error: str) -> discord.Embed: title="Error :(", description=error, colour=discord.Colour.red(), - ).set_footer(text=f"For more information, use the " f"{LYNXIE_PREFIX}help command.") + ) + embed.set_footer( + text=f"For more information, use the " f"{LYNXIE_PREFIX}help command." + ) return embed