mirror of
https://github.com/FranP-code/shopping-discord-bot.git
synced 2025-10-13 00:22:44 +00:00
added customTextSuggest and some ajustments in the bot
This commit is contained in:
9
app.js
9
app.js
@@ -1,15 +1,18 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { Collection, Client, GatewayIntentBits, Events } = require('discord.js');
|
const { Collection, Client, GatewayIntentBits, Events } = require('discord.js');
|
||||||
|
const { getRandomElementFromArray } = require('./constants');
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
|
|
||||||
|
const enviroment = process.env.NODE_ENV;
|
||||||
|
|
||||||
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
|
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
|
||||||
|
|
||||||
client.on('ready', () => {
|
client.on('ready', () => {
|
||||||
console.log('working');
|
console.log('working');
|
||||||
});
|
});
|
||||||
|
|
||||||
client.login(process.env.BOT_TOKEN);
|
client.login(enviroment === 'production' ? process.env.PROD_BOT_TOKEN : process.env.DEV_BOT_TOKEN);
|
||||||
|
|
||||||
client.commands = new Collection();
|
client.commands = new Collection();
|
||||||
|
|
||||||
@@ -28,6 +31,10 @@ for (const file of commandFiles) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client.on(Events.InteractionCreate, async interaction => {
|
client.on(Events.InteractionCreate, async interaction => {
|
||||||
|
if (enviroment !== 'production' && interaction.user.id !== process.env.MY_DISCORD_USER_ID) {
|
||||||
|
interaction.reply(`The dev instance of this bot is only for ${getRandomElementFromArray([`<@${process.env.MY_DISCORD_USER_ID}>`, 'the king'])}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const command = interaction.client.commands.get(interaction.commandName);
|
const command = interaction.client.commands.get(interaction.commandName);
|
||||||
if (interaction.isChatInputCommand()) {
|
if (interaction.isChatInputCommand()) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
const { SlashCommandSubcommandBuilder, hyperlink, bold, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');
|
const { SlashCommandSubcommandBuilder, hyperlink, bold, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');
|
||||||
const puppeteer = require('puppeteer');
|
const puppeteer = require('puppeteer');
|
||||||
const jsdom = require('jsdom');
|
const jsdom = require('jsdom');
|
||||||
|
const { responses } = require('../constants');
|
||||||
|
|
||||||
function truncateText(text, max) {
|
function truncateText(text, max) {
|
||||||
return text.substr(0, max - 1).trim() + (text.length > 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 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;
|
const ELEMENTS_LIMIT = 3;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
@@ -146,7 +128,7 @@ module.exports = {
|
|||||||
const product = interaction.options.getString('product');
|
const product = interaction.options.getString('product');
|
||||||
const platform = interaction.options.getString('platform');
|
const platform = interaction.options.getString('platform');
|
||||||
if (platform && !pages.some(page => (page.name === platform))) {
|
if (platform && !pages.some(page => (page.name === platform))) {
|
||||||
await interaction.editReply(responses.missingPlatform[userLanguage]);
|
await interaction.editReply(responses(userLanguage).missingPlatform);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const country = interaction.options.getString('country') ||
|
const country = interaction.options.getString('country') ||
|
||||||
@@ -213,17 +195,17 @@ module.exports = {
|
|||||||
(new ActionRowBuilder()
|
(new ActionRowBuilder()
|
||||||
.addComponents(
|
.addComponents(
|
||||||
new ButtonBuilder()
|
new ButtonBuilder()
|
||||||
.setLabel(responses.platformInBrowser[userLanguage].replace('%P', page.name))
|
.setLabel(responses(userLanguage).platformInBrowser.replace('%P', page.name))
|
||||||
.setURL(page.searchUrl)
|
.setURL(page.searchUrl)
|
||||||
.setStyle(ButtonStyle.Link),
|
.setStyle(ButtonStyle.Link),
|
||||||
)),
|
)),
|
||||||
);
|
);
|
||||||
const replyTexts = [
|
const replyTexts = [
|
||||||
pagesScraped.length &&
|
pagesScraped.length &&
|
||||||
`${responses.extractedFrom[userLanguage]} ${pagesScraped.map(({ name }) => name).join(' ')}`,
|
`${responses(userLanguage).extractedFrom} ${pagesScraped.map(({ name }) => name).join(' ')}`,
|
||||||
`${productPrices.join('\n')}`,
|
`${productPrices.join('\n')}`,
|
||||||
pagesWithErrorScrapping.length &&
|
pagesWithErrorScrapping.length &&
|
||||||
`${responses.errorScrapping[userLanguage]} ${pagesWithErrorScrapping.map((name) => name).join(' ')}`,
|
`${responses(userLanguage).errorScrapping} ${pagesWithErrorScrapping.map((name) => name).join(' ')}`,
|
||||||
].filter(a => a);
|
].filter(a => a);
|
||||||
await interaction.editReply({ content: replyTexts.join('\n\n'), components: [...buttons] });
|
await interaction.editReply({ content: replyTexts.join('\n\n'), components: [...buttons] });
|
||||||
},
|
},
|
||||||
|
|||||||
41
commands/suggestCustomText.js
Normal file
41
commands/suggestCustomText.js
Normal 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);
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -1,18 +1,12 @@
|
|||||||
const { SlashCommandSubcommandBuilder } = require('discord.js');
|
const { SlashCommandSubcommandBuilder } = require('discord.js');
|
||||||
|
const { urlRegex, responses } = require('../constants');
|
||||||
require('dotenv').config();
|
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 = {
|
module.exports = {
|
||||||
data: new SlashCommandSubcommandBuilder()
|
data: new SlashCommandSubcommandBuilder()
|
||||||
.setName('suggest')
|
.setName('suggest-option')
|
||||||
.setNameLocalizations({
|
.setNameLocalizations({
|
||||||
'es-ES': 'sugerir',
|
'es-ES': 'sugerir-opción',
|
||||||
})
|
})
|
||||||
.setDescription('Suggest a new country and/or platform for add to the bot')
|
.setDescription('Suggest a new country and/or platform for add to the bot')
|
||||||
.setDescriptionLocalizations({
|
.setDescriptionLocalizations({
|
||||||
@@ -42,9 +36,16 @@ module.exports = {
|
|||||||
const country = interaction.options.getString('country');
|
const country = interaction.options.getString('country');
|
||||||
const platform = interaction.options.getString('platform');
|
const platform = interaction.options.getString('platform');
|
||||||
const suggestion = [country, platform].filter(a => a).join(' - ');
|
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);
|
const notificationsChannel = interaction.client.channels.cache.get(process.env.NOTIFICATION_DISCORD_CHANNEL_ID);
|
||||||
notificationsChannel.send(`SUGGESTION: ${suggestion}`);
|
notificationsChannel.send(`SUGGESTION of country: ${suggestion}`);
|
||||||
await interaction.reply(responses.suggestionSended[userLanguage]);
|
await interaction.reply(responses(userLanguage).suggestionSended);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
50
constants.js
Normal file
50
constants.js
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
require('dotenv').config();
|
||||||
|
|
||||||
|
const urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
|
||||||
|
function getRandomElementFromArray(arr) {
|
||||||
|
return arr[Math.floor((Math.random() * arr.length))];
|
||||||
|
}
|
||||||
|
const responsesTexts = {
|
||||||
|
suggestionSended: {
|
||||||
|
'en-US': `Suggestion sended to <@${process.env.MY_DISCORD_USER_ID}>`,
|
||||||
|
'es-ES': `Sugerencia enviada a <@${process.env.MY_DISCORD_USER_ID}>`,
|
||||||
|
},
|
||||||
|
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:',
|
||||||
|
},
|
||||||
|
notSuggest: {
|
||||||
|
'en-US': 'Please suggest someting :tired_face:',
|
||||||
|
'es-ES': 'Por favor, sugiere algo :tired_face:',
|
||||||
|
},
|
||||||
|
linksNotAllowed: {
|
||||||
|
'en-US': 'Links aren\'t allowed :/',
|
||||||
|
'es-ES': 'No esta permitido enviar links :/',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
function responses(userLanguage) {
|
||||||
|
const responsesEntries = Object.entries(responsesTexts);
|
||||||
|
const localizatedResponses = {};
|
||||||
|
responsesEntries.forEach(([responseName, responseTexts]) => {
|
||||||
|
localizatedResponses[responseName] = Object.entries(responseTexts).find(responseText => responseText[0] === userLanguage)[1];
|
||||||
|
});
|
||||||
|
return localizatedResponses;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
urlRegex,
|
||||||
|
getRandomElementFromArray,
|
||||||
|
responses,
|
||||||
|
};
|
||||||
@@ -2,6 +2,8 @@ const { REST, Routes } = require('discord.js');
|
|||||||
const fs = require('node:fs');
|
const fs = require('node:fs');
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
|
|
||||||
|
const enviroment = process.env.NODE_ENV;
|
||||||
|
|
||||||
const commands = [];
|
const commands = [];
|
||||||
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
|
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
|
||||||
|
|
||||||
@@ -10,15 +12,15 @@ for (const file of commandFiles) {
|
|||||||
commands.push(command.data.toJSON());
|
commands.push(command.data.toJSON());
|
||||||
}
|
}
|
||||||
|
|
||||||
const rest = new REST({ version: '10' }).setToken(process.env.BOT_TOKEN);
|
const rest = new REST({ version: '10' }).setToken(enviroment === 'production' ? process.env.PROD_BOT_TOKEN : process.env.DEV_BOT_TOKEN);
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
console.log(`Started refreshing ${commands.length} application (/) commands.`);
|
console.log(`Started refreshing ${commands.length} application (/) commands.`);
|
||||||
|
|
||||||
const data = await rest.put(
|
const data = await rest.put(
|
||||||
process.env.NODE_ENV === 'production' ? Routes.applicationCommands(process.env.DISCORD_CLIENT_ID) :
|
enviroment === 'production' ? Routes.applicationCommands(process.env.PROD_DISCORD_CLIENT_ID) :
|
||||||
Routes.applicationGuildCommands(process.env.DISCORD_CLIENT_ID, process.env.TEST_DISCORD_SERVER_ID),
|
Routes.applicationGuildCommands(process.env.DEV_DISCORD_CLIENT_ID, process.env.TEST_DISCORD_SERVER_ID),
|
||||||
{ body: commands },
|
{ body: commands },
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user