File size: 3,670 Bytes
07d561d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fb2d14
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
const storage = require('./storage');
const { EmbedBuilder } = require('discord.js');

/**
 * broadcast_system.js
 * Handles global announcements and message tracking
 */

async function broadcast(client, content, sender) {
    const broadcastId = Date.now().toString();
    const sentMessages = [];
    const timestamp = new Date().toISOString();

    const embed = new EmbedBuilder()
        .setTitle("📢 Global Announcement")
        .setDescription(content)
        .setColor('#5865F2')
        .setFooter({ text: `FriendlyBot Official | ID: ${broadcastId}` })
        .setTimestamp();

    for (const guild of client.guilds.cache.values()) {
        const config = storage.data.config[guild.id] || {};
        // Respect the opt-out toggle (defaulting to false based on user request)
        if (!!config.postEnabled) {
            try {
                // Determine target channel: either configured one or system channel
                const channelId = config.postChannelId;
                let channel = null;
                
                if (channelId) {
                    channel = await guild.channels.fetch(channelId).catch(() => null);
                }
                
                if (!channel) {
                    channel = guild.systemChannel || guild.channels.cache.find(c => c.isTextBased() && c.permissionsFor(guild.members.me).has('SendMessages'));
                }

                if (channel) {
                    const msg = await channel.send({ embeds: [embed] });
                    sentMessages.push({
                        guildId: guild.id,
                        guildName: guild.name,
                        channelId: channel.id,
                        messageId: msg.id
                    });
                }
            } catch (err) {
                console.error(`[Broadcast] Failed to send to ${guild.name}:`, err.message);
            }
        }
    }

    // Save to history
    storage.data.broadcasts.push({
        id: broadcastId,
        content: content,
        senderTag: sender.tag,
        senderId: sender.id,
        timestamp: timestamp,
        messages: sentMessages
    });
    storage.save('broadcasts');

    return { id: broadcastId, count: sentMessages.length };
}

async function removeBroadcast(client, broadcastIdOrText) {
    // Find the broadcast entry
    const entryIndex = storage.data.broadcasts.findIndex(b => b.id === broadcastIdOrText || b.content.includes(broadcastIdOrText));
    if (entryIndex === -1) return { success: false, message: "Broadcast not found in history." };

    const entry = storage.data.broadcasts[entryIndex];
    let removedCount = 0;

    for (const msgInfo of entry.messages) {
        try {
            const guild = client.guilds.cache.get(msgInfo.guildId);
            if (guild) {
                const channel = await guild.channels.fetch(msgInfo.channelId).catch(() => null);
                if (channel) {
                    const msg = await channel.messages.fetch(msgInfo.messageId).catch(() => null);
                    if (msg) {
                        await msg.delete();
                        removedCount++;
                    }
                }
            }
        } catch (err) {
            console.error(`[Broadcast] Failed to remove from guild ${msgInfo.guildId}:`, err.message);
        }
    }

    // Remove from history
    storage.data.broadcasts.splice(entryIndex, 1);
    storage.save('broadcasts');

    return { success: true, count: removedCount };
}

function getBroadcastHistory() {
    return storage.data.broadcasts;
}

module.exports = {
    broadcast,
    removeBroadcast,
    getBroadcastHistory
};