bug fixes in /wiki
This commit is contained in:
parent
4b8fa012da
commit
d60d2cd477
6 changed files with 68 additions and 17 deletions
|
|
@ -21,6 +21,8 @@ import { GatewayIntentBits, Events, Interaction, ChatInputCommandInteraction } f
|
|||
import { MyClient } from './client';
|
||||
import commandList from './commandList';
|
||||
|
||||
import chalk from 'chalk';
|
||||
|
||||
const client = new MyClient({
|
||||
intents: [
|
||||
GatewayIntentBits.Guilds
|
||||
|
|
@ -33,17 +35,19 @@ for (let command of commandList) {
|
|||
|
||||
client.on(Events.InteractionCreate, async (interaction: Interaction) => {
|
||||
if (interaction instanceof ChatInputCommandInteraction) {
|
||||
let command = client.commands.get(interaction.commandName);
|
||||
const { commandName } = interaction;
|
||||
let command = client.commands.get(commandName);
|
||||
try {
|
||||
await command.execute(interaction);
|
||||
} catch (ex) {
|
||||
console.error(`${chalk.red('Error in command')} ${chalk.bold(`/${commandName}`)}`);
|
||||
console.error(ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
client.once(Events.ClientReady, async () => {
|
||||
console.log(`Logged in as \x1b[1m${client.user.tag}\x1b[22m`);
|
||||
console.log(`Logged in as ${chalk.bold(client.user.tag)}`);
|
||||
})
|
||||
|
||||
client.login(process.env.TOKEN);
|
||||
|
|
@ -28,25 +28,46 @@ const data = new SlashCommandBuilder()
|
|||
.setRequired(false)
|
||||
);
|
||||
|
||||
async function fetchPageFromSite(siteId: string, pageName: string) {
|
||||
const { name: siteName, url: siteUrl } = pageSources[siteId];
|
||||
const mwClient = new MediaWikiClient(siteUrl);
|
||||
const pageData = await mwClient.getPage(pageName);
|
||||
return pageData;
|
||||
}
|
||||
|
||||
async function execute (interaction: ChatInputCommandInteraction) {
|
||||
await interaction.deferReply();
|
||||
|
||||
const siteChoice = interaction.options.getString('source') ?? 'auto';
|
||||
const { name: siteName, url: siteUrl } = pageSources[siteChoice];
|
||||
const mwClient = new MediaWikiClient(siteUrl);
|
||||
const pageData = await mwClient.getPage(interaction.options.getString('p'));
|
||||
|
||||
const pageEmbed = new EmbedBuilder()
|
||||
let siteChoice = interaction.options.getString('source') ?? 'auto';
|
||||
const pageName = interaction.options.getString('p');
|
||||
let pageData = null;
|
||||
if (siteChoice === 'auto') {
|
||||
for (let site of pageSourcesAuto) {
|
||||
pageData = await fetchPageFromSite(site, pageName);
|
||||
if (pageData) break;
|
||||
}
|
||||
} else {
|
||||
pageData = await fetchPageFromSite(siteChoice, pageName);
|
||||
}
|
||||
|
||||
if (pageData) {
|
||||
const pageEmbed = new EmbedBuilder()
|
||||
.setTitle(pageData.title)
|
||||
.setURL(pageData.url)
|
||||
.setDescription(pageData.content)
|
||||
.setFooter({
|
||||
text: `Informazioni da ${siteName}`
|
||||
text: `Informazioni da ${pageData.origin}`
|
||||
});
|
||||
|
||||
await interaction.followUp({
|
||||
embeds: [pageEmbed]
|
||||
});
|
||||
await interaction.followUp({
|
||||
embeds: [pageEmbed]
|
||||
});
|
||||
} else {
|
||||
await interaction.followUp({
|
||||
content: `Pagina **${pageName}** non trovata`
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default { data, execute };
|
||||
|
|
|
|||
|
|
@ -3,24 +3,27 @@ import wiki from 'wikijs';
|
|||
|
||||
export class MediaWikiClient {
|
||||
apiUrl: string
|
||||
siteName: string | null
|
||||
|
||||
constructor (url: string){
|
||||
constructor (url: string, siteName: string | null = null){
|
||||
this.apiUrl = url.endsWith('api.php') ? url : url + '/w/api.php';
|
||||
this.siteName = siteName;
|
||||
}
|
||||
|
||||
async getPage(title: string) {
|
||||
const page = await wiki({
|
||||
const wikiClient = wiki({
|
||||
apiUrl: this.apiUrl,
|
||||
origin: null
|
||||
}).page(title);
|
||||
|
||||
});
|
||||
const page = await wikiClient.page(title);
|
||||
const pageUrl = page.url();
|
||||
const content = await page.content();
|
||||
|
||||
return {
|
||||
url: pageUrl,
|
||||
title,
|
||||
content: content.length > 4000? content.slice(0, 3999) + '\u2026': content
|
||||
content: content.length > 4000? content.slice(0, 3999) + '\u2026': content,
|
||||
origin: this.siteName
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue