Skip to content

Commit 3723c64

Browse files
committed
added poli command and bug fixes
1 parent ac011b6 commit 3723c64

11 files changed

Lines changed: 233 additions & 240 deletions

File tree

src/commands/bible.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,24 @@ export default async function (msg: Message) {
2424
parameter = query.replace("--verse ", "").trim();
2525
}
2626

27-
try {
28-
const response = await axios.get(`https://labs.bible.org/api/`, {
29-
params: {
30-
passage: parameter,
31-
type: "json",
32-
},
33-
headers: {
34-
"User-Agent": "Mozilla/5.0",
35-
},
36-
});
37-
const data = response.data;
38-
39-
if (!Array.isArray(data) || data.length === 0) {
40-
await msg.reply("No verse found for your query.");
41-
return;
42-
}
43-
44-
const v = data[0];
45-
const verses = `${v.bookname} ${v.chapter}:${v.verse} - ${v.text.trim()}`;
46-
47-
await msg.reply(verses);
48-
} catch (error) {
49-
log.error("Command", "Error fetching bible:", error);
50-
await msg.reply("Failed to fetch bible.");
27+
const response = await axios.get(`https://labs.bible.org/api/`, {
28+
params: {
29+
passage: parameter,
30+
type: "json",
31+
},
32+
headers: {
33+
"User-Agent": "Mozilla/5.0",
34+
},
35+
});
36+
const data = response.data;
37+
38+
if (!Array.isArray(data) || data.length === 0) {
39+
await msg.reply("No verse found for your query.");
40+
return;
5141
}
42+
43+
const v = data[0];
44+
const verses = `${v.bookname} ${v.chapter}:${v.verse} - ${v.text.trim()}`;
45+
46+
await msg.reply(verses);
5247
}

src/commands/cmd.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,20 @@ export const command = "cmd";
66
export const role = "user";
77

88
export default async function (msg: Message) {
9-
try {
10-
const userCommands = Object.values(commands)
11-
.filter((cmd: any) => cmd.role === "user")
12-
.map((cmd: any) => cmd.command)
13-
.join(", ");
9+
const userCommands = Object.values(commands)
10+
.filter((cmd: any) => cmd.role === "user")
11+
.map((cmd: any) => cmd.command)
12+
.join(", ");
1413

15-
const adminCommands = Object.values(commands)
16-
.filter((cmd: any) => cmd.role === "admin")
17-
.map((cmd: any) => cmd.command)
18-
.join(", ");
14+
const adminCommands = Object.values(commands)
15+
.filter((cmd: any) => cmd.role === "admin")
16+
.map((cmd: any) => cmd.command)
17+
.join(", ");
1918

20-
let response = "*User:*\n" + userCommands;
21-
if (adminCommands) {
22-
response += "\n\n*Admin:*\n" + adminCommands;
23-
}
24-
25-
await msg.reply(response);
26-
} catch (error) {
27-
log.error("Command", "Error displaying commands:", error);
28-
await msg.reply("Failed to display commands.");
19+
let response = "*User:*\n" + userCommands;
20+
if (adminCommands) {
21+
response += "\n\n*Admin:*\n" + adminCommands;
2922
}
23+
24+
await msg.reply(response);
3025
}

src/commands/go.ts

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,41 @@ export default async function (msg: Message) {
1111
await msg.reply("Please provide a search query.");
1212
return;
1313
}
14-
15-
try {
16-
const response = await axios.get("https://api.duckduckgo.com/", {
17-
params: {
18-
q: query,
19-
format: "json",
20-
pretty: 1,
21-
no_redirect: 1,
22-
no_html: 1,
23-
},
24-
headers: {
25-
"User-Agent": "Mozilla/5.0",
26-
},
27-
});
28-
29-
const data = response.data;
30-
31-
if (data.AbstractText) {
32-
await msg.reply(`${data.AbstractText}\n\n${data.AbstractURL}`);
33-
return;
34-
}
3514

36-
// If no abstract, try to get the first related topic
37-
if (Array.isArray(data.RelatedTopics) && data.RelatedTopics.length > 0) {
38-
const firstTopic =
39-
data.RelatedTopics.find(
40-
(t: any) => typeof t.Text === "string" && t.FirstURL
41-
) || data.RelatedTopics[0];
42-
43-
if (firstTopic && firstTopic.Text && firstTopic.FirstURL) {
44-
await msg.reply(`${firstTopic.Text}\n${firstTopic.FirstURL}`);
45-
return;
46-
}
47-
}
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+
});
4827

