-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
Submodule HanamiCard
added at
5fd188
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` } ] | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters