2024-05-08 02:10:14 +02:00
|
|
|
import { Args } from "@oclif/core";
|
2025-01-23 16:08:42 +01:00
|
|
|
import { Emoji, Media } from "@versia/kit/db";
|
2024-11-01 21:05:54 +01:00
|
|
|
import { Emojis } from "@versia/kit/tables";
|
2024-05-08 02:10:14 +02:00
|
|
|
import chalk from "chalk";
|
2024-06-13 06:52:01 +02:00
|
|
|
import { and, eq, isNull } from "drizzle-orm";
|
2024-05-09 04:07:33 +02:00
|
|
|
import ora from "ora";
|
2024-06-13 01:10:40 +02:00
|
|
|
import { BaseCommand } from "~/cli/base";
|
2024-05-29 02:59:49 +02:00
|
|
|
import { config } from "~/packages/config-manager";
|
2024-05-08 02:10:14 +02:00
|
|
|
|
|
|
|
|
export default class EmojiAdd extends BaseCommand<typeof EmojiAdd> {
|
2024-11-01 21:20:12 +01:00
|
|
|
public static override args = {
|
2024-05-08 02:10:14 +02:00
|
|
|
shortcode: Args.string({
|
|
|
|
|
description: "Shortcode of the emoji",
|
|
|
|
|
required: true,
|
|
|
|
|
}),
|
|
|
|
|
file: Args.string({
|
|
|
|
|
description: "Path to the image file (can be an URL)",
|
|
|
|
|
required: true,
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-01 21:20:12 +01:00
|
|
|
public static override description = "Adds a new emoji";
|
2024-05-08 02:10:14 +02:00
|
|
|
|
2024-11-01 21:20:12 +01:00
|
|
|
public static override examples = [
|
2024-05-09 04:07:33 +02:00
|
|
|
"<%= config.bin %> <%= command.id %> baba_yassie ./emojis/baba_yassie.png",
|
|
|
|
|
"<%= config.bin %> <%= command.id %> baba_yassie https://example.com/emojis/baba_yassie.png",
|
|
|
|
|
];
|
2024-05-08 02:10:14 +02:00
|
|
|
|
2024-11-01 21:20:12 +01:00
|
|
|
public static override flags = {};
|
2024-05-08 02:10:14 +02:00
|
|
|
|
|
|
|
|
public async run(): Promise<void> {
|
2024-05-29 03:14:24 +02:00
|
|
|
const { args } = await this.parse(EmojiAdd);
|
2024-05-08 02:10:14 +02:00
|
|
|
|
|
|
|
|
// Check if emoji already exists
|
2024-06-13 06:52:01 +02:00
|
|
|
const existingEmoji = await Emoji.fromSql(
|
|
|
|
|
and(
|
|
|
|
|
eq(Emojis.shortcode, args.shortcode),
|
|
|
|
|
isNull(Emojis.instanceId),
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-05-08 02:10:14 +02:00
|
|
|
|
|
|
|
|
if (existingEmoji) {
|
|
|
|
|
this.log(
|
|
|
|
|
`${chalk.red("✗")} Emoji with shortcode ${chalk.red(
|
|
|
|
|
args.shortcode,
|
|
|
|
|
)} already exists`,
|
|
|
|
|
);
|
|
|
|
|
this.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 04:07:33 +02:00
|
|
|
let file: File | null = null;
|
2024-05-08 09:26:17 +02:00
|
|
|
|
2024-05-09 04:07:33 +02:00
|
|
|
if (URL.canParse(args.file)) {
|
|
|
|
|
const spinner = ora(
|
|
|
|
|
`Downloading emoji from ${chalk.blue(
|
|
|
|
|
chalk.underline(args.file),
|
2024-05-08 02:10:14 +02:00
|
|
|
)}`,
|
2024-05-09 04:07:33 +02:00
|
|
|
).start();
|
2024-05-08 02:10:14 +02:00
|
|
|
|
2024-05-09 04:07:33 +02:00
|
|
|
const response = await fetch(args.file, {
|
|
|
|
|
headers: {
|
|
|
|
|
"Accept-Encoding": "identity",
|
|
|
|
|
},
|
2024-08-26 18:15:14 +02:00
|
|
|
// @ts-expect-error Proxy is a Bun-specific feature
|
2024-06-26 05:13:40 +02:00
|
|
|
proxy: config.http.proxy.address,
|
2024-05-09 04:07:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
spinner.fail();
|
|
|
|
|
this.log(
|
|
|
|
|
`${chalk.red("✗")} Request returned status code ${chalk.red(
|
|
|
|
|
response.status,
|
|
|
|
|
)}`,
|
|
|
|
|
);
|
|
|
|
|
this.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const filename =
|
|
|
|
|
new URL(args.file).pathname.split("/").pop() ?? "emoji";
|
|
|
|
|
|
|
|
|
|
file = new File([await response.blob()], filename, {
|
|
|
|
|
type:
|
|
|
|
|
response.headers.get("Content-Type") ??
|
|
|
|
|
"application/octet-stream",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
spinner.succeed();
|
|
|
|
|
} else {
|
|
|
|
|
const bunFile = Bun.file(args.file);
|
|
|
|
|
file = new File(
|
|
|
|
|
[await bunFile.arrayBuffer()],
|
|
|
|
|
args.file.split("/").pop() ?? "emoji",
|
|
|
|
|
{
|
|
|
|
|
type: bunFile.type,
|
|
|
|
|
},
|
2024-05-08 02:10:14 +02:00
|
|
|
);
|
2024-05-09 04:07:33 +02:00
|
|
|
}
|
2024-05-08 02:10:14 +02:00
|
|
|
|
2024-05-09 04:07:33 +02:00
|
|
|
const spinner = ora("Uploading emoji").start();
|
2024-05-08 02:10:14 +02:00
|
|
|
|
2025-01-28 17:43:43 +01:00
|
|
|
const media = await Media.fromFile(file);
|
2024-05-09 04:07:33 +02:00
|
|
|
|
|
|
|
|
spinner.succeed();
|
2024-05-08 02:10:14 +02:00
|
|
|
|
2024-06-13 06:52:01 +02:00
|
|
|
await Emoji.insert({
|
|
|
|
|
shortcode: args.shortcode,
|
2025-01-28 17:43:43 +01:00
|
|
|
mediaId: media.id,
|
2024-06-13 06:52:01 +02:00
|
|
|
visibleInPicker: true,
|
|
|
|
|
});
|
2024-05-09 04:07:33 +02:00
|
|
|
|
|
|
|
|
this.log(
|
|
|
|
|
`${chalk.green("✓")} Created emoji ${chalk.green(
|
|
|
|
|
args.shortcode,
|
2025-01-28 17:43:43 +01:00
|
|
|
)} with url ${chalk.blue(chalk.underline(media.getUrl()))}`,
|
2024-05-09 04:07:33 +02:00
|
|
|
);
|
2024-05-08 02:10:14 +02:00
|
|
|
|
|
|
|
|
this.exit(0);
|
|
|
|
|
}
|
|
|
|
|
}
|