Skip to content

Commit

Permalink
feat: mygo
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjjrt committed Jan 2, 2025
1 parent e4df907 commit 1dff914
Showing 5 changed files with 292 additions and 3 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@
"sequelize": "^6.37.4",
"socket.io": "^4.8.0",
"sqlite3": "^5.1.7",
"stream-buffers": "^3.0.3",
"undici": "^6.19.8",
"uuid": "^9.0.1",
"yt-dlp-wrap": "^2.3.12",
@@ -49,9 +50,11 @@
"@types/fluent-ffmpeg": "^2.1.26",
"@types/node": "^18.19.54",
"@types/seedrandom": "^3.0.8",
"@types/stream-buffers": "^3.0.7",
"@types/uuid": "^9.0.8",
"@types/yt-search": "^2.10.3",
"prettier": "^2.8.8",
"sequelize-auto": "^0.8.8",
"tsx": "^4.19.1",
"typescript": "^5.6.2",
"vitepress": "^1.3.4"
105 changes: 105 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions src/commands/mygo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { ErrorEmbed, InfoEmbed } from "./../utils/Embed";
import { SlashCommandBuilder, CommandInteraction, AutocompleteInteraction, AttachmentBuilder } from "discord.js";
import { client, subscriptions } from "../index";
import { Database, RunResult } from "sqlite3";
import { resolve } from "node:path";
import { Mygo } from "utils/db/schema";
import { col, fn, Op } from "sequelize";
import streamBuffers from "stream-buffers";
import Ffmpeg from "fluent-ffmpeg";

const { DB_DIR } = process.env;

export default {
data: new SlashCommandBuilder()
.setName("mygo")
.setDescription("Get MyGO!!!!! picture")
.addStringOption((option) => option.setName("text").setDescription("text").setRequired(true).setAutocomplete(true)),
async execute(interaction: CommandInteraction) {
let subscription = subscriptions.get(interaction.guildId!);
const query = interaction.options.get("text", true).value as string;

const res = await Mygo.findOne({
attributes: [
"text",
"episode",
[fn("MIN", col("frame")), "min_frame"],
[fn("MAX", col("frame")), "max_frame"],
"segment_id",
],
where: {
text: query,
},
group: ["segment_id"],
});

console.log(res);

if (!res) {
return;
}

const writableStreamBuffer = new streamBuffers.WritableStreamBuffer({
initialSize: 1024 * 1024, // Initial size (1MB)
incrementAmount: 1024 * 1024, // Growth size (1MB)
});

Ffmpeg(`video/${res.getDataValue("episode")}.mp4`)
.seekInput((res.getDataValue("max_frame") + res.getDataValue("min_frame")) / 2 / 23.98)
.outputOptions([
"-vframes",
"1"
])
.toFormat("image2") // Set the output format to GIF
.on("start", (commandLine) => {
console.log("FFmpeg command: ", commandLine);
})
.on("error", async (err) => {
await interaction.reply({
embeds: [
new ErrorEmbed(
interaction.client.user,
`Error`,
JSON.stringify(err)
),
],
});
return;
})
.on("end", async () => {
console.log("GIF created successfully!");
const gif = writableStreamBuffer.getContents();
if (!gif) return;
const attachment = new AttachmentBuilder(gif);
await interaction.reply({
files: [attachment],
});
})
.pipe(writableStreamBuffer, { end: true }); // Pipe output to the buffer
},
async autocomplete(interaction: AutocompleteInteraction) {
const query = interaction.options.get("text", true).value as string;

const res = await Mygo.findAll({
attributes: ["text"],
where: {
text: {
[Op.like]: `%${query}%`,
},
},
group: ["segment_id"],
limit: 10,
});

await interaction.respond(
res.map((r) => {
return {
name: r.getDataValue("text"),
value: r.getDataValue("text"),
};
})
);
},
};
10 changes: 9 additions & 1 deletion src/utils/db/index.ts
Original file line number Diff line number Diff line change
@@ -26,8 +26,16 @@ export const permissions = new Sequelize("permissions", "user", "password", {
storage: path.join(DB_DIR as string, "permissions.sqlite"),
});

export const mygo = new Sequelize({
host: "localhost",
dialect: "sqlite",
logging: false,
// SQLite only
storage: path.join(DB_DIR as string, "mygo.sqlite"),
});

export const initDB = () => {
return Promise.all([history, record, permissions].map((db) => db.sync()));
return Promise.all([history, record, permissions, mygo].map((db) => db.sync()));
};

export default history;
Loading

0 comments on commit 1dff914

Please sign in to comment.