feat: lyrics, google and wikipedia command

This commit is contained in:
2023-02-23 21:22:20 +01:00
parent 594dd65adf
commit 1611b5e475
13 changed files with 531 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
import { commandModule, CommandType } from '@sern/handler';
import { publish } from '../../plugins/publish.js';
import { publish } from '#plugins';
/*
import { publish } from "#plugins";
import { ownerOnly } from "#plugins"

View File

@@ -1,5 +1,5 @@
import { commandModule, CommandType } from '@sern/handler';
import { publish } from '../../plugins/publish.js';
import { publish } from '#plugins';
import { ANIME } from '@consumet/extensions';
import {
ActionRowBuilder,

View File

@@ -1,5 +1,5 @@
import { commandModule, CommandType } from '@sern/handler';
import { publish } from '../../plugins/publish.js';
import { publish } from '#plugins';
import FormData from 'form-data';
import {
ActionRowBuilder,

View File

@@ -1,6 +1,6 @@
import { commandModule, CommandType } from '@sern/handler';
import { AttachmentBuilder } from 'discord.js';
import { publish } from '../../plugins/publish.js';
import { publish } from '#plugins';
import { Readable } from 'node:stream'
import { random } from '../../util/randomstring.js';
import fs from 'fs';

View File

@@ -11,8 +11,8 @@ import {
ModalSubmitInteraction,
ApplicationCommandOptionType,
} from 'discord.js';
import { publish } from '../../plugins/publish.js';
import { ownerOnly } from '../../plugins/ownerOnly.js';
import { publish } from '#plugins';
import { ownerOnly } from '#plugins';
import padyama from '../../schemas/padyama.js';
import { random } from '../../util/randomstring.js';

View File

@@ -1,6 +1,6 @@
import { commandModule, CommandType } from '@sern/handler'
import { ApplicationCommandOptionType, ColorResolvable, EmbedBuilder } from 'discord.js';
import { publish } from "../../plugins/publish.js";
import { publish } from "#plugins";
import fs from 'node:fs';
import mctags from '../../util/tags/minecraft.json' assert { type: 'json' }

34
commands/misc/google.ts Normal file
View File

@@ -0,0 +1,34 @@
import { commandModule, CommandType } from '@sern/handler'
import { publish } from "#plugins";
import google from 'googlethis'
import { ApplicationCommandOptionType } from 'discord.js';
/*
import { publish } from "#plugins";
import { ownerOnly } from "#plugins"
*/
export default commandModule({
type: CommandType.Slash,
plugins: [publish()],
description: 'Busca cosas en Google.',
//alias : [],
options: [
{
name: 'busqueda',
description: 'Escribe qué quieres buscar',
type: ApplicationCommandOptionType.String,
required: true,
}
],
execute: async (ctx, options) => {
await ctx.interaction.deferReply()
const prompt = options[1].getString('busqueda', true)
const search = await Promise.all((await google.search(prompt)).results.map(res => {
return `[${res.title}](<${res.url}>)`
}).slice(0, 5))
await ctx.interaction.editReply({
content: `Resultados para la búsqueda \`${prompt}\`:\n${search.join('\n')}`
})
},
});

60
commands/misc/letra.ts Normal file
View File

@@ -0,0 +1,60 @@
import { commandModule, CommandType } from '@sern/handler'
import { publish } from "#plugins";
import Genius from 'genius-lyrics'
import { ActionRowBuilder, ApplicationCommandOptionType, ButtonBuilder, ButtonStyle, EmbedBuilder } from 'discord.js';
const genius = new Genius.Client(process.env.GENIUS)
export default commandModule({
type: CommandType.Slash,
plugins: [publish()],
description: 'Busca la letra de una canción (Genius)',
//alias : [],
options: [
{
name: 'busqueda',
description: 'Qué buscar',
type: ApplicationCommandOptionType.String,
required: true,
autocomplete: true,
command: {
onEvent: [],
execute: async (ctx) => {
const input = ctx.options.getFocused();
const choices = (await genius.songs.search(input)).map(res => {
return `${res.title} - ${res.artist.name}|${res.id}`
})
await ctx.respond(
choices.map(choice => {
const [title, id] = choice.split('|')
return ({name: title, value: id})
})
)
}
}
}
],
execute: async (ctx, options) => {
await ctx.interaction.deferReply({ ephemeral: true })
const prompt = options[1].getString('busqueda', true)
const result = await genius.songs.get(Number(prompt))
const embed = new EmbedBuilder()
.setTitle(`${result.title} - ${result.artist.name}`)
.setColor('Random')
.setDescription((await result.lyrics()).slice(0, 3000))
const button = new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setLabel('URL de Genius')
.setStyle(ButtonStyle.Link)
.setURL(result.url)
)
await ctx.interaction.editReply({
embeds: [embed],
components: [button]
})
},
});

