Skip to content

Commit 310fcda

Browse files
committed
Refactor command handlers to use async/await syntax for improved readability and error handling
1 parent b404f19 commit 310fcda

27 files changed

Lines changed: 351 additions & 535 deletions

src/commands/alert.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,20 @@ export default async function (msg: Message) {
1919
return;
2020
}
2121

22-
await axios
23-
.get(`https://api.popcat.xyz/alert?text=${encodeURIComponent(query)}`, {
22+
const response = await axios.get(
23+
`https://api.popcat.xyz/alert?text=${encodeURIComponent(query)}`,
24+
{
2425
responseType: "arraybuffer",
25-
})
26-
.then(async (response) => {
27-
const tempDir = "./.temp";
28-
await fs.mkdir(tempDir, { recursive: true });
26+
}
27+
);
28+
29+
const tempDir = "./.temp";
30+
await fs.mkdir(tempDir, { recursive: true });
2931

30-
const tempPath = `${tempDir}/${Date.now()}.png`;
31-
await fs.writeFile(tempPath, response.data);
32+
const tempPath = `${tempDir}/${Date.now()}.png`;
33+
await fs.writeFile(tempPath, response.data);
3234

33-
const media = MessageMedia.fromFilePath(tempPath);
34-
await msg.reply(media);
35-
await fs.unlink(tempPath);
36-
})
37-
.catch(async (error) => {
38-
log.error("alert", `Error fetching image: ${error.message}`);
39-
await msg.reply("Error fetching image. Please try again later.");
40-
});
35+
const media = MessageMedia.fromFilePath(tempPath);
36+
await msg.reply(media);
37+
await fs.unlink(tempPath);
4138
}

src/commands/bible.ts

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,29 @@ export default async function (msg: Message) {
3030
parameter = query.replace("--verse ", "").trim();
3131
}
3232

33-
await axios
34-
.get(`https://labs.bible.org/api/`, {
35-
params: {
36-
passage: parameter,
37-
type: "json",
38-
},
39-
headers: {
40-
"User-Agent": "Mozilla/5.0",
41-
},
42-
})
43-
.then(async (response) => {
44-
const data = response.data;
45-
46-
if (!Array.isArray(data) || data.length === 0) {
47-
await msg.reply("No verse found for your query.");
48-
return;
49-
}
33+
const response = await axios.get(`https://labs.bible.org/api/`, {
34+
params: {
35+
passage: parameter,
36+
type: "json",
37+
},
38+
headers: {
39+
"User-Agent": "Mozilla/5.0",
40+
},
41+
});
42+
43+
const data = response.data;
44+
45+
if (!Array.isArray(data) || data.length === 0) {
46+
await msg.reply("No verse found for your query.");
47+
return;
48+
}
5049

51-
const v = data[0];
52-
const verses = `
50+
const v = data[0];
51+
const verses = `
5352
*${v.bookname} ${v.chapter}:${v.verse}*
5453
5554
${v.text.trim()}
5655
`;
5756

58-
await msg.reply(verses);
59-
})
60-
.catch(async (error) => {
61-
log.error("bible", `Error fetching data: ${error.message}`);
62-
await msg.reply(`Error fetching data. Please try again later.`);
63-
return;
64-
});
57+
await msg.reply(verses);
6558
}

src/commands/block.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,4 @@ export default async function (msg: Message) {
4343
}
4444

4545
await msg.react("✅");
46-
return;
4746
}

src/commands/caution.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,20 @@ export default async function (msg: Message) {
2020
return;
2121
}
2222

23-
await axios
24-
.get(`https://api.popcat.xyz/caution?text=${encodeURIComponent(query)}`, {
23+
const response = await axios.get(
24+
`https://api.popcat.xyz/caution?text=${encodeURIComponent(query)}`,
25+
{
2526
responseType: "arraybuffer",
26-
})
27-
.then(async (response) => {
28-
const tempDir = "./.temp";
29-
await fs.mkdir(tempDir, { recursive: true });
27+
}
28+
);
29+
30+
const tempDir = "./.temp";
31+
await fs.mkdir(tempDir, { recursive: true });
3032

31-
const tempPath = `${tempDir}/${Date.now()}.png`;
32-
await fs.writeFile(tempPath, response.data);
33+
const tempPath = `${tempDir}/${Date.now()}.png`;
34+
await fs.writeFile(tempPath, response.data);
3335

34-
const media = MessageMedia.fromFilePath(tempPath);
35-
await msg.reply(media);
36-
await fs.unlink(tempPath);
37-
})
38-
.catch(async (error) => {
39-
log.error("caution", `Error fetching image: ${error.message}`);
40-
await msg.reply("Error fetching image. Please try again later.");
41-
});
36+
const media = MessageMedia.fromFilePath(tempPath);
37+
await msg.reply(media);
38+
await fs.unlink(tempPath);
4239
}

