✨ added imagex to send images along messages
All checks were successful
Build OCI Images / build (push) Successful in 2m57s
All checks were successful
Build OCI Images / build (push) Successful in 2m57s
This commit is contained in:
parent
0e14cca2eb
commit
dc12389cf3
10 changed files with 1330 additions and 357 deletions
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
|
|
@ -4,5 +4,6 @@
|
|||
"editor.codeActionsOnSave": {
|
||||
"source.fixAll": "explicit",
|
||||
"source.organizeImports": "explicit"
|
||||
}
|
||||
},
|
||||
"typescript.tsdk": "node_modules\\typescript\\lib"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
"type": "module",
|
||||
"main": "./dist/main.js",
|
||||
"module": "./dist/main.js",
|
||||
"types": "./dist/main.d.ts",
|
||||
"exports": [
|
||||
"./dist/commands/subscribe.js",
|
||||
"./dist/commands/unsubscribe.js"
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@datamine/database": "workspace:*",
|
||||
"@datamine/imagex": "workspace:^",
|
||||
"@discordjs/rest": "2.2.0",
|
||||
"@hono/node-server": "1.10.0",
|
||||
"dotenv": "16.4.5",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import "dotenv/config";
|
||||
|
||||
import { database } from "@datamine/database";
|
||||
import { extract } from "@datamine/imagex";
|
||||
import { REST } from "@discordjs/rest";
|
||||
import { serve } from "@hono/node-server";
|
||||
import {
|
||||
|
|
@ -34,6 +35,8 @@ const dispatch = app
|
|||
),
|
||||
async (c) => {
|
||||
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();
|
||||
for (const server of servers) {
|
||||
try {
|
||||
|
|
@ -51,6 +54,19 @@ const dispatch = app
|
|||
} 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) {
|
||||
console.error(error);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,10 +6,7 @@
|
|||
},
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"workspaces": [
|
||||
"packages/*",
|
||||
"apps/*"
|
||||
],
|
||||
"workspaces": ["packages/*", "apps/*"],
|
||||
"devDependencies": {
|
||||
"@biomejs/biome": "1.6.4",
|
||||
"@datamine/config": "workspace:^",
|
||||
|
|
|
|||
4
packages/imagex/biome.json
Normal file
4
packages/imagex/biome.json
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"$schema": "https://biomejs.dev/schemas/1.6.4/schema.json",
|
||||
"extends": ["@datamine/config/biome"]
|
||||
}
|
||||
34
packages/imagex/package.json
Normal file
34
packages/imagex/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
24
packages/imagex/src/main.ts
Normal file
24
packages/imagex/src/main.ts
Normal 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;
|
||||
}
|
||||
5
packages/imagex/tsconfig.json
Normal file
5
packages/imagex/tsconfig.json
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"extends": "@datamine/config/typescript",
|
||||
"exclude": ["node_modules"],
|
||||
"include": ["src"]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue