Skip to content

Commit

Permalink
Merge pull request #198 from JasonKhew96/fix_search_opts
Browse files Browse the repository at this point in the history
fix: search options
  • Loading branch information
soruly authored Aug 19, 2024
2 parents d4257aa + f8a2e0a commit 4778a1d
Showing 1 changed file with 24 additions and 9 deletions.
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

0 comments on commit 4778a1d

Please sign in to comment.