src/commands/docker.ts

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,34 @@ export default async function (msg: Message) {
2525
return;
2626
}
2727

28-
await axios
29-
.get(`https://hub.docker.com/v2/repositories/${query}`)
30-
.then(async (response) => {
31-
if (response.data.count === 0 || response.data.message) {
32-
await msg.reply(`No repositories found for "${query}".`);
33-
return;
34-
}
35-
if (!query.includes("/")) {
36-
// The response is a list of repositories for the user
37-
const repos = response.data.results;
38-
if (!repos || repos.length === 0) {
39-
await msg.reply(`No repositories found for user "${query}".`);
40-
return;
41-
}
42-
let reply = `*${query}*\n\n`;
43-
reply += repos
44-
.map(
45-
(repo: any) =>
46-
`\`${repo.name}\`
28+
const response = await axios.get(
29+
`https://hub.docker.com/v2/repositories/${query}`
30+
);
31+
32+
if (response.data.count === 0 || response.data.message) {
33+
await msg.reply(`No repositories found for "${query}".`);
34+
return;
35+
}
36+
if (!query.includes("/")) {
37+
// The response is a list of repositories for the user
38+
const repos = response.data.results;
39+
if (!repos || repos.length === 0) {
40+
await msg.reply(`No repositories found for user "${query}".`);
41+
return;
42+
}
43+
let reply = `*${query}*\n\n`;
44+
reply += repos
45+
.map(
46+
(repo: any) =>
47+
`\`${repo.name}\`
4748
\nStars: ${repo.star_count} Pulls: ${repo.pull_count}`
48-
)
49-
.join("\n\n");
50-
await msg.reply(reply);
51-
return;
52-
}
53-
const repo = response.data;
54-
const info = `
49+
)
50+
.join("\n\n");
51+
await msg.reply(reply);
52+
return;
53+
}
54+
const repo = response.data;
55+
const info = `
5556
\`${repo.name}\`
5657
${repo.description || ""}
5758
@@ -60,17 +61,5 @@ export default async function (msg: Message) {
6061
Last Updated: ${new Date(repo.last_updated).toLocaleDateString()}
6162
Link: https://hub.docker.com/r/${repo.namespace}/${repo.name}
6263
`;
63-
await msg.reply(info);
64-
})
65-
.catch(async (error) => {
66-
if (error.response && error.response.status === 404) {
67-
await msg.reply(`No hub/containers found for "${query}".`);
68-
return;
69-
}
70-
log.error("docker", `Error fetching data: ${error.message}`);
71-
await msg.reply(
72-
`Error fetching data for "${query}". Please try again later.`
73-
);
74-
return;
75-
});
64+
await msg.reply(info);
7665
}

src/commands/fbdl.ts

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,25 @@ export default async function (msg: Message) {
2727
return;
2828
}
2929

30-
getFbVideoInfo(query)
31-
.then(async (result) => {
32-
if (!result.url)
33-
return await msg.reply("No video found at the provided URL.");
34-
35-
await axios
36-
.get(result.hd, { responseType: "arraybuffer" })
37-
.then(async (response) => {
38-
const tempDir = "./.temp";
39-
await fs.mkdirSync(tempDir, { recursive: true });
40-
41-
const tempPath = `${tempDir}/fbdl_${Date.now()}.mp4`;
42-
await fs.writeFileSync(tempPath, response.data);
43-
44-
const audioBuffer = fs.readFileSync(tempPath);
45-
const media = new MessageMedia(
46-
"audio/mpeg",
47-
audioBuffer.toString("base64"),
48-
`${result.title}.mp4`
49-
);
50-
51-
await msg.reply(media, msg.from);
52-
await fs.promises.unlink(tempPath);
53-
})
54-
.catch(async (error) => {
55-
log.error("fbdl", `Error downloading video: ${error.message}`);
56-
await msg.reply("Error downloading video. Please try again later.");
57-
});
58-
})
59-
.catch(async (err) => {
60-
log.error("fbdl", `Error fetching video info: ${err.message}`);
61-
await msg.reply("Error fetching video info. Please try again later.");
62-
});
30+
const result = await getFbVideoInfo(query);
31+
if (!result.url)
32+
return await msg.reply("No video found at the provided URL.");
33+
34+
const response = await axios.get(result.hd, { responseType: "arraybuffer" });
35+
36+
const tempDir = "./.temp";
37+
await fs.mkdirSync(tempDir, { recursive: true });
38+
39+
const tempPath = `${tempDir}/fbdl_${Date.now()}.mp4`;
40+
await fs.writeFileSync(tempPath, response.data);
41+
42+
const audioBuffer = fs.readFileSync(tempPath);
43+
const media = new MessageMedia(
44+
"audio/mpeg",
45+
audioBuffer.toString("base64"),
46+
`${result.title}.mp4`
47+
);
48+
49+
await msg.reply(media, msg.from);
50+
await fs.promises.unlink(tempPath);
6351
}