49-
// Fallback
50-
const searchUrl = `https://duckduckgo.com/?q=${encodeURIComponent(query)}`;
51-
await msg.reply(`DuckDuckGo search results for "${query}":\n${searchUrl}`);
52-
} catch (error) {
53-
log.error("Command", "Error fetching DuckDuckGo:", error);
54-
await msg.reply("Failed to search DuckDuckGo.");
28+
const data = response.data;
29+
30+
if (data.AbstractText) {
31+
await msg.reply(`${data.AbstractText}\n\n${data.AbstractURL}`);
32+
return;
5533
}
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}`);
5651
}

src/commands/poli.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Message, MessageMedia } from "whatsapp-web.js";
2+
import axios from "axios";
3+
import log from "npmlog";
4+
import fs from "fs/promises";
5+
import { client } from "../index";
6+
7+
export const command = "poli";
8+
export const role = "user";
9+
10+
export default async function (msg: Message) {
11+
const query = msg.body.replace(/^poli\s+/i, "").trim();
12+
if (!query) {
13+
await msg.reply("Please provide a prompt.");
14+
return;
15+
}
16+
17+
const response = await axios.get(
18+
`https://image.pollinations.ai/prompt/${encodeURIComponent(query)}`,
19+
{ responseType: "arraybuffer" }
20+
);
21+
22+
const tempDir = "./.temp";
23+
await fs.mkdir(tempDir, { recursive: true });
24+
25+
const tempPath = `${tempDir}/${Date.now()}.png`;
26+
await fs.writeFile(tempPath, response.data);
27+
28+
const media = MessageMedia.fromFilePath(tempPath);
29+
await msg.reply(media);
30+
await fs.unlink(tempPath);
31+
}

src/commands/reload.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,28 @@ export const command = "reload";
99
export const role = "admin";
1010

