107 lines
3.1 KiB
JavaScript
107 lines
3.1 KiB
JavaScript
const { Client, GatewayIntentBits, EmbedBuilder } = require('discord.js');
|
|
require('dotenv').config();
|
|
|
|
/**
|
|
* Sends a summary report of top AI-recommended items using a Discord Bot.
|
|
* @param {Array} items - List of items with "Hãy mua" in their ai_suggestion.
|
|
*/
|
|
async function sendDiscordSummary(items) {
|
|
const token = process.env.DISCORD_BOT_TOKEN;
|
|
const channelId = process.env.DISCORD_CHANNEL_ID;
|
|
|
|
if (!token || !channelId) {
|
|
console.log('Discord Bot Token or Channel ID not configured in .env. Skipping notification.');
|
|
return;
|
|
}
|
|
|
|
if (items.length === 0) {
|
|
console.log('No top deals found to send to Discord.');
|
|
return;
|
|
}
|
|
|
|
// Initialize Discord Client
|
|
const client = new Client({
|
|
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]
|
|
});
|
|
|
|
try {
|
|
// Login to Discord
|
|
await client.login(token);
|
|
|
|
// Fetch the target channel
|
|
const channel = await client.channels.fetch(channelId);
|
|
if (!channel || !channel.isTextBased()) {
|
|
console.error('Discord channel not found or is not a text channel.');
|
|
return;
|
|
}
|
|
|
|
// Sort and limit to top 10 deals
|
|
const topItems = items
|
|
.sort((a, b) => b.profit - a.profit)
|
|
.slice(0, 10);
|
|
|
|
const dashboardUrl = process.env.DASHBOARD_URL || 'https://logs1.danielvu.com/ebay-price-check';
|
|
|
|
// Build embeds
|
|
const embeds = topItems.map(item => {
|
|
const embed = new EmbedBuilder()
|
|
.setTitle(item.title)
|
|
.setURL(item.url)
|
|
.setDescription(`**AI Suggestion:** ${item.ai_suggestion}\n**Price:** $${item.price} | **Profit:** $${item.profit.toFixed(2)}`)
|
|
.setColor(0x22C55E); // Green
|
|
|
|
if (item.images && item.images.length > 0) {
|
|
embed.setThumbnail(item.images[0]);
|
|
}
|
|
|
|
embed.addFields(
|
|
{ name: 'Part Number', value: item.partNumber || 'N/A', inline: true },
|
|
{ name: 'Manufacturer', value: item.manufacturer || 'N/A', inline: true }
|
|
);
|
|
|
|
return embed;
|
|
});
|
|
|
|
// Send the message
|
|
await channel.send({
|
|
content: `🚀 **eBay Deep Scan: Found ${items.length} top deals!**\nView all results here: ${dashboardUrl}`,
|
|
embeds: embeds
|
|
});
|
|
|
|
console.log(`Successfully sent Discord Bot summary for ${topItems.length} items.`);
|
|
} catch (err) {
|
|
console.error('Error sending Discord Bot notification:', err.message);
|
|
} finally {
|
|
// Ensure client is destroyed after use
|
|
client.destroy();
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
sendDiscordSummary
|
|
};
|
|
|
|
// Standalone test block
|
|
if (require.main === module) {
|
|
const testItems = [
|
|
{
|
|
id: "test-123",
|
|
title: "Test RAM Module 16GB - DEBUG",
|
|
url: "https://www.ebay.com/itm/123456",
|
|
ai_suggestion: "Hãy mua ngay, đây là tin nhắn test từ hệ thống!",
|
|
price: 45.00,
|
|
profit: 15.00,
|
|
partNumber: "TEST-PN-001",
|
|
manufacturer: "TestBrand",
|
|
images: ["https://via.placeholder.com/150"]
|
|
}
|
|
];
|
|
|
|
console.log("Running Discord notification test...");
|
|
sendDiscordSummary(testItems).then(() => {
|
|
console.log("Test finished.");
|
|
}).catch(err => {
|
|
console.error("Test failed:", err);
|
|
});
|
|
}
|