Merge pull request #27 from Fluffy-Bean/go

Go
This commit is contained in:
Michał Gdula 2025-03-31 11:32:42 +01:00 committed by GitHub
commit 56946f56a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
36 changed files with 607 additions and 3687 deletions

View file

@ -1,13 +0,0 @@
version = 1
[[analyzers]]
name = "docker"
[[analyzers]]
name = "python"
[analyzers.meta]
runtime_version = "3.x.x"
[[transformers]]
name = "black"

12
.gitignore vendored
View file

@ -1,9 +1,9 @@
# IDEA
/shelf/
/workspace.xml
/httpRequests/
/dataSources/
/dataSources.local.xml
# Editor
.idea
.vscode
# General
.env
# Build files
lynxie

View file

@ -1,23 +0,0 @@
# syntax=docker/dockerfile:1
FROM alpine:3.18.2
# Make a directory for the app
RUN mkdir /app
RUN mkdir /app/lynxie
WORKDIR /app
# Copy the app files
COPY ./lynxie /app/lynxie
COPY ./poetry.lock /app
COPY ./pyproject.toml /app
# Install dependencies
RUN apk update
RUN apk --no-cache add python3 py3-pip curl
# Install poetry
RUN curl -sSL https://install.python-poetry.org | python3 -
RUN /root/.local/bin/poetry install
# Run the app
CMD ["/root/.local/bin/poetry", "run", "python3", "/app/lynxie/main.py"]

View file

@ -1,39 +0,0 @@
cub
young
younger_penetrated
teen
teenager
diaper
pregnant
loli
incest
parent_and_child
father_and_child
mother_and_child
brother_and_sister
sister_and_sister
brother_and_brother
age-difference
bestiality
feral_and_anthro
human_on_feral
feral_on_human
anthro_on_feral
feral_on_anthro
gore
blood
vomit
torture
rape
forced
scat
watersports
urine
snuff
eating_feces
shota

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 934 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

View file

@ -1,17 +0,0 @@
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

@ -1,49 +0,0 @@
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)

View file

@ -1,119 +0,0 @@
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

@ -1,17 +0,0 @@
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

@ -1,37 +0,0 @@
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

@ -1,243 +0,0 @@
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

@ -1,55 +0,0 @@
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

@ -1,16 +0,0 @@
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)

View file

