cleaned code
Signed-off-by: ale <ale@manalejandro.com>
This commit is contained in:
parent
b81b016887
commit
5742e1bafc
78
index.js
78
index.js
@ -1,5 +1,5 @@
|
||||
import { config } from 'dotenv';
|
||||
import { Client, Events, GatewayIntentBits, SlashCommandBuilder, EmbedBuilder, AttachmentBuilder } from 'discord.js';
|
||||
import { Client, Events, GatewayIntentBits, EmbedBuilder, AttachmentBuilder } from 'discord.js';
|
||||
import fetch from 'node-fetch';
|
||||
import * as cheerio from 'cheerio';
|
||||
import { createWriteStream, unlinkSync, existsSync } from 'fs';
|
||||
@ -66,7 +66,7 @@ client.on(Events.ClientReady, async readyClient => {
|
||||
},
|
||||
{
|
||||
name: 'search',
|
||||
description: 'Search for images or videos on Google',
|
||||
description: 'Search for images or videos online',
|
||||
options: [
|
||||
{
|
||||
name: 'query',
|
||||
@ -179,21 +179,6 @@ function getFileExtension(url) {
|
||||
return path.extname(urlPath).toLowerCase();
|
||||
}
|
||||
|
||||
function isImageFile(url) {
|
||||
const extension = getFileExtension(url);
|
||||
return ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp', '.svg'].includes(extension);
|
||||
}
|
||||
|
||||
function isVideoFile(url) {
|
||||
const extension = getFileExtension(url);
|
||||
return ['.mp4', '.webm', '.mov', '.avi', '.mkv', '.flv'].includes(extension);
|
||||
}
|
||||
|
||||
function isAudioFile(url) {
|
||||
const extension = getFileExtension(url);
|
||||
return ['.mp3', '.wav', '.ogg', '.m4a', '.aac', '.flac'].includes(extension);
|
||||
}
|
||||
|
||||
async function validateMultimediaUrl(url) {
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
@ -218,44 +203,6 @@ async function validateMultimediaUrl(url) {
|
||||
}
|
||||
}
|
||||
|
||||
// More lenient validation for search results
|
||||
async function validateImageUrl(url) {
|
||||
try {
|
||||
// First check if the URL looks like an image file
|
||||
const urlLower = url.toLowerCase();
|
||||
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp'];
|
||||
const hasImageExtension = imageExtensions.some(ext => urlLower.includes(ext));
|
||||
|
||||
// If it has an image extension or is from known image hosting sites, consider it valid
|
||||
const knownImageHosts = ['imgur.com', 'i.imgur.com', 'cdn.', 'images.', 'img.', 'static.', 'upload.wikimedia.org', 'picsum.photos', 'cataas.com', 'dog.ceo', 'images.dog.ceo', 'placeholder.com', 'dummyimage.com'];
|
||||
const isFromImageHost = knownImageHosts.some(host => urlLower.includes(host));
|
||||
|
||||
if (hasImageExtension || isFromImageHost) {
|
||||
return { valid: true, contentType: 'image/jpeg' };
|
||||
}
|
||||
|
||||
// For other URLs, try a quick GET request to follow redirects
|
||||
const response = await fetch(url, {
|
||||
method: 'GET',
|
||||
timeout: 3000,
|
||||
redirect: 'follow',
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
||||
}
|
||||
});
|
||||
|
||||
const contentType = response.headers.get('content-type') || '';
|
||||
|
||||
return {
|
||||
valid: response.ok && (contentType.startsWith('image/') || contentType === ''),
|
||||
contentType: contentType || 'image/jpeg'
|
||||
};
|
||||
} catch (error) {
|
||||
// If validation fails, assume it might be valid anyway for search results
|
||||
return { valid: true, contentType: 'image/jpeg', error: error.message };
|
||||
}
|
||||
}
|
||||
|
||||
// Real search function using Unsplash for high-quality full-sized images
|
||||
async function searchImages(query, limit = 10) {
|
||||
try {
|
||||
@ -295,27 +242,10 @@ async function searchImages(query, limit = 10) {
|
||||
});
|
||||
}
|
||||
} catch (unsplashError) {
|
||||
// Continue to fallback methods
|
||||
// Continue to DuckDuckGo fallback
|
||||
}
|
||||
|
||||
// Fallback 1: Try Pixabay-style search using Google Images API alternative
|
||||
if (searchResults.length < limit) {
|
||||
try {
|
||||
// Use Lorem Picsum for placeholder images with proper dimensions
|
||||
const remaining = limit - searchResults.length;
|
||||
for (let i = 0; i < remaining; i++) {
|
||||
const randomId = Math.floor(Math.random() * 1000) + 1;
|
||||
searchResults.push({
|
||||
title: `${query} - High Quality Image ${searchResults.length + 1}`,
|
||||
link: `https://picsum.photos/800/600?random=${randomId}&t=${Date.now()}`
|
||||
});
|
||||
}
|
||||
} catch (fallbackError) {
|
||||
// Continue to next fallback
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback 2: Use DuckDuckGo but try to get better image URLs
|
||||
// Fallback: Use DuckDuckGo for search results
|
||||
if (searchResults.length < limit) {
|
||||
try {
|
||||
const searchUrl = `https://api.duckduckgo.com/?q=${encodeURIComponent(query)}&format=json&no_html=1&skip_disambig=1&safe_search=strict`;
|
||||
|
Loading…
x
Reference in New Issue
Block a user