View File

@@ -0,0 +1,69 @@
import { commandModule, CommandType } from '@sern/handler'
import { publish } from "#plugins";
import { ApplicationCommandOptionType } from 'discord.js';
import { getWikipedia, searchWikipedia, SearchWikipediaObject } from '../../util/wikipedia.js';
export default commandModule({
type: CommandType.Slash,
plugins: [publish()],
description: 'Busca cosas por wikipedia',
//alias : [],
options: [
{
name: 'español',
description: 'Busca cosas por Wikipedia en español',
type: ApplicationCommandOptionType.Subcommand,
options: [
{
name: 'busqueda',
description: 'Escribe qué buscar.',
type: ApplicationCommandOptionType.String,
required: true,
autocomplete: true,
command: {
onEvent: [],
execute: async (ctx) => {
const search = await searchWikipedia('es', ctx)
await ctx.respond(
search.map(res => ({ name: res.title.toString(), value: res.pageid.toString() }))
)
}
}
}
]
},
{
name: 'ingles',
description: 'Busca cosas por Wikipedia en inglés',
type: ApplicationCommandOptionType.Subcommand,
options: [
{
name: 'search',
description: 'Escribe qué buscar.',
type: ApplicationCommandOptionType.String,
required: true,
autocomplete: true,
command: {
onEvent: [],
execute: async (ctx) => {
const search = await searchWikipedia('en', ctx)
await ctx.respond(
search.map(res => ({ name: res.title.toString(), value: res.pageid.toString() }))
)
}
}
}
]
}
],
execute: async (ctx, [, options]) => {
switch (options.getSubcommand()) {
case 'español': {
getWikipedia('es', ctx, options)
} break;
case 'ingles': {
getWikipedia('en', ctx, options)
} break;
}
},
});

View File