@ -1,87 +0,0 @@
import os
import re
import requests
import dotenv
from discord import Object
LYNXIE_PREFIX = "?"
DISCORD_TOKEN = (
dotenv.dotenv_values(".env").get("DISCORD_TOKEN")
or os.environ.get("DISCORD_TOKEN")
or None
)
DISCORD_GUILD_ID = Object(id=1040757387033849976)
LYNXIE_PREFIX = "?"
DATA_PATH = os.path.join("lynxie", "data")
ASSETS_PATH = os.path.join("lynxie", "assets")
DATABASE_URI = "sqlite:///" + os.path.join(DATA_PATH, "lynxie.db")
# https://tinyfox.dev/docs/
tinyfox_animals = requests.get("https://api.tinyfox.dev/img?animal=animal&json").json()
TINYFOX_ANIMALS = tinyfox_animals["available"]
IMAGE_EXTENSIONS = ["png", "jpg", "jpeg", "webp"]
IMAGE_OVERLAYS = {
"bubble": {
"path": os.path.join(ASSETS_PATH, "overlays", "bubble.png"),
"options": [
"default", # Positioned at top
"bottom", # Positioned at bottom
"mask", # Positioned at top, but transparent
"mask-bottom", # Positioned at bottom, but transparent
],
},
"gang": {
"path": os.path.join(ASSETS_PATH, "overlays", "gang.png"),
"options": ["default"],
},
"bandicam": {
"path": os.path.join(ASSETS_PATH, "overlays", "bandicam.png"),
"options": ["default"],
},
"jerma": {
"path": os.path.join(ASSETS_PATH, "overlays", "jerma.png"),
"options": ["default"],
},
"jerm-a": {
"path": os.path.join(ASSETS_PATH, "overlays", "jerm-a.png"),
"options": ["default"],
},
"liveleak": {
"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 file:
for line in file.readlines():
if word := line.strip():
E621_BLACKLIST.add(word)
BAD_WORDS = []
_bad_words_request = requests.get(
"https://raw.githubusercontent.com/mogade/badwords/master/en.txt"
)
for word in _bad_words_request.text.split("\n"):
if word := word.strip():
BAD_WORDS.append(re.compile(word, re.IGNORECASE))

View file

@ -1,58 +0,0 @@
#!/usr/bin/env python3
# vim: set fileencoding=utf-8 :
import asyncio
import discord
from discord.ext import commands
from discord.gateway import DiscordWebSocket
from lynxie.config import DISCORD_TOKEN, LYNXIE_PREFIX
from lynxie.utils import mobile_status, error_message
from lynxie.commands import Help, Ping, Hello, Music, Animals, Img, E621
DiscordWebSocket.identify = mobile_status
lynxie = commands.Bot(
intents=discord.Intents.all(),
command_prefix=LYNXIE_PREFIX,
help_command=None,
)
@lynxie.event
async def on_ready():
print(f"Logged in as {lynxie.user} (ID: {lynxie.user.id})")
@lynxie.event
async def on_command(ctx):
if ctx.author == lynxie.user or ctx.author.bot:
return
@lynxie.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
return
print(error)
error = "An internal error occurred while processing your command, oopsie..."
await ctx.reply(embed=error_message(error), delete_after=5)
async def run():
async with lynxie:
await lynxie.add_cog(Help(lynxie))
await lynxie.add_cog(Ping(lynxie))
await lynxie.add_cog(Hello(lynxie))
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)
if __name__ == "__main__":
asyncio.run(run())

View file

@ -1,59 +0,0 @@
import sys
import discord
from discord.gateway import _log
from lynxie.config import LYNXIE_PREFIX
async def mobile_status(self):
payload = {
"op": self.IDENTIFY,
"d": {
"token": self.token,
"properties": {
"$os": sys.platform,
"$browser": "Discord Android",
"$device": "Discord Android",
"$referrer": "",
"$referring_domain": "",
},
"compress": True,
"large_threshold": 250,
"v": 3,
},
}
if self.shard_id is not None and self.shard_count is not None:
payload["d"]["shard"] = [self.shard_id, self.shard_count]
state = self._connection
if state._activity is not None or state._status is not None:
payload["d"]["presence"] = {
"status": state._status,
"game": state._activity,
"since": 0,
"afk": False,
}
if state._intents is not None:
payload["d"]["intents"] = state._intents.value
await self.call_hooks(
"before_identify", self.shard_id, initial=self._initial_identify
)
await self.send_as_json(payload)
_log.info("Shard ID %s has sent the IDENTIFY payload.", self.shard_id)
def error_message(error: str) -> discord.Embed:
print("Error: " + error)
embed = discord.Embed(
title="Error :(",
description=error,
colour=discord.Colour.red(),
)
embed.set_footer(
text=f"For more information, use the " f"{LYNXIE_PREFIX}help command."
)
return embed

1396
Bot/poetry.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,21 +0,0 @@
[tool.poetry]
name = "lynxie"
version = "0.1.0"
description = ""
authors = ["Fluffy <michal-gdula@protonmail.com>"]
[tool.poetry.dependencies]
python = "^3.11"
discord = "^2.3.2"
discord-py = {extras = ["voice"], version = "^2.3.2"}
black = "^24.3.0"
sqlalchemy = "^2.0.20"
python-dotenv = "^1.0.0"
requests = "^2.31.0"
yt-dlp = "^2024.04.09"
pillow = "^10.3.0"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

View file

@ -1,4 +1,4 @@
# Lynxie
Lynxie the worst Discord bot out there
![Lynxie bot icon](Lynxie.png)
![Lynxie bot icon](.github/Lynxie.png)

140
app/app.go Normal file
View file

@ -0,0 +1,140 @@
package app
import (
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/Fluffy-Bean/lynxie/utils"
"github.com/bwmarrin/discordgo"
)
type Callback func(h *Handler, args []string) Error
type Config struct {
Prefix string
Token string
Intents discordgo.Intent
}
type App struct {
Config Config
Commands map[string]Callback
}
func NewApp(config Config) *App {
return &App{
Config: config,
Commands: make(map[string]Callback),
}
}
func (a *App) RegisterCommand(cmd string, f Callback) {
a.Commands[cmd] = f
}
func (a *App) Run() {
dg, err := discordgo.New("Bot " + a.Config.Token)
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
dg.AddHandler(a.handler)
dg.Identify.Intents = a.Config.Intents
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
dg.Close()
}
type Handler struct {
Session *discordgo.Session
Message *discordgo.MessageCreate
Reference *discordgo.MessageReference
}
func (a *App) handler(session *discordgo.Session, message *discordgo.MessageCreate) {
h := &Handler{
Session: session,
Message: message,
Reference: &discordgo.MessageReference{
ChannelID: message.ChannelID,
MessageID: message.ID,
},
}
if h.Message.Author.ID == h.Session.State.User.ID {
return
}
if h.Message.Author.Bot {
return
}
var cmd string
var args string
cmd = h.Message.Content
cmd = strings.TrimPrefix(cmd, a.Config.Prefix)
cmd, args, _ = strings.Cut(cmd, " ")
callback, ok := a.Commands[cmd]
if !ok {
// Falling back to default help command
if cmd == "help" {
printHelp(a, h)
}
return
}
h.Session.ChannelTyping(h.Message.ChannelID)
err := callback(h, strings.Split(args, " "))
if !err.Ok() {
printError(a, h, err)
}
}
func printHelp(a *App, h *Handler) {
var commands []string
for cmd := range a.Commands {
commands = append(commands, cmd)
}
h.Session.ChannelMessageSendComplex(h.Message.ChannelID, &discordgo.MessageSend{
Embed: &discordgo.MessageEmbed{
Title: "Help",
Description: strings.Join(commands, "\n"),
Color: utils.ColorFromRGB(255, 255, 255),
},
Reference: h.Reference,
})
}
func printError(a *App, h *Handler, e Error) {
log.Println(e.Err)
h.Session.ChannelMessageSendComplex(h.Message.ChannelID, &discordgo.MessageSend{
Embed: &discordgo.MessageEmbed{
Title: "Error",
Description: e.Msg,
Color: utils.ColorFromRGB(255, 0, 0),
},
Reference: h.Reference,
})
}

10
app/errors.go Normal file
View file

@ -0,0 +1,10 @@
package app
type Error struct {
Msg string
Err error
}
func (e *Error) Ok() bool {
return e.Err == nil
}

97
commands/meta.go Normal file
View file

@ -0,0 +1,97 @@
package commands
import (
"flag"
"fmt"
"runtime"
"runtime/debug"
"strings"
"time"
"github.com/Fluffy-Bean/lynxie/app"
"github.com/Fluffy-Bean/lynxie/utils"
"github.com/bwmarrin/discordgo"
)
func RegisterMetaCommands(a *app.App) {
a.RegisterCommand("ping", registerPong(a))
a.RegisterCommand("debug", registerDebug(a))
}
func registerPong(a *app.App) app.Callback {
return func(h *app.Handler, args []string) app.Error {
var options struct {
latency bool
}
cmd := flag.NewFlagSet("", flag.ContinueOnError)
cmd.BoolVar(&options.latency, "latency", false, "Display the latency of ping")
cmd.Parse(args)
var content string
if options.latency {
content = fmt.Sprintf("Pong! %dms", h.Session.HeartbeatLatency().Milliseconds())
} else {
content = "Pong!"
}
h.Session.ChannelMessageSendComplex(h.Message.ChannelID, &discordgo.MessageSend{
Embed: &discordgo.MessageEmbed{
Description: content,
Color: utils.ColorFromRGB(255, 255, 255),
},
Reference: h.Reference,
})
return app.Error{}
}
}
func registerDebug(a *app.App) app.Callback {
return func(h *app.Handler, args []string) app.Error {
modified := false
revision := "-"
tags := "-"
_go := strings.TrimPrefix(runtime.Version(), "go")
gcCount := runtime.MemStats{}.NumGC
localTime := time.Now().Local().Format("2006-01-02 15:04:05")
info, _ := debug.ReadBuildInfo()
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
revision = setting.Value
case "vcs.modified":
modified = setting.Value == "true"
case "-tags":
tags = strings.ReplaceAll(setting.Value, ",", " ")
}
}
if modified {
revision += " (uncommitted changes)"
}
h.Session.ChannelMessageSendComplex(h.Message.ChannelID, &discordgo.MessageSend{
Embed: &discordgo.MessageEmbed{
Description: strings.Join(
[]string{
"```",
"Revision: " + revision,
"Build Tags: " + tags,
"Go version: " + _go,
"OS/Arch: " + runtime.GOOS + "/" + runtime.GOARCH,
"GC Count: " + fmt.Sprint(gcCount),
"Local Time: " + localTime,
"```",
},
"\n",
),
Color: utils.ColorFromRGB(255, 255, 255),
},
Reference: h.Reference,
})
return app.Error{}
}
}

