Add Dockerfile

Update blacklist
Clean up e621 code
This commit is contained in:
Michał Gdula 2023-09-12 11:32:34 +01:00
parent dbe660ded9
commit d28a759f8e
24 changed files with 119 additions and 37 deletions

View file

@ -0,0 +1,17 @@
from .help import Help
from .ping import Ping
from .hello import Hello
from .music import Music
from .animals import Animals
from .image import Img
from .e621 import E621
__all__ = [
"Help",
"Ping",
"Hello",
"Music",
"Animals",
"Img",
"E621",
]

View file

@ -0,0 +1,49 @@
import requests
from io import BytesIO
import discord
from discord.ext import commands
from lynxie.config import TINYFOX_ANIMALS
from lynxie.utils import error_message
class Animals(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def animal(self, ctx, animal_choice: str = ""):
animal_choice = animal_choice.lower().strip() or None
if not animal_choice:
error = (
f"You need to specify an animal! "
f"Try one of these: {', '.join(TINYFOX_ANIMALS)}"
)
await ctx.reply(embed=error_message(error))
return
if animal_choice not in TINYFOX_ANIMALS:
error = (
f"That animal doesn't exist! "
f"Try one of these: {', '.join(TINYFOX_ANIMALS)}"
)
await ctx.reply(embed=error_message(error))
return
async with ctx.typing():
request = requests.get(
"https://api.tinyfox.dev/img?animal=" + animal_choice
)
with BytesIO(request.content) as response:
response.seek(0)
animal_file = discord.File(response, filename="image.png")
embed = discord.Embed(
title=animal_choice.capitalize(),
colour=discord.Colour.orange(),
).set_image(url="attachment://image.png")
await ctx.reply(embed=embed, file=animal_file, mention_author=False)

114
Bot/lynxie/commands/e621.py Normal file
View file

@ -0,0 +1,114 @@
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_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 porb(self, ctx, *tags):
url = "https://e621.net/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(['`'+tag+'`' for tag in caught_tags])} "
f"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 == 503:
error = (
"The bot is currently rate limited! "
"Wait a while before trying again."
)
await ctx.reply(embed=error_message(error))
return
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"]:
tags_to_display = range(min(len(tags), 20))
error = (
"No results found for the given tags! "
f"(Tags: {', '.join(['`'+tags[i]+'`' for i in tags_to_display])})"
)
await ctx.reply(embed=error_message(error))
return
post = response["posts"][0]
general_tags = post["tags"]["general"]
embed = discord.Embed(
title="E621",
description=post["description"] or "No description provided.",
colour=discord.Colour.orange(),
)
embed.add_field(
name="Score",
value=f"⬆️ {post['score']['up']} | ⬇️ {post['score']['down']}",
)
embed.add_field(
name="Favorites",
value=post["fav_count"],
)
embed.add_field(
name="Comments",
value=post["comment_count"],
)
embed.add_field(
name="Source(s)",
value=", ".join(post["sources"]) or "No source provided.",
inline=False,
)
embed.add_field(
name="Tags",
value=(
", ".join(["`"+general_tags[i]+"`" for i in range(min(len(general_tags), 20))])
or "No tags provided."
),
inline=False,
)
embed.set_footer(text=f"ID: {post['id']} | Created: {post['created_at']}")
embed.set_image(url=post["file"]["url"])
await ctx.reply(embed=embed)

View file

@ -0,0 +1,17 @@
import discord
from discord.ext import commands
class Hello(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def hello(self, ctx):
embed = discord.Embed(
title="Hello!",
description="I'm Lynxie, a multipurpose Discord bot written in Python!",
color=discord.Color.orange(),
)
await ctx.reply(embed=embed)

View file

@ -0,0 +1,37 @@
import discord
from discord.ext import commands
from lynxie.config import LYNXIE_PREFIX
class Help(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.help_commands = {
"help": "Show this message",
"ping": "Pong!",
"hello": "Say hello to Lynxie!",
"join": "Join the voice channel you're in",
"play <url>": "Play a song from YouTube, SoundCloud, etc.",
"stop": "Stop the current song and leave the voice channel",
"animal <animal>": "Get a random image of an animal!",
"overlay <image> <style>": "Overlay an image with a "
"style, e.g. `bubble mask`",
"saveable": "Turn image into a GIF to save within Discord",
}
@commands.command()
async def help(self, ctx):
embed = discord.Embed(
title="Help",
description=f"Lynxie's prefix is `{LYNXIE_PREFIX}`",
colour=discord.Colour.orange(),
)
for command, description in self.help_commands.items():
embed.add_field(
name=command,
value=description,
inline=False,
)
await ctx.reply(embed=embed)

View file

@ -0,0 +1,243 @@
import datetime
import requests
from io import BytesIO
from PIL import Image, ImageEnhance
import discord
from discord.ext import commands
from lynxie.config import IMAGE_EXTENSIONS, IMAGE_OVERLAYS
from lynxie.utils import error_message
class Img(commands.Cog):
def __init__(self, bot):
self.bot = bot
@staticmethod
async def get_image_attachments(ctx) -> discord.Attachment or None:
if ctx.message.attachments:
return ctx.message.attachments[0]
if ctx.message.reference:
if ctx.message.reference.resolved.attachments:
return ctx.message.reference.resolved.attachments[0]
if (
ctx.message.reference.resolved.embeds
and ctx.message.reference.resolved.embeds[0].image
):
return ctx.message.reference.resolved.embeds[0].image
if ctx.message.embeds and ctx.message.embeds[0].image:
return ctx.message.embeds[0].image
channel = ctx.guild.get_channel(ctx.channel.id)
async for message in channel.history(limit=10):
if message.attachments:
return message.attachments[0]
if message.embeds and message.embeds[0].image:
return message.embeds[0].image
return None
@commands.command()
async def overlay(
self, ctx, overlay_choice: str = None, overlay_style: str = "default"
):
start_time = datetime.datetime.now()
overlay_choice = overlay_choice.lower().strip() if overlay_choice else None
overlay_style = overlay_style.lower().strip() if overlay_style else "default"
image_attachments = await self.get_image_attachments(ctx)
if not image_attachments:
error = "No image was found!"
await ctx.reply(embed=error_message(error))
return
if not overlay_choice or overlay_choice not in IMAGE_OVERLAYS:
error = (
f"Invalid overlay choice! Use one of "
f"these: {', '.join(IMAGE_OVERLAYS)}"
)
await ctx.reply(embed=error_message(error))
return
if overlay_style not in IMAGE_OVERLAYS[overlay_choice]["options"]:
error = (
f"{overlay_choice} has these "
f"options: {', '.join(IMAGE_OVERLAYS[overlay_choice]['options'])}"
)
await ctx.reply(embed=error_message(error))
return
# Defaults to gwa as I cant be asked to make a better error handler
file_name = (
image_attachments.filename or image_attachments.url or "balls"
).lower()
file_extension = file_name.split(".")[-1]
if file_extension not in IMAGE_EXTENSIONS:
error = (
f"Unsupported file type! Use one "
f"of these: {', '.join(IMAGE_EXTENSIONS)}"
)
await ctx.reply(embed=error_message(error))
return
if image_attachments.size and image_attachments.size > 8 * 1024 * 1024:
error = "Image must be less than 8MB!"
await ctx.reply(embed=error_message(error))
return
width, height = image_attachments.width, image_attachments.height
if not 10 < width <= 4500 or not 10 < height <= 4500:
error = "Image must be at least over 10x10 and under 4500x4500!"
await ctx.reply(embed=error_message(error))
return
async with ctx.typing():
request = requests.get(image_attachments.url)
attachment = Image.open(BytesIO(request.content))
width, height = attachment.width, attachment.height
if width < height:
attachment.thumbnail((200, height))
else:
attachment.thumbnail((width, 200))
width, height = attachment.width, attachment.height
if overlay_choice == "bubble":
overlay = Image.open(IMAGE_OVERLAYS[overlay_choice]["path"])
overlay = overlay.resize((width, overlay.height))
if overlay_style in ["default", "top"]:
attachment.paste(overlay, (0, 0), overlay)
elif overlay_style in ["bottom"]:
overlay = overlay.rotate(180)
attachment.paste(overlay, (0, height - overlay.height), overlay)
elif overlay_style in ["mask", "mask-bottom"]:
# This is a lazy method of creating a mask
# 1. Reduce brightness of overlay to 0 (black)
# 2. Create a white square the size of the image
# 3. Paste the overlay onto the white square
overlay = ImageEnhance.Brightness(overlay).enhance(0)
mask = Image.new("RGB", (width, height), (255, 255, 255))
mask.paste(overlay, (0, 0), overlay)
if overlay_style == "mask-bottom":
mask = mask.rotate(180)
mask = mask.convert("L")
attachment.putalpha(mask)
elif overlay_choice == "gang":
overlay = Image.open(IMAGE_OVERLAYS[overlay_choice]["path"])
position = ((width - overlay.width) // 2, (height - overlay.height))
attachment.paste(overlay, position, overlay)
elif overlay_choice == "bandicam":
overlay = Image.open(IMAGE_OVERLAYS[overlay_choice]["path"])
overlay.thumbnail((width, overlay.height))
attachment.paste(overlay, ((width - overlay.width) // 2, 0), overlay)
elif overlay_choice == "jerma":
overlay = Image.open(IMAGE_OVERLAYS[overlay_choice]["path"])
overlay.thumbnail((width, overlay.height))
attachment.paste(
overlay, (width - overlay.width, height - overlay.height), overlay
)
elif overlay_choice == "jerm-a":
overlay = Image.open(IMAGE_OVERLAYS[overlay_choice]["path"])
overlay.thumbnail((width, overlay.height))
attachment.paste(
overlay,
((width - overlay.width) // 2, height - overlay.height),
overlay,
)
elif overlay_choice == "liveleak":
overlay = Image.open(IMAGE_OVERLAYS[overlay_choice]["path"])
overlay.thumbnail((width, overlay.height))
attachment.paste(overlay, (0, 0), overlay)
with BytesIO() as response:
attachment.save(response, format="PNG")
response.seek(0)
response = discord.File(response, filename="image.png")
time_taken = (datetime.datetime.now() - start_time).microseconds / 1000
embed = (
discord.Embed(
title=overlay_choice.capitalize(),
colour=discord.Colour.orange(),
)
.set_image(url="attachment://image.png")
.set_footer(text=f"{width}x{height}, {time_taken}ms")
)
await ctx.reply(embed=embed, file=response, mention_author=False)
@commands.command()
async def saveable(self, ctx):
start_time = datetime.datetime.now()
image_attachments = await self.get_image_attachments(ctx)
if not image_attachments:
error = "No image was found!"
await ctx.reply(embed=error_message(error))
return
# Defaults to gwa as I cant be asked to make a better error handler
file_name = (
image_attachments.filename or image_attachments.url or "balls"
).lower()
file_extension = file_name.split(".")[-1]
if file_extension not in IMAGE_EXTENSIONS:
error = (
f"Unsupported file type! Use one "
f"of these: {', '.join(IMAGE_EXTENSIONS)}"
)
await ctx.reply(embed=error_message(error))
return
if image_attachments.size and image_attachments.size > 8 * 1024 * 1024:
error = "Image must be less than 8MB!"
await ctx.reply(embed=error_message(error))
return
width, height = image_attachments.width, image_attachments.height
if not 10 < width <= 4500 or not 10 < height <= 4500:
error = "Image must be at least 10x10 and under 4500x4500!"
await ctx.reply(embed=error_message(error))
return
async with ctx.typing():
request = requests.get(image_attachments.url)
attachment = Image.open(BytesIO(request.content))
width, height = attachment.width, attachment.height
with BytesIO() as response:
attachment.save(response, format="GIF")
response.seek(0)
response = discord.File(response, filename="image.gif")
time_taken = (datetime.datetime.now() - start_time).microseconds / 1000
embed = (
discord.Embed(
title="Saveable",
description="Image converted to GIF, "
"click the star to save it :3",
colour=discord.Colour.orange(),
)
.set_image(url="attachment://image.gif")
.set_footer(text=f"{width}x{height}, {time_taken}ms")
)
await ctx.reply(embed=embed, file=response, mention_author=False)

View file

@ -0,0 +1,55 @@
import yt_dlp
import discord
from discord.ext import commands
from lynxie.utils import error_message
ffmpeg_options = {"options": "-vn"}
ydl_opts = {"format": "bestaudio"}
ytdl = yt_dlp.YoutubeDL(ydl_opts)
class Music(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def join(self, ctx, *, channel: discord.VoiceChannel):
if ctx.voice_client is not None:
return await ctx.voice_client.move_to(channel)
await channel.connect()
@commands.command()
async def play(self, ctx, *, url):
async with ctx.typing():
song_info = ytdl.extract_info(url, download=False)
ctx.voice_client.play(
discord.FFmpegPCMAudio(song_info["url"], **ffmpeg_options)
)
embed = discord.Embed(
title="Now playing",
description=f"[{song_info['title']}]({song_info['webpage_url']})",
color=discord.Color.orange(),
)
await ctx.reply(embed=embed, mention_author=False)
@commands.command()
async def stop(self, ctx):
await ctx.voice_client.disconnect()
@play.before_invoke
async def ensure_voice(self, ctx):
if ctx.voice_client is None:
if ctx.author.voice:
await ctx.author.voice.channel.connect()
else:
error = "You are not connected to a voice channel."
await ctx.reply(
embed=error_message(error),
mention_author=False,
)
return
elif ctx.voice_client.is_playing():
ctx.voice_client.stop()

View file

@ -0,0 +1,16 @@
import discord
from discord.ext import commands
class Ping(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def ping(self, ctx):
embed = discord.Embed(
title="Pong!",
description=f"{round(self.bot.latency * 1000)}ms",
color=discord.Color.orange(),
)
await ctx.reply(embed=embed, mention_author=False)