src/commands/github.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ export default async function (msg: Message) {
2525
return;
2626
}
2727

28-
await axios
29-
.get(`https://api.github.com/users/${query}`)
30-
.then(async (response) => {
31-
const user = response.data;
32-
const info = `
28+
const response = await axios.get(`https://api.github.com/users/${query}`);
29+
30+
const user = response.data;
31+
const info = `
3332
\`${user.name || user.login}\
3433
${user.bio || ""}
3534
@@ -46,17 +45,5 @@ export default async function (msg: Message) {
4645
Link: ${user.blog || "N/A"}
4746
`;
4847

49-
await msg.reply(info);
50-
})
51-
.catch(async (error) => {
52-
if (error.response && error.response.status === 404) {
53-
await msg.reply(`No user found for "${query}".`);
54-
return;
55-
}
56-
log.error("github", `Error fetching data: ${error.message}`);
57-
await msg.reply(
58-
`Error fetching data for "${query}". Please try again later.`
59-
);
60-
return;
61-
});
48+
await msg.reply(info);
6249
}

src/commands/go.ts

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,53 +18,41 @@ export default async function (msg: Message) {
1818
return;
1919
}
2020

21-
await axios
22-
.get("https://api.duckduckgo.com/", {
23-
params: {
24-
q: query,
25-
format: "json",
26-
pretty: 1,
27-
no_redirect: 1,
28-
no_html: 1,
29-
},
30-
headers: {
31-
"User-Agent": "Mozilla/5.0",
32-
},
33-
})
34-
.then(async (response) => {
35-
const data = response.data;
21+
const response = await axios.get("https://api.duckduckgo.com/", {
22+
params: {
23+
q: query,
24+
format: "json",
25+
pretty: 1,
26+
no_redirect: 1,
27+
no_html: 1,
28+
},
29+
headers: {
30+
"User-Agent": "Mozilla/5.0",
31+
},
32+
});
3633

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

49-
if (firstTopic && firstTopic.Text && firstTopic.FirstURL) {
50-
await msg.reply(`${firstTopic.Text}\n${firstTopic.FirstURL}`);
51-
return;
52-
}
53-
}
40+
// If no abstract, try to get the first related topic
41+
if (Array.isArray(data.RelatedTopics) && data.RelatedTopics.length > 0) {
42+
const firstTopic =
43+
data.RelatedTopics.find(
44+
(t: any) => typeof t.Text === "string" && t.FirstURL
45+
) || data.RelatedTopics[0];
5446

55-
// Fallback
56-
const searchUrl = `https://duckduckgo.com/?q=${encodeURIComponent(
57-
query
58-
)}`;
59-
await msg.reply(
60-
`Why dont you duckduck it yourself? Heres the link: \n${searchUrl}`
61-
);
62-
})
63-
.catch(async (error) => {
64-
log.error("go", `Error fetching data: ${error.message}`);
65-
await msg.reply(
66-
`Error fetching data for "${query}". Please try again later.`
67-
);
47+
if (firstTopic && firstTopic.Text && firstTopic.FirstURL) {
48+
await msg.reply(`${firstTopic.Text}\n${firstTopic.FirstURL}`);
6849
return;
69-
});
50+
}
51+
}
52+
53+
// Fallback
54+
const searchUrl = `https://duckduckgo.com/?q=${encodeURIComponent(query)}`;
55+
await msg.reply(
56+
`Why dont you duckduck it yourself? Heres the link: \n${searchUrl}`
57+
);
7058
}

0 commit comments

Comments
 (0)