File size: 4,396 Bytes
2821330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const axios = require('axios');

module.exports.config = {
    name: "regco",
    version: "1.0.1",
    hasPermssion: 0,
    credits: "Jonell Magallanes",
    description: "Recognize music",
    usePrefix: true,
    commandCategory: "Tool",
    usages: "Utility",
    cooldowns: 5,
};

module.exports.languages = {
    "vi": {
        "invalidFormat": "❌ Tin nhαΊ―n bαΊ‘n phαΊ£n hα»“i phαΊ£i lΓ  mα»™t audio, video, αΊ£nh nΓ o Δ‘Γ³"
    },
    "en": {
        "invalidFormat": "❌ You need to reply to a message containing audio or video"
    }
};

module.exports.run = async ({ api, event, getText }) => {
    if (event.type !== "message_reply") 
        return api.sendMessage(getText("invalidFormat"), event.threadID, event.messageID);

    if (!event.messageReply.attachments || event.messageReply.attachments.length === 0) 
        return api.sendMessage(getText("invalidFormat"), event.threadID, event.messageID);

    if (event.messageReply.attachments.length > 1) 
        return api.sendMessage(getText("invalidFormat"), event.threadID, event.messageID);

    const pro = await api.sendMessage("Uploading Attachment URL.....", event.threadID, event.messageID);
    const attachmentUrl = event.messageReply.attachments[0].url;
    const apiUrl = `https://ccprojectsapis.zetsu.xyz/api/recog?url=${attachmentUrl}`;

    const config = {
        headers: {
            'User-Agent': 'Mozilla/5.0'
        }
    };

    const retryLimit = 4;
    let attempt = 0;
    let responseData = null;

    const retryMessages = [
        "Retrying to recognize the music...",
        "Still trying to recognize the music...",
        "Last attempt to recognize the music...",
        "Final retry to recognize the music..."
    ];

    while (attempt < retryLimit) {
        try {
            if (attempt > 0) {
                api.editMessage(retryMessages[attempt - 1], pro.messageID, event.threadID, event.messageID);
            } else {
                api.editMessage("Recognizing music...", pro.messageID, event.threadID, event.messageID);
            }

            const response = await axios.get(apiUrl, config);
            responseData = response.data;

            if (responseData.status === "success" && responseData.result?.artist !== "Unknown") {
                api.editMessage("Completed.", pro.messageID, event.threadID, event.messageID);
                break;
            }

            attempt++;
            if (attempt >= retryLimit) {
                throw new Error("Failed to recognize music after multiple attempts.");
            }
            await new Promise(resolve => setTimeout(resolve, 2000));
        } catch (error) {
            console.error('Error:', error);
            if (attempt >= retryLimit - 1) {
                api.editMessage(`❌ Error: ${error.message}`, pro.messageID, event.threadID, event.messageID);
                return api.sendMessage(`❌ Error: ${error.message}`, event.threadID, event.messageID);
            }
            attempt++;
            await new Promise(resolve => setTimeout(resolve, 2000));
        }
    }

    const {
        artist = "Unknown",
        title = "Unknown",
        album = "Unknown",
        release_date = "Unknown",
        label = "Unknown",
        timecode = "Unknown",
        song_link = "Unknown",
        apple_music = {}
    } = responseData.result || {};

    const previewUrl = apple_music.previews?.[0]?.url || "No preview available";
    const artworkUrl = apple_music.artwork?.url
        ? apple_music.artwork.url.replace("{w}", "500").replace("{h}", "500")
        : "No artwork available";

    let messageContent = 
        `🎡 π— π˜‚π˜€π—Άπ—° π—₯π—²π—°π—Όπ—΄π—»π—Άπ˜π—Άπ—Όπ—» π—₯π—²π˜€π˜‚π—Ήπ˜\n━━━━━━━━━━━━━━━━━━\n` +
        `🎀 Artist: ${artist}\n` +
        `🎢 Title: ${title}\n` +
        `πŸ’Ώ Album: ${album}\n` +
        `πŸ“… Release Date: ${release_date}\n` +
        `🏷 Label: ${label}\n` +
        `⏳ Timecode: ${timecode}\n` +
        `πŸ”— Song Link: ${song_link}\n` +
        `🎧 Apple Music Preview: ${previewUrl}\n` +
        `πŸ–Ό Artwork: ${artworkUrl}`;

    api.unsendMessage(pro.messageID);
    return api.sendMessage(messageContent, event.threadID, event.messageID);
};