-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(random): add ResolveToHdAction
Adds a new action that allows users to upscale images to HD quality.
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import type { WAMessage, WASocket } from "@whiskeysockets/baileys"; | ||
import FormData from "form-data"; | ||
import got from "got"; | ||
import BaseMessageHandlerAction from "../../../foundation/actions/BaseMessageHandlerAction.js"; | ||
import { Queue } from "../../../services/queue.js"; | ||
import { withSign } from "../../../supports/flag.js"; | ||
import { | ||
downloadContentBufferFromMessage, | ||
getJid, | ||
getMessageFromViewOnce, | ||
} from "../../../supports/message.js"; | ||
import type { MessagePattern } from "../../../types/MessagePattern.js"; | ||
|
||
export default class extends BaseMessageHandlerAction { | ||
patterns(): MessagePattern { | ||
return [withSign("hd")]; | ||
} | ||
|
||
async process(socket: WASocket, _message: WAMessage): Promise<void> { | ||
this.reactToProcessing(socket, _message); | ||
|
||
const message = getMessageFromViewOnce(_message); | ||
|
||
let mediaKey: Uint8Array | null | undefined; | ||
let directPath: string | null | undefined; | ||
let url: string | null | undefined; | ||
|
||
let photoBuffer: Buffer; | ||
|
||
if ( | ||
message?.extendedTextMessage?.contextInfo?.quotedMessage?.imageMessage | ||
) { | ||
mediaKey = | ||
message!.extendedTextMessage!.contextInfo!.quotedMessage!.imageMessage! | ||
.mediaKey; | ||
directPath = | ||
message!.extendedTextMessage!.contextInfo!.quotedMessage!.imageMessage! | ||
.directPath; | ||
url = | ||
message!.extendedTextMessage!.contextInfo!.quotedMessage!.imageMessage! | ||
.url; | ||
} else if (message?.imageMessage) { | ||
mediaKey = message!.imageMessage!.mediaKey; | ||
directPath = message!.imageMessage!.directPath; | ||
url = message!.imageMessage!.url; | ||
} else { | ||
return; | ||
} | ||
|
||
photoBuffer = await downloadContentBufferFromMessage( | ||
{ directPath, mediaKey, url }, | ||
"image" | ||
); | ||
|
||
const data = new FormData(); | ||
data.append("image", photoBuffer, "image.jpg"); | ||
data.append("scale", "4"); | ||
|
||
const result = await got | ||
.post("https://api2.pixelcut.app/image/upscale/v1", { | ||
body: data, | ||
headers: { | ||
accept: "application/json", | ||
"user-agent": | ||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" + | ||
Date.now(), | ||
}, | ||
}) | ||
.json<{ result_url: string }>(); | ||
|
||
Queue.add(async () => { | ||
if (!result.result_url) { | ||
this.reactToFailed(socket, _message); | ||
return; | ||
} | ||
|
||
await socket.sendMessage( | ||
getJid(_message), | ||
{ | ||
image: { | ||
url: result.result_url, | ||
}, | ||
}, | ||
{ quoted: _message } | ||
); | ||
await this.reactToDone(socket, _message); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import InfoServerAction from "../../../actions/message/random/InfoServerAction.js"; | ||
import PingAction from "../../../actions/message/random/PingAction.js"; | ||
import ResolveToHdAction from "../../../actions/message/random/ResolveToHdAction.js"; | ||
import type BaseMessageHandlerAction from "../../../foundation/actions/BaseMessageHandlerAction.js"; | ||
|
||
export default <BaseMessageHandlerAction[]>[ | ||
new PingAction(), | ||
new InfoServerAction(), | ||
new ResolveToHdAction(), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters