bug fixes in /wiki
This commit is contained in:
parent
4b8fa012da
commit
d60d2cd477
6 changed files with 68 additions and 17 deletions
|
|
@ -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.
|
||||
|
|
|
|||
13
package-lock.json
generated
13
package-lock.json
generated
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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'));
|
||||
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);
|
||||
}
|
||||
|
||||
const pageEmbed = new EmbedBuilder()
|
||||
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