Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const wiki = require('wikijs').default();
- const {EmbedBuilder} = require('discord.js');
- async function wikipedia(interaction){
- if (interaction.commandName === 'wiki') {
- const searchQuery = interaction.options.get('search').value;
- interaction.deferReply();
- try {
- const search = await wiki.search(searchQuery);
- if (search.results.length === 0) {
- interaction.editReply("No Wikipedia page found for that query.");
- return;
- }
- const result = await wiki.page(search.results[0]);
- const summary = await result.summary();
- const imageUrl = await result.mainImage();
- if (summary.length > 2048) {
- const truncatedSummary = summary.slice(0, 2045);
- const lastPeriodIndex = truncatedSummary.lastIndexOf('.');
- const finalSummary = truncatedSummary.slice(0, lastPeriodIndex + 1) + '';
- const embed = new EmbedBuilder()
- .setColor('White')
- .setTitle(`Wikipedia search: ${result.raw.title}`)
- .setDescription(finalSummary)
- .setThumbnail(imageUrl)
- .addFields({ name: '\u200b', value: '\u200b' })
- .addFields({ name: 'Read More:', value: `[${result.raw.title}](${result.raw.fullurl})`});
- interaction.editReply({ embeds: [embed] });
- } else {
- const embed = new EmbedBuilder()
- .setColor('White')
- .setTitle(`Wikipedia search: ${result.raw.title}`)
- .setDescription(summary)
- .setThumbnail(imageUrl)
- .addFields({ name: '\u200b', value: '\u200b' })
- .addFields({ name: 'Read More:', value: `[${result.raw.title}](${result.raw.fullurl})`});
- interaction.editReply({ embeds: [embed] });
- }
- } catch (err){
- interaction.editReply(`An error occurred while searching.\n\n${err}`)
- }
- }
- }
- module.exports = wikipedia;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement