From d60d2cd4775004e32244fcdd12654c2c666d62da Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Fri, 17 Jan 2025 11:22:08 +0100 Subject: [PATCH] bug fixes in /wiki --- README.md | 9 +++++++++ package-lock.json | 13 +++++++++++++ package.json | 1 + src/bot.ts | 8 ++++++-- src/commands/wiki.ts | 41 +++++++++++++++++++++++++++++++---------- src/mediawiki.ts | 13 ++++++++----- 6 files changed, 68 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 30ac723..e779de5 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,15 @@ _TODO_ * Register commands with `npm run register`. Please note this is needed only when the command list is updated. If you do it every single time you start the bot you may get rate limited. * Start the bot with `npm start`. +### Running continuously + +You can run the bot in the background, with the benefit of running it at startup and have it restart on errors, with tools such as `pm2`. + +#### pm2 + +* `npm install -g pm2` +* `pm2 start npm -- start` + ## License The whole code is licensed under [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0.html) License. diff --git a/package-lock.json b/package-lock.json index 29f39a2..e0cbe59 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "license": "Apache-2.0", "dependencies": { + "chalk": "^5.4.1", "discord.js": "^14.17.3", "dotenv": "^16.4.7", "wikijs": "^6.4.1" @@ -727,6 +728,18 @@ "node": ">=4" } }, + "node_modules/chalk": { + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.4.1.tgz", + "integrity": "sha512-zgVZuo2WcZgfUEmsn6eO3kINexW8RAE4maiQ8QNs8CtpPCSyMiYsULR3HQYkm3w8FIA3SberyMJMSldGsW+U3w==", + "license": "MIT", + "engines": { + "node": "^12.17.0 || ^14.13 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, "node_modules/create-require": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", diff --git a/package.json b/package.json index 95b046f..75ea31e 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "register": "tsx src/register.ts" }, "dependencies": { + "chalk": "^5.4.1", "discord.js": "^14.17.3", "dotenv": "^16.4.7", "wikijs": "^6.4.1" diff --git a/src/bot.ts b/src/bot.ts index 5a1d187..7e0c946 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -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); \ No newline at end of file diff --git a/src/commands/wiki.ts b/src/commands/wiki.ts index 88d26bc..5c1ea9c 100644 --- a/src/commands/wiki.ts +++ b/src/commands/wiki.ts @@ -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 }; diff --git a/src/mediawiki.ts b/src/mediawiki.ts index 2595ac5..c415ea4 100644 --- a/src/mediawiki.ts +++ b/src/mediawiki.ts @@ -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 } } } \ No newline at end of file