added imagex to send images along messages
All checks were successful
Build OCI Images / build (push) Successful in 2m57s

This commit is contained in:
Rose 2024-09-11 13:19:35 -04:00
parent 0e14cca2eb
commit dc12389cf3
No known key found for this signature in database
10 changed files with 1330 additions and 357 deletions

View file

@ -4,5 +4,6 @@
"editor.codeActionsOnSave": { "editor.codeActionsOnSave": {
"source.fixAll": "explicit", "source.fixAll": "explicit",
"source.organizeImports": "explicit" "source.organizeImports": "explicit"
} },
"typescript.tsdk": "node_modules\\typescript\\lib"
} }

View file

@ -5,7 +5,6 @@
"type": "module", "type": "module",
"main": "./dist/main.js", "main": "./dist/main.js",
"module": "./dist/main.js", "module": "./dist/main.js",
"types": "./dist/main.d.ts",
"exports": [ "exports": [
"./dist/commands/subscribe.js", "./dist/commands/subscribe.js",
"./dist/commands/unsubscribe.js" "./dist/commands/unsubscribe.js"

View file

@ -27,6 +27,7 @@
}, },
"dependencies": { "dependencies": {
"@datamine/database": "workspace:*", "@datamine/database": "workspace:*",
"@datamine/imagex": "workspace:^",
"@discordjs/rest": "2.2.0", "@discordjs/rest": "2.2.0",
"@hono/node-server": "1.10.0", "@hono/node-server": "1.10.0",
"dotenv": "16.4.5", "dotenv": "16.4.5",

View file

@ -1,6 +1,7 @@
import "dotenv/config"; import "dotenv/config";
import { database } from "@datamine/database"; import { database } from "@datamine/database";
import { extract } from "@datamine/imagex";
import { REST } from "@discordjs/rest"; import { REST } from "@discordjs/rest";
import { serve } from "@hono/node-server"; import { serve } from "@hono/node-server";
import { import {
@ -34,6 +35,8 @@ const dispatch = app
), ),
async (c) => { async (c) => {
const body = await c.req.valid("json"); const body = await c.req.valid("json");
// biome-ignore lint/style/noNonNullAssertion: it will always have a description and I am sure about this :3
const images = extract(body.description!);
const servers = await database.query.servers.findMany(); const servers = await database.query.servers.findMany();
for (const server of servers) { for (const server of servers) {
try { try {
@ -51,6 +54,19 @@ const dispatch = app
} satisfies RESTPostAPIChannelMessageJSONBody, } satisfies RESTPostAPIChannelMessageJSONBody,
}, },
); );
if (images.length > 0) {
await rest.post(
Routes.channelMessages(`${BigInt(server.channel)}`),
{
body: {
content: images.join("\n"),
allowed_mentions: {
parse: [AllowedMentionsTypes.Role],
},
} satisfies RESTPostAPIChannelMessageJSONBody,
},
);
}
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }

View file

@ -6,10 +6,7 @@
}, },
"type": "module", "type": "module",
"private": true, "private": true,
"workspaces": [ "workspaces": ["packages/*", "apps/*"],
"packages/*",
"apps/*"
],
"devDependencies": { "devDependencies": {
"@biomejs/biome": "1.6.4", "@biomejs/biome": "1.6.4",
"@datamine/config": "workspace:^", "@datamine/config": "workspace:^",

View file

@ -0,0 +1,4 @@
{
"$schema": "https://biomejs.dev/schemas/1.6.4/schema.json",
"extends": ["@datamine/config/biome"]
}

View file

@ -0,0 +1,34 @@
{
"name": "@datamine/imagex",
"packageManager": "yarn@4.1.1",
"private": true,
"type": "module",
"main": "./dist/main.js",
"module": "./dist/main.js",
"types": "./dist/main.d.ts",
"exports": {
"import": {
"types": "./dist/main.d.ts",
"default": "./dist/main.js"
}
},
"devDependencies": {
"@biomejs/biome": "1.6.4",
"@datamine/config": "workspace:*",
"@types/node": "20.12.7",
"pkgroll": "2.0.2",
"tsx": "4.7.2",
"typescript": "5.4.5"
},
"scripts": {
"lint": "biome ci ./src/**/*",
"build": "pkgroll"
},
"dependencies": {
"rehype-parse": "^9.0.0",
"remark-gfm": "^4.0.0",
"remark-parse": "^11.0.0",
"unified": "^11.0.5",
"unist-util-visit": "^5.0.0"
}
}

View file

@ -0,0 +1,24 @@
import rehypeParse from "rehype-parse";
import remarkGfm from "remark-gfm";
import remarkParse from "remark-parse";
import { unified } from "unified";
import { visit } from "unist-util-visit";
export function extract(md: string) {
const images: string[] = [];
const mdast = unified().use(remarkParse).use(remarkGfm).parse(md);
visit(mdast, "image", (node) => {
images.push(node.url);
});
visit(mdast, "html", (node) => {
const hast = unified()
.use(rehypeParse, { fragment: true })
.parse(node.value);
visit(hast, "element", (node) => {
if (node.tagName === "img") {
images.push(node.properties?.src as string);
}
});
});
return images;
}

View file

@ -0,0 +1,5 @@
{
"extends": "@datamine/config/typescript",
"exclude": ["node_modules"],
"include": ["src"]
}

1594
yarn.lock

File diff suppressed because it is too large Load diff