Skip to content

Commit 5c3acd5

Browse files
committed
added go command
1 parent 37b718e commit 5c3acd5

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

src/commands/go.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Message } from "whatsapp-web.js";
2+
import axios from "axios";
3+
import log from "npmlog";
4+
5+
export const command = "go";
6+
7+
export default async function (msg: Message) {
8+
const query = msg.body.replace(/^go\s+/i, "").trim();
9+
if (!query) {
10+
await msg.reply("Please provide a search query.");
11+
return;
12+
}
13+
14+
try {
15+
const response = await axios.get("https://api.duckduckgo.com/", {
16+
params: {
17+
q: query,
18+
format: "json",
19+
pretty: 1,
20+
no_redirect: 1,
21+
no_html: 1,
22+
},
23+
headers: {
24+
"User-Agent": "Mozilla/5.0",
25+
},
26+
});
27+
28+
const data = response.data;
29+
30+
if (data.AbstractText) {
31+
await msg.reply(`${data.AbstractText}\n${data.AbstractURL}`);
32+
return;
33+
}
34+
35+
// If no abstract, try to get the first related topic
36+
if (Array.isArray(data.RelatedTopics) && data.RelatedTopics.length > 0) {
37+
const firstTopic =
38+
data.RelatedTopics.find(
39+
(t: any) => typeof t.Text === "string" && t.FirstURL
40+
) || data.RelatedTopics[0];
41+
42+
if (firstTopic && firstTopic.Text && firstTopic.FirstURL) {
43+
await msg.reply(`${firstTopic.Text}\n${firstTopic.FirstURL}`);
44+
return;
45+
}
46+
}
47+
48+
// Fallback
49+
const searchUrl = `https://duckduckgo.com/?q=${encodeURIComponent(query)}`;
50+
await msg.reply(`DuckDuckGo search results for "${query}":\n${searchUrl}`);
51+
} catch (error) {
52+
log.error("GoCommand", "Error fetching DuckDuckGo results:", error);
53+
await msg.reply("Failed to search DuckDuckGo.");
54+
}
55+
}

0 commit comments

Comments
 (0)