@@ -1,10 +1,6 @@
import { commandModule, CommandType } from '@sern/handler'
import { publish } from "../plugins/publish.js";
import { ownerOnly } from "../plugins/ownerOnly.js"
/*
import { publish } from "#plugins";
import { ownerOnly } from "#plugins"
*/
export default commandModule({
name: 'ping',

300
package-lock.json generated
View File

@@ -22,6 +22,8 @@
"execa": "^6.1.0",
"express": "^4.18.1",
"form-data": "^4.0.0",
"genius-lyrics": "^4.4.3",
"googlethis": "^1.7.1",
"got": "^12.5.3",
"libsodium-wrappers": "^0.7.10",
"mongoose": "^6.5.1",
@@ -1161,6 +1163,14 @@
"node": ">= 0.8"
}
},
"node_modules/entities": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/entities/-/entities-2.2.0.tgz",
"integrity": "sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==",
"funding": {
"url": "https://github.com/fb55/entities?sponsor=1"
}
},
"node_modules/escape-html": {
"version": "1.0.3",
"license": "MIT"
@@ -1461,6 +1471,15 @@
"resolved": "https://registry.npmjs.org/aproba/-/aproba-2.0.0.tgz",
"integrity": "sha512-lYe4Gx7QT+MKGbDsA+Z+he/Wtef0BiwDOlK/XkBrdfsh9J/jPPXbX0tE9x9cl27Tmu5gg3QUbUrQYa/y+KOHPQ=="
},
"node_modules/genius-lyrics": {
"version": "4.4.3",
"resolved": "https://registry.npmjs.org/genius-lyrics/-/genius-lyrics-4.4.3.tgz",
"integrity": "sha512-06L8GUg49FrUYEmSQvrSH74RH5S+qyerHwBpvk8vZLwWgpEw4mIWZDob5IpXT1ryhqazM9K6CXGNucKYPO8kng==",
"dependencies": {
"cheerio": "^1.0.0-rc.9",
"undici": "^5.8.2"
}
},
"node_modules/get-intrinsic": {
"version": "1.1.3",
"license": "MIT",
@@ -1502,6 +1521,152 @@
"url": "https://github.com/sponsors/isaacs"
}
},
"node_modules/googlethis": {
"version": "1.7.1",
"resolved": "https://registry.npmjs.org/googlethis/-/googlethis-1.7.1.tgz",
"integrity": "sha512-2rJWqcNQeY5/D3HUQn4aM9piru5LjPFG6TTumww5hRMm6tmdnHu/WwSXji6ueUbKVZUHsHQO8mICV7mJK9I7ug==",
"dependencies": {
"axios": "^0.21.1",
"cheerio": "1.0.0-rc.10",
"form-data": "^4.0.0",
"unraw": "^2.0.1"
},
"engines": {
"node": ">=14"
},
"funding": {
"url": "https://github.com/sponsors/LuanRT"
}
},
"node_modules/googlethis/node_modules/axios": {
"version": "0.21.4",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz",
"integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==",
"dependencies": {
"follow-redirects": "^1.14.0"
}
},
"node_modules/googlethis/node_modules/cheerio": {
"version": "1.0.0-rc.10",
"resolved": "https://registry.npmjs.org/cheerio/-/cheerio-1.0.0-rc.10.tgz",
"integrity": "sha512-g0J0q/O6mW8z5zxQ3A8E8J1hUgp4SMOvEoW/x84OwyHKe/Zccz83PVT4y5Crcr530FV6NgmKI1qvGTKVl9XXVw==",
"dependencies": {
"cheerio-select": "^1.5.0",
"dom-serializer": "^1.3.2",
"domhandler": "^4.2.0",
"htmlparser2": "^6.1.0",
"parse5": "^6.0.1",
"parse5-htmlparser2-tree-adapter": "^6.0.1",
"tslib": "^2.2.0"
},
"engines": {
"node": ">= 6"
},
"funding": {
"url": "https://github.com/cheeriojs/cheerio?sponsor=1"
}
},
"node_modules/googlethis/node_modules/cheerio-select": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/cheerio-select/-/cheerio-select-1.6.0.tgz",
"integrity": "sha512-eq0GdBvxVFbqWgmCm7M3XGs1I8oLy/nExUnh6oLqmBditPO9AqQJrkslDpMun/hZ0yyTs8L0m85OHp4ho6Qm9g==",
"dependencies": {
"css-select": "^4.3.0",
"css-what": "^6.0.1",
"domelementtype": "^2.2.0",
"domhandler": "^4.3.1",
"domutils": "^2.8.0"
},
"funding": {
"url": "https://github.com/sponsors/fb55"
}
},
"node_modules/googlethis/node_modules/css-select": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/css-select/-/css-select-4.3.0.tgz",
"integrity": "sha512-wPpOYtnsVontu2mODhA19JrqWxNsfdatRKd64kmpRbQgh1KtItko5sTnEpPdpSaJszTOhEMlF/RPz28qj4HqhQ==",
"dependencies": {
"boolbase": "^1.0.0",
"css-what": "^6.0.1",
"domhandler": "^4.3.1",
"domutils": "^2.8.0",
"nth-check": "^2.0.1"
},
"funding": {
"url": "https://github.com/sponsors/fb55"
}
},
"node_modules/googlethis/node_modules/dom-serializer": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-1.4.1.tgz",
"integrity": "sha512-VHwB3KfrcOOkelEG2ZOfxqLZdfkil8PtJi4P8N2MMXucZq2yLp75ClViUlOVwyoHEDjYU433Aq+5zWP61+RGag==",
"dependencies": {
"domelementtype": "^2.0.1",
"domhandler": "^4.2.0",
"entities": "^2.0.0"
},
"funding": {
"url": "https://github.com/cheeriojs/dom-serializer?sponsor=1"
}
},
"node_modules/googlethis/node_modules/domhandler": {
"version": "4.3.1",
"resolved": "https://registry.npmjs.org/domhandler/-/domhandler-4.3.1.tgz",
"integrity": "sha512-GrwoxYN+uWlzO8uhUXRl0P+kHE4GtVPfYzVLcUxPL7KNdHKj66vvlhiweIHqYYXWlw+T8iLMp42Lm67ghw4WMQ==",
"dependencies": {
"domelementtype": "^2.2.0"
},
"engines": {
"node": ">= 4"
},
"funding": {
"url": "https://github.com/fb55/domhandler?sponsor=1"
}
},
"node_modules/googlethis/node_modules/domutils": {
"version": "2.8.0",
"resolved": "https://registry.npmjs.org/domutils/-/domutils-2.8.0.tgz",
"integrity": "sha512-w96Cjofp72M5IIhpjgobBimYEfoPjx1Vx0BSX9P30WBdZW2WIKU0T1Bd0kz2eNZ9ikjKgHbEyKx8BB6H1L3h3A==",
"dependencies": {
"dom-serializer": "^1.0.1",
"domelementtype": "^2.2.0",
"domhandler": "^4.2.0"
},
"funding": {
"url": "https://github.com/fb55/domutils?sponsor=1"
}
},
"node_modules/googlethis/node_modules/htmlparser2": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-6.1.0.tgz",
"integrity": "sha512-gyyPk6rgonLFEDGoeRgQNaEUvdJ4ktTmmUh/h2t7s+M8oPpIPxgNACWa+6ESR57kXstwqPiCut0V8NRpcwgU7A==",
"funding": [
"https://github.com/fb55/htmlparser2?sponsor=1",
{
"type": "github",
"url": "https://github.com/sponsors/fb55"
}
],
"dependencies": {
"domelementtype": "^2.0.1",
"domhandler": "^4.0.0",
"domutils": "^2.5.2",
"entities": "^2.0.0"
}
},
"node_modules/googlethis/node_modules/parse5": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/parse5/-/parse5-6.0.1.tgz",
"integrity": "sha512-Ofn/CTFzRGTTxwpNEs9PP93gXShHcTq255nzRYSKe8AkVpZY7e1fpmTfOyoIvjP5HG7Z2ZM7VS9PPhQGW2pOpw=="
},
"node_modules/googlethis/node_modules/parse5-htmlparser2-tree-adapter": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/parse5-htmlparser2-tree-adapter/-/parse5-htmlparser2-tree-adapter-6.0.1.tgz",
"integrity": "sha512-qPuWvbLgvDGilKc5BoicRovlT4MtYT6JfJyBOMDsKoiT+GiuP5qyrPCnR9HcPECIJJmZh5jRndyNThnhhb/vlA==",
"dependencies": {
"parse5": "^6.0.1"
}
},
"node_modules/got": {
"version": "12.5.3",
"resolved": "https://registry.npmjs.org/got/-/got-12.5.3.tgz",
@@ -2965,6 +3130,11 @@
"node": ">= 0.8"
}
},
"node_modules/unraw": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/unraw/-/unraw-2.0.1.tgz",
"integrity": "sha512-tdOvLfRzHolwYcHS6HIX860MkK9LQ4+oLuNwFYL7bpgTEO64PZrcQxkisgwJYCfF8sKiWLwwu1c83DvMkbefIQ=="
},
"node_modules/util-deprecate": {
"version": "1.0.2",
"license": "MIT"
@@ -3837,6 +4007,11 @@
"encodeurl": {
"version": "1.0.2"
},
"entities": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/entities/-/entities-2.2.0.tgz",
"integrity": "sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A=="
},
"escape-html": {
"version": "1.0.3"
},
@@ -4063,6 +4238,15 @@
}
}
},
"genius-lyrics": {
"version": "4.4.3",
"resolved": "https://registry.npmjs.org/genius-lyrics/-/genius-lyrics-4.4.3.tgz",
"integrity": "sha512-06L8GUg49FrUYEmSQvrSH74RH5S+qyerHwBpvk8vZLwWgpEw4mIWZDob5IpXT1ryhqazM9K6CXGNucKYPO8kng==",
"requires": {
"cheerio": "^1.0.0-rc.9",
"undici": "^5.8.2"
}
},
"get-intrinsic": {
"version": "1.1.3",
"requires": {
@@ -4087,6 +4271,117 @@
"path-is-absolute": "^1.0.0"
}
},
"googlethis": {
"version": "1.7.1",
"resolved": "https://registry.npmjs.org/googlethis/-/googlethis-1.7.1.tgz",
"integrity": "sha512-2rJWqcNQeY5/D3HUQn4aM9piru5LjPFG6TTumww5hRMm6tmdnHu/WwSXji6ueUbKVZUHsHQO8mICV7mJK9I7ug==",
"requires": {
"axios": "^0.21.1",
"cheerio": "1.0.0-rc.10",
"form-data": "^4.0.0",
"unraw": "^2.0.1"
},
"dependencies": {
"axios": {
"version": "0.21.4",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz",
"integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==",
"requires": {
"follow-redirects": "^1.14.0"
}
},
"cheerio": {
"version": "1.0.0-rc.10",
"resolved": "https://registry.npmjs.org/cheerio/-/cheerio-1.0.0-rc.10.tgz",
"integrity": "sha512-g0J0q/O6mW8z5zxQ3A8E8J1hUgp4SMOvEoW/x84OwyHKe/Zccz83PVT4y5Crcr530FV6NgmKI1qvGTKVl9XXVw==",
"requires": {
"cheerio-select": "^1.5.0",
"dom-serializer": "^1.3.2",
"domhandler": "^4.2.0",
"htmlparser2": "^6.1.0",
"parse5": "^6.0.1",
"parse5-htmlparser2-tree-adapter": "^6.0.1",
"tslib": "^2.2.0"
}
},
"cheerio-select": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/cheerio-select/-/cheerio-select-1.6.0.tgz",
"integrity": "sha512-eq0GdBvxVFbqWgmCm7M3XGs1I8oLy/nExUnh6oLqmBditPO9AqQJrkslDpMun/hZ0yyTs8L0m85OHp4ho6Qm9g==",
"requires": {
"css-select": "^4.3.0",
"css-what": "^6.0.1",
"domelementtype": "^2.2.0",
"domhandler": "^4.3.1",
"domutils": "^2.8.0"
}
},
"css-select": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/css-select/-/css-select-4.3.0.tgz",
"integrity": "sha512-wPpOYtnsVontu2mODhA19JrqWxNsfdatRKd64kmpRbQgh1KtItko5sTnEpPdpSaJszTOhEMlF/RPz28qj4HqhQ==",
"requires": {
"boolbase": "^1.0.0",
"css-what": "^6.0.1",
"domhandler": "^4.3.1",
"domutils": "^2.8.0",
"nth-check": "^2.0.1"
}
},
"dom-serializer": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-1.4.1.tgz",
"integrity": "sha512-VHwB3KfrcOOkelEG2ZOfxqLZdfkil8PtJi4P8N2MMXucZq2yLp75ClViUlOVwyoHEDjYU433Aq+5zWP61+RGag==",
"requires": {
"domelementtype": "^2.0.1",
"domhandler": "^4.2.0",
"entities": "^2.0.0"
}
},
"domhandler": {
"version": "4.3.1",
"resolved": "https://registry.npmjs.org/domhandler/-/domhandler-4.3.1.tgz",
"integrity": "sha512-GrwoxYN+uWlzO8uhUXRl0P+kHE4GtVPfYzVLcUxPL7KNdHKj66vvlhiweIHqYYXWlw+T8iLMp42Lm67ghw4WMQ==",
"requires": {
"domelementtype": "^2.2.0"
}
},
"domutils": {
"version": "2.8.0",
"resolved": "https://registry.npmjs.org/domutils/-/domutils-2.8.0.tgz",
"integrity": "sha512-w96Cjofp72M5IIhpjgobBimYEfoPjx1Vx0BSX9P30WBdZW2WIKU0T1Bd0kz2eNZ9ikjKgHbEyKx8BB6H1L3h3A==",
"requires": {
"dom-serializer": "^1.0.1",
"domelementtype": "^2.2.0",
"domhandler": "^4.2.0"
}
},
"htmlparser2": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-6.1.0.tgz",
"integrity": "sha512-gyyPk6rgonLFEDGoeRgQNaEUvdJ4ktTmmUh/h2t7s+M8oPpIPxgNACWa+6ESR57kXstwqPiCut0V8NRpcwgU7A==",
"requires": {
"domelementtype": "^2.0.1",
"domhandler": "^4.0.0",
"domutils": "^2.5.2",
"entities": "^2.0.0"
}
},
"parse5": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/parse5/-/parse5-6.0.1.tgz",
"integrity": "sha512-Ofn/CTFzRGTTxwpNEs9PP93gXShHcTq255nzRYSKe8AkVpZY7e1fpmTfOyoIvjP5HG7Z2ZM7VS9PPhQGW2pOpw=="
},
"parse5-htmlparser2-tree-adapter": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/parse5-htmlparser2-tree-adapter/-/parse5-htmlparser2-tree-adapter-6.0.1.tgz",
"integrity": "sha512-qPuWvbLgvDGilKc5BoicRovlT4MtYT6JfJyBOMDsKoiT+GiuP5qyrPCnR9HcPECIJJmZh5jRndyNThnhhb/vlA==",
"requires": {
"parse5": "^6.0.1"
}
}
}
},
"got": {
"version": "12.5.3",
"resolved": "https://registry.npmjs.org/got/-/got-12.5.3.tgz",
@@ -5027,6 +5322,11 @@
"unpipe": {
"version": "1.0.0"
},
"unraw": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/unraw/-/unraw-2.0.1.tgz",
"integrity": "sha512-tdOvLfRzHolwYcHS6HIX860MkK9LQ4+oLuNwFYL7bpgTEO64PZrcQxkisgwJYCfF8sKiWLwwu1c83DvMkbefIQ=="
},
"util-deprecate": {
"version": "1.0.2"
},

