Skip to content

Commit

Permalink
added catppify
Browse files Browse the repository at this point in the history
  • Loading branch information
raluvy95 committed May 30, 2023
1 parent 9392b04 commit 14dfa9a
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ SERVER_ID="DISCORD SERVER ID"
OWNER_ID="OWNER_ID ANOTHER_OWNER_ID"
# set 0 to disable, 1 to enable
DEBUG=0
# 0 for false, 1 for true
CATPPIFY=1
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ node_modules/
package-lock.json
.vscode/settings.json
database.sqlite
bin/
bin/

input.*
generated.*
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ bot for first time, kill it and run `npm run migrate` **with your
database.sqlite** inside the root project. It's recommend to keep
database.sqlite at this moment.

## Catppify

This is a tool to generate image using Catppuccin palette. You need to intergate `palettes` and `catppify` files from [my repo](https://github.com/raluvy95/catppify) to `./bin` directory
Set `CATPPIFY=0` in your .env file if you don't want.

# Contributing

Feel free to contribute whatever you want, as long as our build passed. (using
Expand Down
131 changes: 131 additions & 0 deletions src/commands/tools/catppify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { Message } from "eris";
import { client } from "../../client/Client";
import { MovCommand } from "../../client/Command";
import https from "https";
import { createWriteStream, readFile, unlinkSync } from "fs";
import { spawn } from "child_process";

function secondsToDhms(seconds: number): string {
seconds = Number(seconds)
const d = Math.floor(seconds / (3600 * 24));
const h = Math.floor(seconds % (3600 * 24) / 3600);
const m = Math.floor(seconds % 3600 / 60);
const s = Math.floor(seconds % 60);

const dDisplay = d > 0 ? d + "d " : "";
const hDisplay = h > 0 ? h + "h" : "";
const mDisplay = m > 0 ? m + "m " : "";
const sDisplay = s > 0 ? s + "s" : "";
return dDisplay + hDisplay + mDisplay + sDisplay;
}

function download(url: string, dest: string, cb?: ((err?: NodeJS.ErrnoException | null | undefined) => void) | undefined) {
const file = createWriteStream(dest);
https.get(url, function (response) {
response.pipe(file);
file.on('finish', function () {
file.close(cb);
});
});
}

function generator(msg: Message, args: string[]) {
if (process.env.CATPPIFY) {
let ext = !args[0] ? '' : args[0].split('.').at(-1)
let link: string = ''
let palette: string
let noise: string


if (args[0]?.startsWith("http://")) {
client.createMessage(msg.channel.id, "HTTP is not supported. Use HTTPS instead!")
return
}

if (!args[0]?.match('^https:\/\/') && !ext?.match(/(jp(e?)g|png|gif|webp)/gi)) {
if (msg.attachments.length < 1) {
client.createMessage(msg.channel.id, "Invalid link")
return
}
link = msg.attachments[0].url
ext = link.split('.').at(-1)
palette = args[0]
noise = args[1]
} else {
link = args[0]
palette = args[1]
noise = args[2]
}

if (!palette) {
palette = 'mocha'
} else {
const avaliable_palette = ['frappe', 'latte', 'macchiato', 'mocha', 'oled']
if (!avaliable_palette.includes(palette)) {
client.createMessage(msg.channel.id, `Invalid palette. Use one of the following avaliable palettes: ${avaliable_palette.join(", ")}`)
return
}
}
if (!noise) {
noise = '4'
} else {
if (isNaN(Number(noise))) {
client.createMessage(msg.channel.id, `That's not a number lol`)
return
}
const avaliable_noise = ['0', '1', '2', '3', '4']
if (!avaliable_noise.includes(noise)) {
client.createMessage(msg.channel.id, `Invalid palette. Use one of the following avaliable noises: ${avaliable_noise.join(", ")}`)
return
}
}
const init_time = Date.now()
client.sendChannelTyping(msg.channel.id)
download(link, `input.${ext}`, () => {
const process = spawn("python3", ["./bin/catppify", `input.${ext}`, '-p', palette, '-n', noise])

process.stderr.on('data', (data) => {
console.log((data as Buffer).toString())
});

process.on('exit', (code) => {
if (code !== 0) {
client.createMessage(msg.channel.id, "There's something went wrong. Please contact your developer")
return
}
readFile("./generated.png", (err, data) => {
if (err) {
console.error(err)
client.createMessage(msg.channel.id, "There's something went wrong. Please contact your developer")
return

} else {
const took = Date.now() - init_time
client.createMessage(msg.channel.id, `Here you go (Took ${secondsToDhms(took / 1000)})`, {
file: data,
name: "generated.png"
})

unlinkSync("generated.png")
unlinkSync(`input.${ext}`)
}
})

})
})
} else {
client.createMessage(msg.channel.id, "Catppify is disabled.")
return
}
}

class Catppify extends MovCommand {
constructor() {
super("catppify", generator, {
description: "Generate your image based on Catppuccin theme!",
usage: "<link or attachment> [palette] [noise]"
})
}
}

export default new Catppify();
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CommandInteraction } from 'eris';
dotenv.config()
import { client } from './client/Client';
import { debug } from './utils/debug';
import { unlink } from 'fs';

process.on("unhandledRejection", (rej) => {
console.error(rej);
Expand All @@ -29,4 +30,8 @@ client.on("interactionCreate", i => {
}
})


unlink("generated.png", () => { })
unlink("input.png", () => { })

client.connect()

0 comments on commit 14dfa9a

Please sign in to comment.