1111
export default async function (msg: Message) {
12-
try {
13-
let count = 0;
14-
const commandsPath = path.join(__dirname, "..", "commands");
15-
fs.readdirSync(commandsPath).forEach((file) => {
16-
if (/\.js$|\.ts$/.test(file)) {
17-
const filePath = path.join(commandsPath, file);
18-
delete require.cache[require.resolve(filePath)];
19-
const commandModule = require(filePath);
12+
let count = 0;
13+
const commandsPath = path.join(__dirname, "..", "commands");
14+
fs.readdirSync(commandsPath).forEach((file) => {
15+
if (/\.js$|\.ts$/.test(file)) {
16+
const filePath = path.join(commandsPath, file);
17+
delete require.cache[require.resolve(filePath)];
18+
const commandModule = require(filePath);
2019

21-
if (
22-
typeof commandModule.command === "string" &&
23-
typeof commandModule.default === "function"
24-
) {
25-
commands[commandModule.command] = {
26-
command: commandModule.command,
27-
role: commandModule.role || "user",
28-
exec: commandModule.default,
29-
};
30-
count++;
31-
log.info("Loader", `Reloaded command: ${commandModule.command}`);
32-
}
20+
if (
21+
typeof commandModule.command === "string" &&
22+
typeof commandModule.default === "function"
23+
) {
24+
commands[commandModule.command] = {
25+
command: commandModule.command,
26+
role: commandModule.role || "user",
27+
exec: commandModule.default,
28+
};
29+
count++;
30+
log.info("Loader", `Reloaded command: ${commandModule.command}`);
3331
}
34-
});
32+
}
33+
});
3534

36-
await msg.reply(`Reloaded ${count} commands successfully.`);
37-
} catch (error) {
38-
log.error("Command", "Error occured while processing the request:", error);
39-
await msg.reply("An error occurred while processing your request.");
40-
}
35+
await msg.reply(`Reloaded ${count} commands successfully.`);
4136
}

src/commands/run.ts

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,62 @@ export const command = "run";
88
export const role = "admin";
99

1010
export default async function (msg: Message) {
11-
try {
12-
const body = msg.body.trim();
13-
const match = body.match(/^run\s+(python|java|c|js|php)\s*\r?\n([\s\S]+)/i);
11+
const body = msg.body.trim();
12+
const match = body.match(/^run\s+(python|java|c|js|php)\s*\r?\n([\s\S]+)/i);
1413

15-
log.info("Command", `Received body: ${JSON.stringify(body)}`);
16-
if (!match) {
17-
log.info("Command", "Invalid run command format");
18-
await msg.reply(
19-
"Please use the format:\n\nrun python\n<code>\n\nor\n\nrun java\n<code>"
20-
);
21-
return;
22-
}
14+
log.info("Command", `Received body: ${JSON.stringify(body)}`);
15+
if (!match) {
16+
log.info("Command", "Invalid run command format");
17+
await msg.reply(
18+
"Please use the format:\n\nrun python\n<code>\n\nor\n\nrun java\n<code>"
19+
);
20+
return;
21+
}
2322

24-
const lang = match[1].toLowerCase();
25-
const code = match[2];
23+
const lang = match[1].toLowerCase();
24+
const code = match[2];
2625

27-
let command: string;
28-
let tempFile: string;
26+
let command: string;
27+
let tempFile: string;
2928

30-
const tempDir = "./.temp";
31-
await fs.mkdir(tempDir, { recursive: true });
29+
const tempDir = "./.temp";
30+
await fs.mkdir(tempDir, { recursive: true });
3231

33-
if (lang === "python") {
34-
tempFile = `${tempDir}/run.py`;
35-
command = `python3 "${tempFile}"`;
36-
} else if (lang === "java") {
37-
tempFile = `${tempDir}/run.java`;
38-
command = `javac "${tempFile}" && java -cp /tmp Code`;
39-
} else if (lang === "c") {
40-
tempFile = `${tempDir}/run.c`;
41-
command = `gcc "${tempFile}" -o /tmp/code && /tmp/code`;
42-
} else if (lang === "js") {
43-
tempFile = `${tempDir}/code.js`;
44-
command = `node "${tempFile}"`;
45-
} else if (lang === "php") {
46-
tempFile = `${tempDir}/run.php`;
47-
command = `php "${tempFile}"`;
48-
} else {
49-
await msg.reply("Unsupported language.");
50-
return;
51-
}
32+
if (lang === "python") {
33+
tempFile = `${tempDir}/run.py`;
34+
command = `python3 "${tempFile}"`;
35+
} else if (lang === "java") {
36+
tempFile = `${tempDir}/run.java`;
37+
command = `javac "${tempFile}" && java -cp /tmp Code`;
38+
} else if (lang === "c") {
39+
tempFile = `${tempDir}/run.c`;
40+
command = `gcc "${tempFile}" -o /tmp/code && /tmp/code`;
41+
} else if (lang === "js") {
42+
tempFile = `${tempDir}/code.js`;
43+
command = `node "${tempFile}"`;
44+
} else if (lang === "php") {
45+
tempFile = `${tempDir}/run.php`;
46+
command = `php "${tempFile}"`;
47+
} else {
48+
await msg.reply("Unsupported language.");
49+
return;
50+
}
5251

53-
await fs.writeFile(tempFile, code);
52+
await fs.writeFile(tempFile, code);
5453

55-
const execPromise = util.promisify(exec);
54+
const execPromise = util.promisify(exec);
5655

57-
try {
58-
const { stdout, stderr } = await execPromise(command, {
59-
timeout: 10000,
60-
maxBuffer: 1024 * 1024,
61-
});
62-
let response = stdout || stderr || "No output.";
63-
if (response.length > 4000) {
64-
response = response.slice(0, 4000) + "\n\n[Output truncated]";
65-
}
66-
await msg.reply("```" + response + "```");
67-
} catch (err: any) {
68-
await msg.reply("Error executing code:\n" + (err.stderr || err.message));
56+
try {
57+
const { stdout, stderr } = await execPromise(command, {
58+
timeout: 10000,
59+
maxBuffer: 1024 * 1024,
60+
});
61+
let response = stdout || stderr || "No output.";
62+
if (response.length > 4000) {
63+
response = response.slice(0, 4000) + "\n\n[Output truncated]";
6964
}
70-
} catch (error) {
71-
log.error("Command", "Error occured while processing the request:", error);
72-
await msg.reply("An error occurred while processing your request.");
65+
await msg.reply("```" + response + "```");
66+
} catch (err: any) {
67+
await msg.reply("Error executing code:\n" + (err.stderr || err.message));
7368
}
7469
}

0 commit comments

Comments
 (0)