fix /wiki command
This commit is contained in:
parent
d60d2cd477
commit
f9d5d5dce8
2 changed files with 43 additions and 6 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import { ChatInputCommandInteraction, EmbedBuilder, SlashCommandBuilder } from "discord.js";
|
import { ChatInputCommandInteraction, EmbedBuilder, SlashCommandBuilder } from "discord.js";
|
||||||
import { MediaWikiClient } from '../mediawiki';
|
import { MediaWikiClient, SectionObject } from '../mediawiki';
|
||||||
|
|
||||||
const pageSources = {
|
const pageSources = {
|
||||||
'cittadeldank': {
|
'cittadeldank': {
|
||||||
|
|
@ -30,7 +30,7 @@ const data = new SlashCommandBuilder()
|
||||||
|
|
||||||
async function fetchPageFromSite(siteId: string, pageName: string) {
|
async function fetchPageFromSite(siteId: string, pageName: string) {
|
||||||
const { name: siteName, url: siteUrl } = pageSources[siteId];
|
const { name: siteName, url: siteUrl } = pageSources[siteId];
|
||||||
const mwClient = new MediaWikiClient(siteUrl);
|
const mwClient = new MediaWikiClient(siteUrl, siteName);
|
||||||
const pageData = await mwClient.getPage(pageName);
|
const pageData = await mwClient.getPage(pageName);
|
||||||
return pageData;
|
return pageData;
|
||||||
}
|
}
|
||||||
|
|
@ -41,6 +41,8 @@ async function execute (interaction: ChatInputCommandInteraction) {
|
||||||
let siteChoice = interaction.options.getString('source') ?? 'auto';
|
let siteChoice = interaction.options.getString('source') ?? 'auto';
|
||||||
const pageName = interaction.options.getString('p');
|
const pageName = interaction.options.getString('p');
|
||||||
let pageData = null;
|
let pageData = null;
|
||||||
|
|
||||||
|
|
||||||
if (siteChoice === 'auto') {
|
if (siteChoice === 'auto') {
|
||||||
for (let site of pageSourcesAuto) {
|
for (let site of pageSourcesAuto) {
|
||||||
pageData = await fetchPageFromSite(site, pageName);
|
pageData = await fetchPageFromSite(site, pageName);
|
||||||
|
|
@ -50,15 +52,20 @@ async function execute (interaction: ChatInputCommandInteraction) {
|
||||||
pageData = await fetchPageFromSite(siteChoice, pageName);
|
pageData = await fetchPageFromSite(siteChoice, pageName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (pageData) {
|
if (pageData) {
|
||||||
const pageEmbed = new EmbedBuilder()
|
const pageEmbed = new EmbedBuilder()
|
||||||
.setTitle(pageData.title)
|
.setTitle(pageData.title)
|
||||||
.setURL(pageData.url)
|
.setURL(pageData.url)
|
||||||
.setDescription(pageData.content)
|
.setDescription(pageData.summary)
|
||||||
.setFooter({
|
.setFooter({
|
||||||
text: `Informazioni da ${pageData.origin}`
|
text: `Informazioni da ${pageData.origin}`
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (pageData.image) {
|
||||||
|
pageEmbed.setThumbnail(pageData.image);
|
||||||
|
}
|
||||||
|
|
||||||
await interaction.followUp({
|
await interaction.followUp({
|
||||||
embeds: [pageEmbed]
|
embeds: [pageEmbed]
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,21 @@
|
||||||
|
|
||||||
import wiki from 'wikijs';
|
import wiki from 'wikijs';
|
||||||
|
|
||||||
|
// fix for wiki().page().content() because wrong typing
|
||||||
|
function strToObjArr(s: string | object[]): object[] {
|
||||||
|
if (typeof s === 'string') {
|
||||||
|
let obj = JSON.parse(s);
|
||||||
|
if (obj instanceof Array) return obj;
|
||||||
|
else return [obj];
|
||||||
|
} else {
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type SectionObject = {title: string, content: string};
|
||||||
|
|
||||||
|
type WithPageId = {pageId: any};
|
||||||
|
|
||||||
export class MediaWikiClient {
|
export class MediaWikiClient {
|
||||||
apiUrl: string
|
apiUrl: string
|
||||||
siteName: string | null
|
siteName: string | null
|
||||||
|
|
@ -10,20 +25,35 @@ export class MediaWikiClient {
|
||||||
this.siteName = siteName;
|
this.siteName = siteName;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getPage(title: string) {
|
async getPage(title: string, detail: boolean = false) {
|
||||||
const wikiClient = wiki({
|
const wikiClient = wiki({
|
||||||
apiUrl: this.apiUrl,
|
apiUrl: this.apiUrl,
|
||||||
origin: null
|
origin: null
|
||||||
});
|
});
|
||||||
const page = await wikiClient.page(title);
|
const page = await wikiClient.page(title);
|
||||||
|
const pageId = page.raw.pageid;
|
||||||
const pageUrl = page.url();
|
const pageUrl = page.url();
|
||||||
const content = await page.content();
|
const pageSummary = await page.summary();
|
||||||
|
const pageImageThumb = pageId? (await this.getPageThumbnailUrl(pageId) ?? null): null;
|
||||||
|
let pageDetail: object[] = [];
|
||||||
|
if (detail){
|
||||||
|
pageDetail = strToObjArr(await page.content());
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
url: pageUrl,
|
url: pageUrl,
|
||||||
title,
|
title,
|
||||||
content: content.length > 4000? content.slice(0, 3999) + '\u2026': content,
|
summary: pageSummary.length > 4000? pageSummary.slice(0, 3999) + '\u2026': pageSummary,
|
||||||
|
detail: detail? pageDetail.map((x: SectionObject) => ({title: x.title, content: (x.content?.slice(0, 1000))})).filter((x: SectionObject) => x.content.length > 0).slice(0, 20): null,
|
||||||
|
image: pageImageThumb,
|
||||||
origin: this.siteName
|
origin: this.siteName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getPageThumbnailUrl(pageId: number): Promise<string> {
|
||||||
|
const respRaw = await fetch (`${this.apiUrl}?format=json&action=query&redirects=1&pageids=${encodeURIComponent(pageId)}&piprop=thumbnail&prop=pageimages`);
|
||||||
|
const respJson = await respRaw.json();
|
||||||
|
const thumbnailUrl = (respJson?.query?.pages || [])[pageId]?.thumbnail?.source;
|
||||||
|
return thumbnailUrl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue