Skip to content

Commit 136284d

Browse files
committed
Add wiki command to fetch summaries from Wikipedia and update environment variables
1 parent 2b069a6 commit 136284d

4 files changed

Lines changed: 51 additions & 23 deletions

File tree

.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
PROJECT_CANIS_ALIAS=Canis
22
DEBUG=true
33
COMMAND_PREFIX=!
4-
PORT=3000
4+
COMMAND_PREFIX_LESS=true
5+
PORT=3000
6+
AUTO_RELOAD=false

src/commands/go.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default async function (msg: Message) {
2828
const data = response.data;
2929

3030
if (data.AbstractText) {
31-
await msg.reply(`${data.AbstractText}\n${data.AbstractURL}`);
31+
await msg.reply(`${data.AbstractText}\n\n${data.AbstractURL}`);
3232
return;
3333
}
3434

@@ -49,7 +49,7 @@ export default async function (msg: Message) {
4949
const searchUrl = `https://duckduckgo.com/?q=${encodeURIComponent(query)}`;
5050
await msg.reply(`DuckDuckGo search results for "${query}":\n${searchUrl}`);
5151
} catch (error) {
52-
log.error("Command", "Error fetching DuckDuckGo results:", error);
52+
log.error("Command", "Error fetching DuckDuckGo:", error);
5353
await msg.reply("Failed to search DuckDuckGo.");
5454
}
5555
}

src/commands/wiki.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Message } from "whatsapp-web.js";
2+
import axios from "axios";
3+
import log from "npmlog";
4+
5+
export const command = "wiki";
6+
7+
export default async function (msg: Message) {
8+
const query = msg.body.replace(/^wiki\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(
16+
`https://en.wikipedia.org/api/rest_v1/page/summary/${encodeURIComponent(query)}`);
17+
18+
const data = response.data;
19+
const title = data.title || query;
20+
const description = data.description ? `(${data.description})` : "";
21+
const extract = data.extract || "No summary available.";
22+
23+
await msg.reply(`${title} ${description}\n${extract}`);
24+
} catch (error) {
25+
log.error("Command", "Error fetching wikipedia:", error);
26+
await msg.reply("Failed to fetch wikipedia.");
27+
}
28+
}

src/index.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import path from "path";
1010
import { startServer } from "./server";
1111

1212
const commandPrefix = process.env.COMMAND_PREFIX || "!";
13+
const commandPrefixLess = process.env.COMMAND_PREFIX_LESS === "true";
1314
const botName = process.env.PROJECT_CANIS_ALIAS || "Canis";
1415
const debug = process.env.DEBUG === "true";
16+
const autoReload = process.env.AUTO_RELOAD === "true";
1517
const port = process.env.PORT || 3000;
1618

1719
log.info("Bot", `Welcome to ${botName}!`);
@@ -36,20 +38,21 @@ const loadCommand = (file: string) => {
3638
log.info("Loader", `Loaded command: ${commandModule.command}`);
3739
}
3840
}
39-
}
41+
};
4042

4143
fs.readdirSync(commandsPath).forEach(loadCommand);
4244

4345
// Watch for changes
44-
fs.watch(commandsPath, (eventType, filename) => {
45-
if (filename && /\.js$|\.ts$/.test(filename)) {
46-
try {
47-
loadCommand(filename);
48-
} catch (err) {
49-
log.error("Loader", `Failed to reload command: ${filename}`, err);
46+
if (autoReload)
47+
fs.watch(commandsPath, (eventType, filename) => {
48+
if (filename && /\.js$|\.ts$/.test(filename)) {
49+
try {
50+
loadCommand(filename);
51+
} catch (err) {
52+
log.error("Loader", `Failed to reload command: ${filename}`, err);
53+
}
5054
}
51-
}
52-
});
55+
});
5356

5457
client.on("qr", (qr) => {
5558
// Generate and scan this code with your phone
@@ -72,25 +75,20 @@ client.initialize();
7275

7376
const messageEvent = (msg: Message) => {
7477
const prefix = !msg.body.startsWith(commandPrefix);
75-
if (prefix) return;
78+
if (!commandPrefixLess && prefix) return;
7679
if (msg.fromMe && prefix) return; // Ignore messages sent by the bot itself without prefix
7780

7881
const keyWithPrefix = msg.body.split(" ")[0];
7982
const key = keyWithPrefix.startsWith(commandPrefix)
8083
? keyWithPrefix.slice(commandPrefix.length)
8184
: keyWithPrefix;
8285
const handler = commands[key];
83-
84-
if (!handler && debug) {
85-
log.warn("Command", `No handler found for command: ${key}`);
86-
msg.reply(
87-
`Unknown command: ${key}. Type ${commandPrefix}help for a list of commands.`
88-
);
89-
return;
90-
}
91-
86+
87+
if (!handler) return;
88+
msg.body = msg.body.slice(keyWithPrefix.length).trim();
89+
9290
if (debug) log.info("Message", msg.body.slice(0, 255));
9391
handler(msg);
9492
};
9593

96-
export { commands }
94+
export { commands };

0 commit comments

Comments
 (0)