212
commands/porb.go Normal file
View file

@ -0,0 +1,212 @@
package commands
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/Fluffy-Bean/lynxie/app"
"github.com/Fluffy-Bean/lynxie/utils"
"github.com/bwmarrin/discordgo"
)
func RegisterPorbCommands(a *app.App) {
a.RegisterCommand("e621", registerE621(a))
}
func registerE621(a *app.App) app.Callback {
username := os.Getenv("E621_USERNAME")
password := os.Getenv("E621_PASSWORD")
client := http.Client{
Timeout: 10 * time.Second,
}
return func(h *app.Handler, args []string) app.Error {
var options struct {
tags string
order string
rating string
}
cmd := flag.NewFlagSet("", flag.ContinueOnError)
cmd.StringVar(&options.order, "order", "random", "Search order")
cmd.StringVar(&options.rating, "rating", "e", "Search rating")
cmd.StringVar(&options.tags, "tags", "", "Search tags")
cmd.Parse(args)
req, err := http.NewRequest(
http.MethodGet,
fmt.Sprintf(
"https://e621.net/posts.json/?limit=1&tags=order:%s+rating:%s+%s",
options.order,
options.rating,
options.tags,
),
nil,
)
if err != nil {
return app.Error{
Msg: "Failed to make request",
Err: err,
}
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", fmt.Sprintf("Lynxie/1.0 (by %s on e621)", username))
req.SetBasicAuth(username, password)
res, err := client.Do(req)
if err != nil {
return app.Error{
Msg: "Failed to do request",
Err: err,
}
}
defer res.Body.Close()
var data struct {
Posts []struct {
Id int `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
File struct {
Width int `json:"width"`
Height int `json:"height"`
Ext string `json:"ext"`
Size int `json:"size"`
Md5 string `json:"md5"`
Url string `json:"url"`
} `json:"file"`
Preview struct {
Width int `json:"width"`
Height int `json:"height"`
Url string `json:"url"`
} `json:"preview"`
Sample struct {
Has bool `json:"has"`
Height int `json:"height"`
Width int `json:"width"`
Url string `json:"url"`
Alternates struct {
} `json:"alternates"`
} `json:"sample"`
Score struct {
Up int `json:"up"`
Down int `json:"down"`
Total int `json:"total"`
} `json:"score"`
Tags struct {
General []string `json:"general"`
Artist []string `json:"artist"`
Contributor []interface{} `json:"contributor"`
Copyright []string `json:"copyright"`
Character []interface{} `json:"character"`
Species []string `json:"species"`
Invalid []interface{} `json:"invalid"`
Meta []string `json:"meta"`
Lore []interface{} `json:"lore"`
} `json:"tags"`
LockedTags []interface{} `json:"locked_tags"`
ChangeSeq int `json:"change_seq"`
Flags struct {
Pending bool `json:"pending"`
Flagged bool `json:"flagged"`
NoteLocked bool `json:"note_locked"`
StatusLocked bool `json:"status_locked"`
RatingLocked bool `json:"rating_locked"`
Deleted bool `json:"deleted"`
} `json:"flags"`
Rating string `json:"rating"`
FavCount int `json:"fav_count"`
Sources []string `json:"sources"`
Pools []int `json:"pools"`
Relationships struct {
ParentId interface{} `json:"parent_id"`
HasChildren bool `json:"has_children"`
HasActiveChildren bool `json:"has_active_children"`
Children []interface{} `json:"children"`
} `json:"relationships"`
ApproverId interface{} `json:"approver_id"`
UploaderId int `json:"uploader_id"`
Description string `json:"description"`
CommentCount int `json:"comment_count"`
IsFavorited bool `json:"is_favorited"`
HasNotes bool `json:"has_notes"`
Duration interface{} `json:"duration"`
} `json:"posts"`
}
json.NewDecoder(res.Body).Decode(&data)
if len(data.Posts) == 0 {
return app.Error{
Msg: "No posts found",
Err: fmt.Errorf("no posts found"),
}
}
var description string
if len(data.Posts[0].Description) > 0 {
description = data.Posts[0].Description
} else {
description = "No description provided."
}
var generalTags string
if len(data.Posts[0].Tags.General) > 0 {
generalTags = strings.Join(data.Posts[0].Tags.General[:20], ", ")
} else {
generalTags = "No tags provided."
}
h.Session.ChannelMessageSendComplex(h.Message.ChannelID, &discordgo.MessageSend{
Embed: &discordgo.MessageEmbed{
Title: "E621",
Description: description,
Fields: []*discordgo.MessageEmbedField{
{
Name: "Score",
Value: fmt.Sprintf("⬆️ %d | ⬇️ %d", data.Posts[0].Score.Up, data.Posts[0].Score.Down),
},
{
Name: "Favorites",
Value: fmt.Sprintf("%d", data.Posts[0].FavCount),
},
{
Name: "Comments",
Value: fmt.Sprintf("%d", data.Posts[0].CommentCount),
},
{
Name: "Source(s)",
Value: strings.Join(data.Posts[0].Sources, ", "),
Inline: false,
},
{
Name: "Tag(s)",
Value: generalTags,
Inline: false,
},
},
Image: &discordgo.MessageEmbedImage{
URL: data.Posts[0].File.Url,
},
Footer: &discordgo.MessageEmbedFooter{
Text: fmt.Sprintf(
"ID: %d | Created: %s",
data.Posts[0].Id,
data.Posts[0].CreatedAt.Format(time.DateTime),
),
},
Color: utils.ColorFromRGB(255, 255, 255),
},
Reference: h.Reference,
})
return app.Error{}
}
}

90
commands/tinyfox.go Normal file
View file

@ -0,0 +1,90 @@
package commands
import (
"errors"
"flag"
"fmt"
"net/http"
"slices"
"time"
"github.com/Fluffy-Bean/lynxie/app"
"github.com/Fluffy-Bean/lynxie/utils"
"github.com/bwmarrin/discordgo"
)
func RegisterTinyfoxCommands(a *app.App) {
a.RegisterCommand("animal", registerAnimal(a))
}
func registerAnimal(a *app.App) app.Callback {
animals := []string{
"fox", "yeen", "dog", "guara", "serval", "ott", "jackal", "bleat", "woof", "chi", "puma", "skunk", "tig", "wah",
"manul", "snep", "jaguar", "badger", "chee", "racc", "bear", "capy", "bun", "marten", "caracal", "snek",
"shiba", "dook", "leo", "yote", "poss", "chee", "lynx",
}
client := http.Client{
Timeout: 10 * time.Second,
}
return func(h *app.Handler, args []string) app.Error {
var options struct {
animal string
}
cmd := flag.NewFlagSet("", flag.ContinueOnError)
cmd.StringVar(&options.animal, "animal", "", "Get an image of an animal!")
cmd.Parse(args)
if options.animal == "" {
return app.Error{
Msg: "Animal name is required!",
Err: errors.New("animal name is required"),
}
}
if !slices.Contains(animals, options.animal) {
return app.Error{
Msg: fmt.Sprintf("Animal %s is invalid", options.animal),
Err: errors.New("entered invalid animal name"),
}
}
req, err := http.NewRequest(http.MethodGet, "https://api.tinyfox.dev/img?animal="+options.animal, nil)
if err != nil {
return app.Error{
Msg: "Failed to make request",
Err: err,
}
}
res, err := client.Do(req)
if err != nil {
return app.Error{
Msg: "Failed to do request",
Err: err,
}
}
defer res.Body.Close()
h.Session.ChannelMessageSendComplex(h.Message.ChannelID, &discordgo.MessageSend{
Embed: &discordgo.MessageEmbed{
Title: "Animal",
Image: &discordgo.MessageEmbedImage{
URL: "attachment://image.png",
},
Color: utils.ColorFromRGB(255, 255, 255),
},
Files: []*discordgo.File{
{
Name: "image.png",
ContentType: "",
Reader: res.Body,
},
},
Reference: h.Reference,
})
return app.Error{}
}
}

View file

@ -1,13 +0,0 @@
version: "3.9"
services:
bot:
build: Bot
restart: unless-stopped
volumes:
- ./Bot/data:/app/data
environment:
DISCORD_TOKEN: ${DISCORD_TOKEN}
DISCORD_GUILD_ID: ${DISCORD_GUILD_ID}
E621_USERNAME: ${E621_USERNAME}
E621_API_KEY: ${E621_API_KEY}

11
go.mod Normal file
View file

@ -0,0 +1,11 @@
module github.com/Fluffy-Bean/lynxie
go 1.24.0
require github.com/bwmarrin/discordgo v0.28.1
require (
github.com/gorilla/websocket v1.4.2 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
)

12
go.sum Normal file
View file

@ -0,0 +1,12 @@
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

23
main.go Normal file
View file

@ -0,0 +1,23 @@
package main
import (
"os"
"github.com/Fluffy-Bean/lynxie/app"
"github.com/Fluffy-Bean/lynxie/commands"
"github.com/bwmarrin/discordgo"
)
func main() {
a := app.NewApp(app.Config{
Prefix: ">",
Token: os.Getenv("TOKEN"),
Intents: discordgo.IntentsGuildMessages,
})
commands.RegisterMetaCommands(a)
commands.RegisterTinyfoxCommands(a)
commands.RegisterPorbCommands(a)
a.Run()
}

1396
poetry.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,22 +0,0 @@
[tool.poetry]
name = "lynxie"
version = "0.1.0"
description = ""
authors = ["Fluffy <michal-gdula@protonmail.com>"]
readme = ".github/README.md"
[tool.poetry.dependencies]
python = "^3.11"
discord = "^2.3.2"
discord-py = {extras = ["voice"], version = "^2.3.2"}
black = "^24.3.0"
sqlalchemy = "^2.0.20"
python-dotenv = "^1.0.0"
requests = "^2.31.0"
yt-dlp = "^2024.04.09"
pillow = "^10.3.0"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

5
utils/color.go Normal file
View file

@ -0,0 +1,5 @@
package utils
func ColorFromRGB(r, g, b int) int {
return (r << 16) + (g << 8) + b
}