Skip to content

Commit

Permalink
Rework Discord Webhooks and add OBS Save Source Screenshot
Browse files Browse the repository at this point in the history
Co-authored-by: Hannah_GBS <Hannah-GBS@users.noreply.github.com>
  • Loading branch information
Hannah-GBS and Hannah-GBS committed Sep 13, 2024
1 parent 8b5b3fe commit 9de4209
Show file tree
Hide file tree
Showing 3 changed files with 267 additions and 175 deletions.
58 changes: 27 additions & 31 deletions packages/packages/src/discord/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { createHTTPClient } from "../httpEndpoint";
import type { Account, BotAccount } from "./auth";
import { botProperty, defaultProperties } from "./resource";
import type { GUILD_MEMBER_SCHEMA, ROLE_SCHEMA, USER_SCHEMA } from "./schemas";
import { readBinaryFile } from "@tauri-apps/api/fs";

export type Requests = {
[_: `POST /channels/${string}/messages`]: any;
Expand Down Expand Up @@ -374,60 +375,55 @@ export function register(pkg: Package, { api }: Ctx, core: Core) {
content: io.dataInput({
id: "content",
name: "Message",
type: t.string(),
type: t.option(t.string()),
}),
username: io.dataInput({
id: "username",
name: "Username",
type: t.string(),
type: t.option(t.string()),
}),
avatarUrl: io.dataInput({
id: "avatarUrl",
name: "Avatar URL",
type: t.string(),
type: t.option(t.string()),
}),
tts: io.dataInput({
id: "tts",
name: "TTS",
type: t.bool(),
}),
// fileLocation: io.dataInput({
// id: "fileLocation",
// name: "File Location",
// type: types.option(types.string()),
// }),
fileLocation: io.dataInput({
id: "fileLocation",
name: "File Location",
type: t.option(t.string()),
}),
status: io.dataOutput({
id: "status",
name: "Status",
type: t.int(),
}),
}),
async run({ ctx, io }) {
const body: Record<string, string> = {};
if (ctx.getInput(io.content)) body.content = ctx.getInput(io.content);
if (ctx.getInput(io.avatarUrl))
body.avatar_url = ctx.getInput(io.avatarUrl);
if (ctx.getInput(io.username)) body.username = ctx.getInput(io.username);
if (ctx.getInput(io.tts)) body.tts = ctx.getInput(io.tts).toString();
// ctx.getInput<Option<string>>("content").map((v) => (body.content = v));
// ctx.getInput<Option<string>>("avatarUrl").map((v) => (body.avatar_url = v));
// ctx.getInput<Option<string>>("username").map((v) => (body.username = v));
// ctx.getInput<Option<boolean>>("tts").map((v) => (body.tts = v.toString()));
// await ctx.getInput<Option<string>>("fileLocation").mapAsync(async (v) => {
// body["file[0]"] = JSON.stringify({
// file: await fs.readBinaryFile(v),
// fileName: ctx
// .getInput<string>("fileLocation")
// .split(/[\/\\]/)
// .at(-1),
// });
// });

const formData = new FormData();

for (const [key, value] of Object.entries(body)) {
formData.set(key, value);
}
ctx.getInput(io.content).peek((v) => {
formData.set("content", v);
});
ctx.getInput(io.avatarUrl).peek((v) => {
formData.set("avatar_url", v);
});
ctx.getInput(io.username).peek((v) => {
formData.set("username", v);
});
formData.set("tts", ctx.getInput(io.tts).toString());

await ctx.getInput(io.fileLocation).peekAsync(async (v) => {
formData.set(
"files[0]",
new Blob([await readBinaryFile(v)]),
v.split(/[\/\\]/).at(-1),
);
});

const response = await core.fetch(ctx.getInput(io.webhookUrl), {
method: "POST",
Expand Down
99 changes: 85 additions & 14 deletions packages/packages/src/obs/requests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JSONEnum, jsToJSON, jsonToJS } from "@macrograph/json";
import { JSONEnum, jsToJSON, jsonToJS, toJSON } from "@macrograph/json";

Check warning on line 1 in packages/packages/src/obs/requests.ts

View workflow job for this annotation

GitHub Actions / Format & Lint (Biome)

This import is unused.
import { Maybe, None, type Option, Some } from "@macrograph/option";
import type {
CreateIOFn,
Expand Down Expand Up @@ -600,8 +600,6 @@ export function register(pkg: Package<EventTypes>, types: Types) {

//Missing GetSourceScreenshot as it has Base64-Encoded Screenshot data

//Missing SaveSourceScreenshot as it has Base64-Encoded Screenshot data

createOBSExecSchema({
name: "Get Group List",
createIO: ({ io }) =>
Expand Down Expand Up @@ -740,6 +738,63 @@ export function register(pkg: Package<EventTypes>, types: Types) {
.then((o) => o.unwrapOr([]));
}

function imageFormatListFactory(obs: Accessor<Option<OBSWebSocket>>) {
return async () => {
const o = await obs().mapAsync(async (obs) => {
const { supportedImageFormats } = await obs.call("GetVersion");

return supportedImageFormats;
});
return o.unwrapOr([]);
};
}

createOBSExecSchema({
name: "Save Source Screenshot",
createIO: ({ io, obs }) => ({
sourceName: io.dataInput({
id: "sourceName",
name: "Source Name",
type: t.string(),
fetchSuggestions: inputListSuggestionFactory(obs),
}),
imageFormat: io.dataInput({
id: "imageFormat",
name: "Image Format",
type: t.string(),
fetchSuggestions: imageFormatListFactory(obs),
}),
imageFilePath: io.dataInput({
id: "imageFilePath",
name: "Image File Path",
type: t.string(),
}),
imageWidth: io.dataInput({
id: "imageWidth",
name: "Image Width",
type: t.option(t.int()),
}),
imageHeight: io.dataInput({
id: "imageHeight",
name: "Image Height",
type: t.option(t.int()),
}),
}),
async run({ ctx, io, obs }) {
await obs.call("SaveSourceScreenshot", {
sourceName: ctx.getInput(io.sourceName),
imageFormat: ctx.getInput(io.imageFormat),
imageFilePath: ctx.getInput(io.imageFilePath),
imageWidth: ctx.getInput(io.imageWidth).isSome()
? ctx.getInput(io.imageWidth).unwrap()
: undefined,
imageHeight: ctx.getInput(io.imageHeight).isSome()
? ctx.getInput(io.imageHeight).unwrap()
: undefined,
});
},
});

createOBSExecSchema({
name: "Set Current Program Scene",
createIO: ({ io, obs }) =>
Expand Down Expand Up @@ -2180,11 +2235,11 @@ export function register(pkg: Package<EventTypes>, types: Types) {
sceneItemTransform: types.SceneItemTransform.create({
alignment: alignmentConversion(
sceneItemTransformObj.alignment as number,
types
types,
),
boundsAlignment: alignmentConversion(
sceneItemTransformObj.boundsAlignment as number,
types
types,
),
boundsHeight: sceneItemTransformObj.boundsHeight as number,
boundsType: types.BoundsType.variant(
Expand Down Expand Up @@ -2417,11 +2472,11 @@ export function register(pkg: Package<EventTypes>, types: Types) {
const transform = types.SceneItemTransform.create({
alignment: alignmentConversion(
sceneItemTransformObj.alignment as number,
types
types,
),
boundsAlignment: alignmentConversion(
sceneItemTransformObj.boundsAlignment as number,
types
types,
),
boundsHeight: sceneItemTransformObj.boundsHeight as number,
boundsType: types.BoundsType.variant(
Expand Down Expand Up @@ -2466,19 +2521,35 @@ export function register(pkg: Package<EventTypes>, types: Types) {
sceneItemTransform: io.dataInput({
id: "sceneItemTransform",
name: "Scene Item Transform",
type: t.map(t.enum(JSONEnum)),
type: t.struct(types.SceneItemTransformImport),
}),
}),
async run({ ctx, io, obs }) {
const data = ctx.getInput(io.sceneItemTransform);

const body = {} as any;

if (data.alignment.isSome())
body.alignment = alignmentConversion(
data.alignment.unwrap().variant,
types,
);
if (data.boundsAlignment.isSome())
body.boundsAlignment = alignmentConversion(
data.alignment.unwrap().variant,
types,
);

for (const [key, value] of Object.entries(data)) {
if (value.isSome()) {
if (!key.includes("alignment")) body[key] = value.unwrap();
}
}

await obs.call("SetSceneItemTransform", {
sceneName: ctx.getInput(io.sceneName),
sceneItemId: ctx.getInput(io.sceneItemId),
sceneItemTransform: jsonToJS({
variant: "Map",
data: {
value: ctx.getInput(io.sceneItemTransform),
},
}),
sceneItemTransform: body,
});
},
});
Expand Down
Loading

0 comments on commit 9de4209

Please sign in to comment.