Skip to content

Instantly share code, notes, and snippets.

@micuit-cuit
Created March 23, 2023 15:13
Show Gist options
  • Save micuit-cuit/98520ea5235e50118d8efe528f02034a to your computer and use it in GitHub Desktop.
Save micuit-cuit/98520ea5235e50118d8efe528f02034a to your computer and use it in GitHub Desktop.
for chat gpt
//it is a module that is used to get wich api of vinted all a new item is in and then it will send a message to the channel that is set in the config.json file
//contien les fonction:
// - start()
// - restart()
// - stop()
const config = require('../config/config.json');
const vinted = require('vinted-api');
const fs = require('fs');
const { ActionRowBuilder, ButtonBuilder } = require('discord.js');
function code(client) {
for (let i = 0; i < config.chanelLink.length; i++) {
let chanel = config.chanelLink[i].salon;
vinted.search(config.chanelLink[i].lien).then((posts) => {
client.channels.fetch(chanel).then((channel) => {
console.log(`actualisation du salon ${channel.name}, ${posts.items.length} articles trouvés à ${new Date().toLocaleString()}`);
let item = posts.items
let items = []
let lastId = config.chanelLink[0].lastId ? config.chanelLink[0].lastId : 0;
for (let j = item.length - 1; j > 0; j--) {
let element = item[j];
if (element.id > lastId) {
lastId = element.id;
} else {
continue;
}
items.push({
title: element.title,
price: element.price,
size: element.size_title,
url: element.url,
image_url: element.photo.url,
total_item_price: element.total_item_price,
})
//create the embed
let embedDisplay = {
color: 0xF2B950,
title: element.title,
url: element.url,
fields: [
{
name: '> 📏 | **__Taille__ :**',
value: element.size_title
},
{
name: '> 💵 | **__Prix__ :**',
value: element.price + '€'
},
{
name: '> 💰 | **__Prix Total__ :**',
value: element.total_item_price + '€'
},
{
name: '> ⏰ | **__Temps__ :**',
value: `<t:${new Date().getTime()}:R>`
}
],
image: {
url: element.photo.url ? element.photo.url : '',
},
timestamp: new Date()
};
let row = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setStyle('Link')
.setURL(element.url)
.setLabel('Détails')
.setEmoji('🔍')
)
.addComponents(
new ButtonBuilder()
.setStyle('Link')
.setURL("https://www.vinted.fr/items/" + element.id + "/want_it/new?button_name=message&ch=wd&receiver_id=" + element.id)
.setLabel('Négocier')
.setEmoji('🤝')
)
let row2 = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setStyle('Link')
.setURL("https://www.vinted.fr/transaction/buy/new?source_screen=item&transaction[item_id]=" + element.id)
.setLabel(' Acheter ')
.setEmoji('💸')
)
try { channel.send({ embeds: [embedDisplay], components: [row, row2] }); } catch (err) { console.log(err); }
config.chanelLink[0].lastId = lastId;
fs.writeFile('./config/config.json', JSON.stringify(config, null, 4), (err) => { if (err) throw err; });
}
}
);
});
}
}
function sleep(sleep, client) {
var loop = setInterval(async function () {
//pour chaque salon dans le fichier config.json on va chercher les nouveaux produits et on les envoi dans le salon
code(client);
}
, sleep);
}
module.exports = {
start(client) {
//start a loop that will check every config.timeUpdate mn if there is a new item in the api
//start the loop
let time = config.chanelRefresh * 60000;
sleep(time, client);
},
restart(client) {
//stop the loop
clearInterval(loop);
//start the loop
let time = config.chanelRefresh * 60000;
sleep(time, client);
},
stop() {
//stop the loop
clearInterval(loop);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment