Compare commits
4 Commits
9ba3d81a21
...
ae431f6258
| Author | SHA1 | Date | |
|---|---|---|---|
| ae431f6258 | |||
| fbbbebaf2e | |||
| b1dfe93e22 | |||
| ae428d8415 |
+19
-15
@@ -2,7 +2,7 @@ import axios from "axios";
|
|||||||
import sharp from "sharp";
|
import sharp from "sharp";
|
||||||
import emojiRegex from "emoji-regex";
|
import emojiRegex from "emoji-regex";
|
||||||
|
|
||||||
import {createCanvas, GlobalFonts, type Image as CanvasImage, loadImage, SKRSContext2D} from "@napi-rs/canvas";
|
import {createCanvas, GlobalFonts, Image, type Image as CanvasImage, loadImage, SKRSContext2D} from "@napi-rs/canvas";
|
||||||
import {Message, MessageEntity, PhotoSize} from "typescript-telegram-bot-api";
|
import {Message, MessageEntity, PhotoSize} from "typescript-telegram-bot-api";
|
||||||
import {Command} from "../base/command";
|
import {Command} from "../base/command";
|
||||||
import {bot, botUser} from "../index";
|
import {bot, botUser} from "../index";
|
||||||
@@ -64,7 +64,7 @@ export class Quote extends Command {
|
|||||||
|
|
||||||
const quote = quoteRaw.length ? quoteRaw : "…";
|
const quote = quoteRaw.length ? quoteRaw : "…";
|
||||||
|
|
||||||
const entities = reply.entities ?? reply.caption_entities ?? [];
|
const entities = msg.quote ? msg.quote.entities : reply.entities ?? reply.caption_entities ?? [];
|
||||||
|
|
||||||
const png = await renderQuoteCard(msg, quote, reply, entities);
|
const png = await renderQuoteCard(msg, quote, reply, entities);
|
||||||
await bot.sendPhoto({
|
await bot.sendPhoto({
|
||||||
@@ -85,6 +85,13 @@ const emojiCache = new Map<string, CanvasImage>();
|
|||||||
const customEmojiCache = new Map<string, CanvasImage>();
|
const customEmojiCache = new Map<string, CanvasImage>();
|
||||||
|
|
||||||
function appleEmojiUrl(emoji: string): string {
|
function appleEmojiUrl(emoji: string): string {
|
||||||
|
const codePoints = [...emoji]
|
||||||
|
.map(char => char.codePointAt(0)!.toString(16))
|
||||||
|
.join("-");
|
||||||
|
return `https://cdn.jsdelivr.net/npm/emoji-datasource-apple@15.0.0/img/apple/64/${codePoints}.png`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function githubEmojiUrl(emoji: string): string {
|
||||||
const codePoints = [...emoji]
|
const codePoints = [...emoji]
|
||||||
.map(char => char.codePointAt(0)!.toString(16))
|
.map(char => char.codePointAt(0)!.toString(16))
|
||||||
.join("-");
|
.join("-");
|
||||||
@@ -97,27 +104,24 @@ function twemojiUrl(emoji: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadEmoji(emoji: string): Promise<CanvasImage> {
|
async function loadEmoji(emoji: string): Promise<CanvasImage> {
|
||||||
let url = appleEmojiUrl(emoji);
|
const downloadAndCache = async (url: string): Promise<Image> => {
|
||||||
let cached = emojiCache.get(url);
|
|
||||||
if (cached) return cached;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await axios.get<ArrayBuffer>(url, {responseType: "arraybuffer"});
|
const res = await axios.get<ArrayBuffer>(url, {responseType: "arraybuffer"});
|
||||||
const img = await loadImage(Buffer.from(res.data));
|
const img = await loadImage(Buffer.from(res.data));
|
||||||
emojiCache.set(url, img);
|
emojiCache.set(url, img);
|
||||||
return img;
|
return img;
|
||||||
} catch (e) {
|
};
|
||||||
logError(e);
|
|
||||||
|
|
||||||
url = twemojiUrl(emoji);
|
const checkIfCached = async (emoji: string, emojiToUrl: (emoji: string) => string): Promise<CanvasImage> => {
|
||||||
cached = emojiCache.get(url);
|
const url = emojiToUrl(emoji);
|
||||||
|
const cached = emojiCache.get(url);
|
||||||
if (cached) return cached;
|
if (cached) return cached;
|
||||||
|
return await downloadAndCache(emojiToUrl(emoji));
|
||||||
|
};
|
||||||
|
|
||||||
|
const sources = [appleEmojiUrl, githubEmojiUrl, twemojiUrl];
|
||||||
|
for (const source of sources) {
|
||||||
try {
|
try {
|
||||||
const res = await axios.get<ArrayBuffer>(url, {responseType: "arraybuffer"});
|
return await checkIfCached(emoji, source);
|
||||||
const img = await loadImage(Buffer.from(res.data));
|
|
||||||
emojiCache.set(url, img);
|
|
||||||
return img;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logError(e);
|
logError(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import {logError, oldReplyToMessage, randomValue} from "../util/utils";
|
|||||||
import {Environment} from "../common/environment";
|
import {Environment} from "../common/environment";
|
||||||
|
|
||||||
export class Test extends Command {
|
export class Test extends Command {
|
||||||
regexp = /^(test|тест|еуые|ntcn|инноке(нтий|ш|нтич))/i;
|
regexp = /^(test|тест|еуые|ntcn|инноке(нтий|ш|нтич))$/i;
|
||||||
title = "тест";
|
title = "тест";
|
||||||
description = "System functionality check";
|
description = "System functionality check";
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user