Skip to content

Commit

Permalink
add comomand
Browse files Browse the repository at this point in the history
  • Loading branch information
yorunoken committed Apr 24, 2024
1 parent df02b78 commit 57d6263
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 2 deletions.
1 change: 1 addition & 0 deletions HanamiCard
Submodule HanamiCard added at 5fd188
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
"private": true,
"name": "hanamibot",
"type": "module",
"main": "src/index.ts",
"scripts": {
"check": "tsc && eslint",
"start": "bun src/index.ts",
"start": "concurrently 'cd HanamiCard && git pull && go run main.go' 'bun .'",
"build": "bun scripts/build.ts",
"prod": "bun run build && bun run start"
},
"dependencies": {
"@lilybird/handlers": "0.4.0-beta.2",
"@lilybird/transformers": "0.2.0-alpha.1",
"concurrently": "^8.2.2",
"lilybird": "0.6.0-beta.3",
"osu-web.js": "^2.4.0",
"rosu-pp-js": "^1.0.2",
Expand Down
49 changes: 49 additions & 0 deletions src/commands-message/osu/card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { parseOsuArguments } from "@utils/args";
import { client } from "@utils/initalize";
import { UserType } from "@type/commandArgs";
import { EmbedBuilderType } from "@type/embedBuilders";
import { cardBuilder } from "@builders/card";
import { Mode } from "@type/osu";
import { EmbedType } from "lilybird";
import type { GuildTextChannel, Message } from "@lilybird/transformers";
import type { MessageCommand } from "@type/commands";

export default {
name: "card",
aliases: ["card", "cards", "stats"],
description: "Display card of a user.",
usage: "/card",
cooldown: 1000,
run
} satisfies MessageCommand;

async function run({ message, args, channel }: { message: Message, args: Array<string>, commandName: string, channel: GuildTextChannel }): Promise<void> {
const { user } = parseOsuArguments(message, args, <Mode>Mode.OSU);
if (user.type === UserType.FAIL) {
await channel.send(user.failMessage);
return;
}

const osuUserRequest = await client.safeParse(client.users.getUser(user.banchoId, { urlParams: { mode: user.mode } }));
if (!osuUserRequest.success) {
await channel.send({
embeds: [
{
type: EmbedType.Rich,
title: "Uh oh! :x:",
description: `It seems like the user **\`${user.banchoId}\`** doesn't exist! :(`
}
]
});
return;
}
const osuUser = osuUserRequest.data;

const options = await cardBuilder({
type: EmbedBuilderType.CARD,
initiatorId: message.author.id,
user: osuUser
});
await channel.send(options);
}

63 changes: 63 additions & 0 deletions src/commands/osu/card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { getCommandArgs } from "@utils/args";
import { client } from "@utils/initalize";
import { UserType } from "@type/commandArgs";
import { EmbedBuilderType } from "@type/embedBuilders";
import { cardBuilder } from "@builders/card";
import { ApplicationCommandOptionType, EmbedType } from "lilybird";
import type { ApplicationCommandData, GuildInteraction } from "@lilybird/transformers";
import type { SlashCommand } from "@type/commands";

export default {
data: {
name: "card",
description: "Display card of a user.",
options: [
{
type: ApplicationCommandOptionType.STRING,
name: "username",
description: "Specify an osu! username"
},
{
type: ApplicationCommandOptionType.USER,
name: "discord",
description: "Specify a linked Discord user"
}
]
},
run
} satisfies SlashCommand;

async function run(interaction: GuildInteraction<ApplicationCommandData>): Promise<void> {
await interaction.deferReply();

const { user } = getCommandArgs(interaction);

if (user.type === UserType.FAIL) {
await interaction.editReply(user.failMessage);
return;
}

const osuUserRequest = await client.safeParse(client.users.getUser(user.banchoId, { urlParams: { mode: user.mode } }));
if (!osuUserRequest.success) {
await interaction.editReply({
embeds: [
{
type: EmbedType.Rich,
title: "Uh oh! :x:",
description: `It seems like the user **\`${user.banchoId}\`** doesn't exist! :(`
}
]
});
return;
}

const osuUser = osuUserRequest.data;

const embeds = await cardBuilder({
type: EmbedBuilderType.CARD,
initiatorId: interaction.member.user.id,
user: osuUser
});

await interaction.editReply(embeds);
}
24 changes: 24 additions & 0 deletions src/embed-builders/card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { CardBuilderOptions } from "@type/embedBuilders";
import type { ReplyOptions } from "lilybird";

export async function cardBuilder({ user }: CardBuilderOptions): Promise<ReplyOptions> {
const { username, statistics } = user;
const params = `username=${username}&rank=${statistics.global_rank}&accuracy=${statistics
.hit_accuracy}&level=${`${statistics.level.current}.${statistics.level.progress}`}&avatar=${user.avatar_url}`;

const url = `http://localhost:8080/generateCard?${params}`;
const response = await fetch(url);

if (!response.ok)
throw new Error("Failed to generate card");

const responseData = await response.json() as { image: string };

const imageBlob = await fetch(`data:image/png;base64,${responseData.image}`).then(async (res) => res.blob());

return {
content: `User card for ${username}`,
// @ts-expect-error TypeScript thinks blob is incorrect type but it is.
files: [ { file: imageBlob, name: `${username}.png` } ]
};
}
1 change: 1 addition & 0 deletions src/embed-builders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./leaderboard";
export * from "./map";
export * from "./plays";
export * from "./profile";
export * from "./card";

// Simple functions
export * from "./simple/avatar";
Expand Down
9 changes: 8 additions & 1 deletion src/types/embedBuilders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const enum EmbedBuilderType {
AVATAR = "avatarBuilder",
BACKGROUND = "backgroundBuilder",
BANNER = "bannerBuilder",
CARD = "cardBuilder",
SIMULATE = "simulateBuilder"
}

Expand Down Expand Up @@ -77,6 +78,11 @@ export interface ProfileBuilderOptions extends BuilderOptions {
mode: Mode;
}

export interface CardBuilderOptions extends BuilderOptions {
type: EmbedBuilderType.CARD;
user: UserExtended;
}

export interface AvatarBuilderOptions extends BuilderOptions {
type: EmbedBuilderType.AVATAR;
user: UserExtended;
Expand All @@ -102,4 +108,5 @@ export type EmbedBuilderOptions =
| AvatarBuilderOptions
| BackgroundBuilderOptions
| BannerBuilderOptions
| SimulateBuilderOptions;
| SimulateBuilderOptions
| CardBuilderOptions;

0 comments on commit 57d6263

Please sign in to comment.