Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: search options #198

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,17 @@ const getAnilistInfo = (id) =>
return resolve((await response.json()).data.Media);
});

const submitSearch = (imageFileURL, message) =>
const submitSearch = (imageFileURL, opts) =>
new Promise(async (resolve, reject) => {
let trial = 5;
let response = null;
while (trial > 0 && (!response || response.status === 503 || response.status === 402)) {
trial--;
response = await fetch(
`https://api.trace.moe/search?${[
`uid=tg${message.from.id}`,
`uid=tg${opts.fromId}`,
`url=${encodeURIComponent(imageFileURL)}`,
messageIsNoCrop(message) ? "" : "cutBorders=1",
opts.noCrop ? "" : "cutBorders=1",
].join("&")}`,
TRACE_MOE_KEY ? { headers: { "x-trace-key": TRACE_MOE_KEY } } : {},
).catch((e) => {
Expand Down Expand Up @@ -275,6 +275,19 @@ const messageIsMentioningBot = (message) => {
return false;
};

const getSearchOpts = (message) => {
const opts = {
mute: false,
noCrop: false,
skip: false,
fromId: message.from.id,
};
if (messageIsMute(message)) opts.mute = true;
if (messageIsNoCrop(message)) opts.noCrop = true;
if (messageIsSkipPreview(message)) opts.skip = true;
return opts;
};

const messageIsMute = (message) => {
if (message.caption) return message.caption.toLowerCase().includes("mute");
return message.text?.toLowerCase().includes("mute");
Expand Down Expand Up @@ -326,6 +339,7 @@ const getImageFromMessage = async (message) => {
};

const privateMessageHandler = async (message) => {
const searchOpts = getSearchOpts(message);
const responding_msg = message.reply_to_message ? message.reply_to_message : message;
const imageURL = await getImageFromMessage(responding_msg);
if (!imageURL) {
Expand All @@ -337,12 +351,12 @@ const privateMessageHandler = async (message) => {
return await sendMessage(message.chat.id, "You can Send / Forward anime screenshots to me.");
}
setMessageReaction(message.chat.id, message.message_id, ["👌"]);
const result = await submitSearch(imageURL, responding_msg, message);
const result = await submitSearch(imageURL, searchOpts);
sendChatAction(message.chat.id, "typing");
setMessageReaction(message.chat.id, message.message_id, ["👍"]);

if (result.video && !messageIsSkipPreview(message)) {
const videoLink = messageIsMute(message) ? `${result.video}&mute` : result.video;
if (result.video && !searchOpts.skip) {
const videoLink = searchOpts.mute ? `${result.video}&mute` : result.video;
const video = await fetch(videoLink, { method: "HEAD" });
if (video.ok && video.headers.get("content-length") > 0) {
await sendVideo(message.chat.id, videoLink, {
Expand All @@ -361,6 +375,7 @@ const privateMessageHandler = async (message) => {
};

const groupMessageHandler = async (message) => {
const searchOpts = getSearchOpts(message);
const responding_msg = message.reply_to_message ? message.reply_to_message : message;
const imageURL = await getImageFromMessage(responding_msg);
if (!imageURL) {
Expand All @@ -378,7 +393,7 @@ const groupMessageHandler = async (message) => {
);
}
setMessageReaction(message.chat.id, message.message_id, ["👌"]);
const result = await submitSearch(imageURL, responding_msg, message);
const result = await submitSearch(imageURL, searchOpts);
sendChatAction(message.chat.id, "typing");
setMessageReaction(message.chat.id, message.message_id, ["👍"]);

Expand All @@ -393,8 +408,8 @@ const groupMessageHandler = async (message) => {
return;
}

if (result.video && !messageIsSkipPreview(message)) {
const videoLink = messageIsMute(message) ? `${result.video}&mute` : result.video;
if (result.video && !searchOpts.skip) {
const videoLink = searchOpts.mute ? `${result.video}&mute` : result.video;
const video = await fetch(videoLink, { method: "HEAD" });
if (video.ok && video.headers.get("content-length") > 0) {
await sendVideo(message.chat.id, videoLink, {
Expand Down