View File

@@ -45,6 +45,8 @@
"execa": "^6.1.0",
"express": "^4.18.1",
"form-data": "^4.0.0",
"genius-lyrics": "^4.4.3",
"googlethis": "^1.7.1",
"got": "^12.5.3",
"libsodium-wrappers": "^0.7.10",
"mongoose": "^6.5.1",

59
util/wikipedia.ts Normal file
View File

@@ -0,0 +1,59 @@
import { ActionRow, ActionRowBuilder, AutocompleteInteraction, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js";
import axios from "axios";
import { Context, SlashOptions } from "@sern/handler";
/**
Search Wikipedia for a given input string in the specified language using the Wikipedia API.
@async
@function searchWikipedia
@param {string} lang - The language code for the Wikipedia language edition to search in.
@param {AutocompleteInteraction} autocomplete - The autocomplete interaction object containing the input to search for.
@returns {Promise<SearchWikipediaObject[]>} - A promise that resolves with an array of search results.
**/
export async function searchWikipedia(lang: string, autocomplete: AutocompleteInteraction) {
const input = autocomplete.options.getFocused()
if (!input) {
return [{ ns: 0, title: 'Empieza a escribir para buscar!', pageid: 0, size: 0, wordcount: 0, snippet: 0, timestamp: 0 }] as unknown as SearchWikipediaObject[]
}
const request = await axios.get(`https://${lang}.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch=${input}`)
return request.data.query.search as SearchWikipediaObject[]
}
export async function getWikipedia(lang: string, ctx: Context, options: SlashOptions) {
const pageid = options.getString((lang === 'es') ? 'busqueda' : 'search', true)
if (Number.isNaN(Number(pageid))) return ctx.reply({ content: 'Elige en el autocompletado el artículo.', ephemeral: true })
const request = await axios.get(`https://${lang}.wikipedia.org/w/api.php?action=query&prop=extracts&exintro&explaintext&pageids=${pageid}&format=json`)
const firstParagraph = (request.data.query.pages[pageid]).extract.split('\n\n')[0] as string
const title = (request.data.query.pages[pageid]).title as string
const canonicalArticleURL = await axios.get(`https://${lang}.wikipedia.org/w/api.php?action=query&prop=info&pageids=${pageid}&inprop=url&format=json`).then(res => {
return res.data.query.pages[pageid].canonicalurl as string
})
const embed = new EmbedBuilder()
.setTitle(title)
.setColor('Random')
.setAuthor({ name: ctx.user.username, iconURL: ctx.user.displayAvatarURL() })
.setDescription(`${firstParagraph.slice(0, 500)}...`)
.setFooter({ text: `Resultado de Wikipedia en ${(lang === 'es') ? 'español' : 'inglés'}`, iconURL: 'https://i.imgur.com/pcpfc8i.png' })
const button = new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setLabel('URL al artículo')
.setURL(canonicalArticleURL)
.setStyle(ButtonStyle.Link)
)
await ctx.reply({ embeds: [embed], components: [button] })
}
export interface SearchWikipediaObject {
ns: number
title: string
pageid: number
size: number
wordcount: number
snippet: string
timestamp: string
}