-
Notifications
You must be signed in to change notification settings - Fork 22
feat(badge): support emoji input #892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ import { | |
| IMessage, | ||
| LocalizeFunction, | ||
| SubCommand, | ||
| TextArgument, | ||
| } from "@statsify/discord"; | ||
| import { type Canvas, Image } from "skia-canvas"; | ||
| import { DemoProfile } from "./demo.profile.js"; | ||
|
|
@@ -41,7 +42,10 @@ export class BadgeCommand { | |
| description: (t) => t("commands.badge-set"), | ||
| tier: UserTier.GOLD, | ||
| preview: "badge.png", | ||
| args: [new FileArgument("badge", true)], | ||
| args: [ | ||
| new FileArgument("badge"), | ||
| new TextArgument("emoji", (t) => t("arguments.emoji"), false), | ||
| ], | ||
| }) | ||
| public set(context: CommandContext) { | ||
| return this.run(context, "set"); | ||
|
|
@@ -62,6 +66,7 @@ export class BadgeCommand { | |
| ): Promise<IMessage> { | ||
| const userId = context.getInteraction().getUserId(); | ||
| const file = context.option<APIAttachment | null>("badge"); | ||
| const emoji = context.option<string | null>("emoji"); | ||
| const user = context.getUser(); | ||
| const t = context.t(); | ||
|
|
||
|
|
@@ -82,41 +87,13 @@ export class BadgeCommand { | |
| } | ||
|
|
||
| case "set": { | ||
| if (!file) | ||
| if (!file && !emoji) | ||
| throw new ErrorMessage( | ||
| (t) => t("errors.unknown.title"), | ||
| (t) => t("errors.unknown.description") | ||
| ); | ||
|
|
||
| const canvas = createCanvas(32, 32); | ||
| const ctx = canvas.getContext("2d"); | ||
| ctx.imageSmoothingEnabled = false; | ||
|
|
||
| if (!["image/png", "image/jpeg", "image/gif"].includes(file.content_type ?? "")) | ||
| throw new ErrorMessage( | ||
| (t) => t("errors.unsupportedFileType.title"), | ||
| (t) => t("errors.unsupportedFileType.description") | ||
| ); | ||
|
|
||
| const badge = await loadImage(file.url); | ||
|
|
||
| const ratio = Math.min(canvas.width / badge.width, canvas.height / badge.height); | ||
| const scaled = badge.width > 32 || badge.height > 32; | ||
|
|
||
| const width = scaled ? badge.width * ratio : badge.width; | ||
| const height = scaled ? badge.height * ratio : badge.height; | ||
|
|
||
| ctx.drawImage( | ||
| badge, | ||
| 0, | ||
| 0, | ||
| badge.width, | ||
| badge.height, | ||
| (canvas.width - width) / 2, | ||
| (canvas.height - height) / 2, | ||
| width, | ||
| height | ||
| ); | ||
| const canvas = file ? await this.getBadgeCanvas(file) : await this.getEmojiCanvas(emoji as string); | ||
|
|
||
| await this.apiService.updateUserBadge(userId, await canvas.toBuffer("png")); | ||
| const profile = await this.getProfile(t, user, canvas); | ||
|
|
@@ -141,6 +118,61 @@ export class BadgeCommand { | |
| } | ||
| } | ||
|
|
||
| private async getBadgeCanvas(file: APIAttachment) { | ||
| const canvas = createCanvas(32, 32); | ||
| const ctx = canvas.getContext("2d"); | ||
| ctx.imageSmoothingEnabled = false; | ||
|
|
||
| if (!["image/png", "image/jpeg", "image/gif"].includes(file.content_type ?? "")) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where webp the goat? This should probably be abstracted away into a constant. |
||
| throw new ErrorMessage( | ||
| (t) => t("errors.unsupportedFileType.title"), | ||
| (t) => t("errors.unsupportedFileType.description") | ||
| ); | ||
|
|
||
| const badge = await loadImage(file.url); | ||
|
|
||
| const ratio = Math.min(canvas.width / badge.width, canvas.height / badge.height); | ||
| const scaled = badge.width > 32 || badge.height > 32; | ||
|
|
||
| const width = scaled ? badge.width * ratio : badge.width; | ||
| const height = scaled ? badge.height * ratio : badge.height; | ||
|
|
||
| ctx.drawImage( | ||
| badge, | ||
| 0, | ||
| 0, | ||
| badge.width, | ||
| badge.height, | ||
| (canvas.width - width) / 2, | ||
| (canvas.height - height) / 2, | ||
| width, | ||
| height | ||
| ); | ||
|
|
||
| return canvas; | ||
| } | ||
|
|
||
| private async getEmojiCanvas(input: string) { | ||
| const canvas = createCanvas(32, 32); | ||
| const ctx = canvas.getContext("2d"); | ||
| ctx.imageSmoothingEnabled = false; | ||
|
|
||
| const customEmoji = input.trim().match(/^<a?:\w+:(\d+)>$/); | ||
|
|
||
| if (customEmoji) { | ||
| const badge = await loadImage(`https://cdn.discordapp.com/emojis/${customEmoji[1]}.png?size=32&quality=lossless`); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should probably be handling if the bot can't load the emoji for whatever reason. |
||
| ctx.drawImage(badge, 0, 0, badge.width, badge.height, 0, 0, 32, 32); | ||
| return canvas; | ||
| } | ||
|
|
||
| ctx.font = "28px Apple Color Emoji, Segoe UI Emoji, Noto Color Emoji, sans-serif"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of the apple emoji. Can we get https://github.com/twitter/twemoji? |
||
| ctx.textAlign = "center"; | ||
| ctx.textBaseline = "middle"; | ||
| ctx.fillText(input.trim(), 16, 17); | ||
|
|
||
| return canvas; | ||
| } | ||
|
|
||
| private async getProfile(t: LocalizeFunction, user: User, badge?: Image | Canvas) { | ||
| if (!user?.uuid) throw new ErrorMessage("errors.unknown"); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just split files and emojis into two different subcommands, something along the lines of
/badge emojiand/badge image. I am not a fan of having two arguments that could potentially conflict.