I’m working on a Discord bot in JavaScript and I want to add a cool feature. The idea is to make the bot grab the first paragraph of a Wikipedia article when someone types a command followed by the article name. For example, if a user types ‘!wiki Python’, the bot would fetch and post the intro paragraph about Python from Wikipedia.
I’ve seen other bots do this before, but I’m not sure how to implement it. Can anyone point me in the right direction? I’m open to using other programming languages if JavaScript isn’t the best choice for this task.
Here’s a rough outline of what I’m thinking:
const discord = require('discord.js');
const bot = new discord.Client();
bot.on('message', async message => {
if (message.content.startsWith('!wiki')) {
const article = message.content.slice(6);
// How do I fetch the Wikipedia summary here?
// And then send it back to the channel?
}
});
bot.login('my_bot_token');
Any help or suggestions would be great! Thanks in advance.
hey there! i’ve done something similar before. you can use the ‘wikipedia’ npm package to fetch article summaries. install it with ‘npm install wikipedia’ and then use it like this:
const wiki = require('wikipedia');
const summary = await wiki.summary(article);
message.channel.send(summary.extract);
hope that helps! let me know if u need more info
I’ve implemented a similar feature in one of my projects. For optimal performance, I’d recommend using the MediaWiki API directly. It’s more flexible and doesn’t require additional dependencies. Here’s a basic implementation:
const fetch = require('node-fetch');
async function getWikiSummary(article) {
const url = `https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=${encodeURIComponent(article)}`;
const response = await fetch(url);
const data = await response.json();
const pages = data.query.pages;
const pageId = Object.keys(pages)[0];
return pages[pageId].extract;
}
Then in your message handler:
const summary = await getWikiSummary(article);
message.channel.send(summary.slice(0, 1500) + '...');
This approach gives you more control over the API calls and response handling.
I’ve tackled a similar project before, and I found that using the ‘axios’ library for HTTP requests and the Wikimedia API works really well. Here’s a quick rundown of how you could implement it:
First, install axios with ‘npm install axios’. Then, you can set up your function like this:
const axios = require('axios');
async function getWikiSummary(article) {
const url = `https://en.wikipedia.org/api/rest_v1/page/summary/${encodeURIComponent(article)}`;
try {
const response = await axios.get(url);
return response.data.extract;
} catch (error) {
console.error('Error fetching Wikipedia summary:', error);
return 'Sorry, I couldn\'t find a summary for that article.';
}
}
In your message handler, you’d use it like:
const summary = await getWikiSummary(article);
message.channel.send(summary);
This approach is pretty straightforward and gives you a clean summary without having to parse through a lot of data. Just remember to handle potential errors, as not all searches will return valid results.