added customTextSuggest and some ajustments in the bot

This commit is contained in:
2022-11-14 21:00:10 -03:00
parent ee340cb9c5
commit 7126a76037
6 changed files with 122 additions and 39 deletions

View File

@@ -1,6 +1,7 @@
const { SlashCommandSubcommandBuilder, hyperlink, bold, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');
const puppeteer = require('puppeteer');
const jsdom = require('jsdom');
const { responses } = require('../constants');
function truncateText(text, max) {
return text.substr(0, max - 1).trim() + (text.length > max ? '...' : '');
@@ -62,25 +63,6 @@ const countryData = {
const pages = Object.values(countryData).map((country) => country.pages).flat(1);
const responses = {
'extractedFrom': {
'en-US': 'Prices extracted from:',
'es-ES': 'Precios extraídos de:',
},
'missingPlatform': {
'en-US': 'ERROR: Platform don\'t found!!',
'es-ES': 'ERROR: Plataforma no encontrada!!',
},
'platformInBrowser': {
'en-US': 'Search in %P on browser',
'es-ES': 'Buscar en %P en el buscador',
},
'errorScrapping': {
'en-US': 'No products could be found in:',
'es-ES': 'No se pudieron encontrar productos en:',
},
};
const ELEMENTS_LIMIT = 3;
module.exports = {
@@ -146,7 +128,7 @@ module.exports = {
const product = interaction.options.getString('product');
const platform = interaction.options.getString('platform');
if (platform && !pages.some(page => (page.name === platform))) {
await interaction.editReply(responses.missingPlatform[userLanguage]);
await interaction.editReply(responses(userLanguage).missingPlatform);
return;
}
const country = interaction.options.getString('country') ||
@@ -213,17 +195,17 @@ module.exports = {
(new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setLabel(responses.platformInBrowser[userLanguage].replace('%P', page.name))
.setLabel(responses(userLanguage).platformInBrowser.replace('%P', page.name))
.setURL(page.searchUrl)
.setStyle(ButtonStyle.Link),
)),
);
const replyTexts = [
pagesScraped.length &&
`${responses.extractedFrom[userLanguage]} ${pagesScraped.map(({ name }) => name).join(' ')}`,
`${responses(userLanguage).extractedFrom} ${pagesScraped.map(({ name }) => name).join(' ')}`,
`${productPrices.join('\n')}`,
pagesWithErrorScrapping.length &&
`${responses.errorScrapping[userLanguage]} ${pagesWithErrorScrapping.map((name) => name).join(' ')}`,
`${responses(userLanguage).errorScrapping} ${pagesWithErrorScrapping.map((name) => name).join(' ')}`,
].filter(a => a);
await interaction.editReply({ content: replyTexts.join('\n\n'), components: [...buttons] });
},

View File

@@ -0,0 +1,41 @@
const { SlashCommandSubcommandBuilder } = require('discord.js');
const { urlRegex, responses } = require('../constants');
require('dotenv').config();
module.exports = {
data: new SlashCommandSubcommandBuilder()
.setName('suggest-custom')
.setNameLocalizations({
'es-ES': 'sugerencia-custom',
})
.setDescription('Suggest a new feature or idea for the bot')
.setDescriptionLocalizations({
'es-ES': 'Sugiera una nueva funcionalidad o una idea para el bot',
})
.addStringOption(option => option
.setName('text')
.setNameLocalizations({
'es-ES': 'texto',
})
.setDescription('Add your suggest/idea')
.setDescriptionLocalizations({
'es-ES': 'Introduce tu sugerencia/idea',
})
.setMaxLength(1800)),
async execute(interaction) {
const userLanguage = interaction.locale || 'en-US';
const suggestion = interaction.options.getString('text');
if (!suggestion) {
interaction.reply(responses(userLanguage).notSuggest);
return;
}
if (urlRegex.test(suggestion)) {
interaction.reply(responses(userLanguage).linksNotAllowed);
return;
}
const notificationsChannel = interaction.client.channels.cache.get(process.env.NOTIFICATION_DISCORD_CHANNEL_ID);
notificationsChannel.send(`SUGGESTION: ${suggestion}`);
await interaction.reply(responses(userLanguage).suggestionSended);
},
};

View File

@@ -1,18 +1,12 @@
const { SlashCommandSubcommandBuilder } = require('discord.js');
const { urlRegex, responses } = require('../constants');
require('dotenv').config();
const responses = {
'suggestionSended': {
'en-US': `Suggestion sended to <@${process.env.MY_DISCORD_USER_ID}>`,
'es-ES': `Sugerencia enviada a <@${process.env.MY_DISCORD_USER_ID}>`,
},
};
module.exports = {
data: new SlashCommandSubcommandBuilder()
.setName('suggest')
.setName('suggest-option')
.setNameLocalizations({
'es-ES': 'sugerir',
'es-ES': 'sugerir-opción',
})
.setDescription('Suggest a new country and/or platform for add to the bot')
.setDescriptionLocalizations({
@@ -42,9 +36,16 @@ module.exports = {
const country = interaction.options.getString('country');
const platform = interaction.options.getString('platform');
const suggestion = [country, platform].filter(a => a).join(' - ');
if (!country && !platform) {
interaction.reply(responses(userLanguage).notSuggest);
return;
}
if (urlRegex.test(suggestion)) {
interaction.reply(responses(userLanguage).linksNotAllowed);
return;
}
const notificationsChannel = interaction.client.channels.cache.get(process.env.NOTIFICATION_DISCORD_CHANNEL_ID);
notificationsChannel.send(`SUGGESTION: ${suggestion}`);
await interaction.reply(responses.suggestionSended[userLanguage]);
notificationsChannel.send(`SUGGESTION of country: ${suggestion}`);
await interaction.reply(responses(userLanguage).suggestionSended);
},
};