bug fixes in /wiki

This commit is contained in:
Yusur 2025-01-17 11:22:08 +01:00
parent 4b8fa012da
commit d60d2cd477
6 changed files with 68 additions and 17 deletions

View file

@ -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. * 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`. * 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 ## License
The whole code is licensed under [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0.html) License. The whole code is licensed under [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0.html) License.

13
package-lock.json generated
View file

@ -9,6 +9,7 @@
"version": "0.1.0", "version": "0.1.0",
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"chalk": "^5.4.1",
"discord.js": "^14.17.3", "discord.js": "^14.17.3",
"dotenv": "^16.4.7", "dotenv": "^16.4.7",
"wikijs": "^6.4.1" "wikijs": "^6.4.1"
@ -727,6 +728,18 @@
"node": ">=4" "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": { "node_modules/create-require": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz",

View file

@ -11,6 +11,7 @@
"register": "tsx src/register.ts" "register": "tsx src/register.ts"
}, },
"dependencies": { "dependencies": {
"chalk": "^5.4.1",
"discord.js": "^14.17.3", "discord.js": "^14.17.3",
"dotenv": "^16.4.7", "dotenv": "^16.4.7",
"wikijs": "^6.4.1" "wikijs": "^6.4.1"

View file

@ -21,6 +21,8 @@ import { GatewayIntentBits, Events, Interaction, ChatInputCommandInteraction } f
import { MyClient } from './client'; import { MyClient } from './client';
import commandList from './commandList'; import commandList from './commandList';
import chalk from 'chalk';
const client = new MyClient({ const client = new MyClient({
intents: [ intents: [
GatewayIntentBits.Guilds GatewayIntentBits.Guilds
@ -33,17 +35,19 @@ for (let command of commandList) {
client.on(Events.InteractionCreate, async (interaction: Interaction) => { client.on(Events.InteractionCreate, async (interaction: Interaction) => {
if (interaction instanceof ChatInputCommandInteraction) { if (interaction instanceof ChatInputCommandInteraction) {
let command = client.commands.get(interaction.commandName); const { commandName } = interaction;
let command = client.commands.get(commandName);
try { try {
await command.execute(interaction); await command.execute(interaction);
} catch (ex) { } catch (ex) {
console.error(`${chalk.red('Error in command')} ${chalk.bold(`/${commandName}`)}`);
console.error(ex); console.error(ex);
} }
} }
}); });
client.once(Events.ClientReady, async () => { 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); client.login(process.env.TOKEN);

View file

@ -28,25 +28,46 @@ const data = new SlashCommandBuilder()
.setRequired(false) .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) { async function execute (interaction: ChatInputCommandInteraction) {
await interaction.deferReply(); await interaction.deferReply();
const siteChoice = interaction.options.getString('source') ?? 'auto'; let siteChoice = interaction.options.getString('source') ?? 'auto';
const { name: siteName, url: siteUrl } = pageSources[siteChoice]; const pageName = interaction.options.getString('p');
const mwClient = new MediaWikiClient(siteUrl); let pageData = null;
const pageData = await mwClient.getPage(interaction.options.getString('p')); if (siteChoice === 'auto') {
for (let site of pageSourcesAuto) {
pageData = await fetchPageFromSite(site, pageName);
if (pageData) break;
}
} else {
pageData = await fetchPageFromSite(siteChoice, pageName);
}
const pageEmbed = new EmbedBuilder() if (pageData) {
const pageEmbed = new EmbedBuilder()
.setTitle(pageData.title) .setTitle(pageData.title)
.setURL(pageData.url) .setURL(pageData.url)
.setDescription(pageData.content) .setDescription(pageData.content)
.setFooter({ .setFooter({
text: `Informazioni da ${siteName}` text: `Informazioni da ${pageData.origin}`
}); });
await interaction.followUp({ await interaction.followUp({
embeds: [pageEmbed] embeds: [pageEmbed]
}); });
} else {
await interaction.followUp({
content: `Pagina **${pageName}** non trovata`
});
}
} }
export default { data, execute }; export default { data, execute };

View file

@ -3,24 +3,27 @@ import wiki from 'wikijs';
export class MediaWikiClient { export class MediaWikiClient {
apiUrl: string 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.apiUrl = url.endsWith('api.php') ? url : url + '/w/api.php';
this.siteName = siteName;
} }
async getPage(title: string) { async getPage(title: string) {
const page = await wiki({ const wikiClient = wiki({
apiUrl: this.apiUrl, apiUrl: this.apiUrl,
origin: null origin: null
}).page(title); });
const page = await wikiClient.page(title);
const pageUrl = page.url(); const pageUrl = page.url();
const content = await page.content(); const content = await page.content();
return { return {
url: pageUrl, url: pageUrl,
title, title,
content: content.length > 4000? content.slice(0, 3999) + '\u2026': content content: content.length > 4000? content.slice(0, 3999) + '\u2026': content,
origin: this.siteName
} }
} }
} }