Brozy123 commited on
Commit
07d561d
·
verified ·
1 Parent(s): c24afb6

Create broadcast_system

Browse files
Files changed (1) hide show
  1. broadcast_system +108 -0
broadcast_system ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const storage = require('./storage');
2
+ const { EmbedBuilder } = require('discord.js');
3
+
4
+ /**
5
+ * broadcast_system.js
6
+ * Handles global announcements and message tracking
7
+ */
8
+
9
+ async function broadcast(client, content, sender) {
10
+ const broadcastId = Date.now().toString();
11
+ const sentMessages = [];
12
+ const timestamp = new Date().toISOString();
13
+
14
+ const embed = new EmbedBuilder()
15
+ .setTitle("📢 Global Announcement")
16
+ .setDescription(content)
17
+ .setColor('#5865F2')
18
+ .setFooter({ text: `FriendlyBot Official | ID: ${broadcastId}` })
19
+ .setTimestamp();
20
+
21
+ for (const guild of client.guilds.cache.values()) {
22
+ const config = storage.data.config[guild.id] || {};
23
+ // Respect the opt-out toggle (defaulting to false based on user request)
24
+ if (!!config.postEnabled) {
25
+ try {
26
+ // Determine target channel: either configured one or system channel
27
+ const channelId = config.postChannelId;
28
+ let channel = null;
29
+
30
+ if (channelId) {
31
+ channel = await guild.channels.fetch(channelId).catch(() => null);
32
+ }
33
+
34
+ if (!channel) {
35
+ channel = guild.systemChannel || guild.channels.cache.find(c => c.isTextBased() && c.permissionsFor(guild.members.me).has('SendMessages'));
36
+ }
37
+
38
+ if (channel) {
39
+ const msg = await channel.send({ embeds: [embed] });
40
+ sentMessages.push({
41
+ guildId: guild.id,
42
+ guildName: guild.name,
43
+ channelId: channel.id,
44
+ messageId: msg.id
45
+ });
46
+ }
47
+ } catch (err) {
48
+ console.error(`[Broadcast] Failed to send to ${guild.name}:`, err.message);
49
+ }
50
+ }
51
+ }
52
+
53
+ // Save to history
54
+ storage.data.broadcasts.push({
55
+ id: broadcastId,
56
+ content: content,
57
+ senderTag: sender.tag,
58
+ senderId: sender.id,
59
+ timestamp: timestamp,
60
+ messages: sentMessages
61
+ });
62
+ storage.save('broadcasts');
63
+
64
+ return { id: broadcastId, count: sentMessages.length };
65
+ }
66
+
67
+ async function removeBroadcast(client, broadcastIdOrText) {
68
+ // Find the broadcast entry
69
+ const entryIndex = storage.data.broadcasts.findIndex(b => b.id === broadcastIdOrText || b.content.includes(broadcastIdOrText));
70
+ if (entryIndex === -1) return { success: false, message: "Broadcast not found in history." };
71
+
72
+ const entry = storage.data.broadcasts[entryIndex];
73
+ let removedCount = 0;
74
+
75
+ for (const msgInfo of entry.messages) {
76
+ try {
77
+ const guild = client.guilds.cache.get(msgInfo.guildId);
78
+ if (guild) {
79
+ const channel = await guild.channels.fetch(msgInfo.channelId).catch(() => null);
80
+ if (channel) {
81
+ const msg = await channel.messages.fetch(msgInfo.messageId).catch(() => null);
82
+ if (msg) {
83
+ await msg.delete();
84
+ removedCount++;
85
+ }
86
+ }
87
+ }
88
+ } catch (err) {
89
+ console.error(`[Broadcast] Failed to remove from guild ${msgInfo.guildId}:`, err.message);
90
+ }
91
+ }
92
+
93
+ // Remove from history
94
+ storage.data.broadcasts.splice(entryIndex, 1);
95
+ storage.save('broadcasts');
96
+
97
+ return { success: true, count: removedCount };
98
+ }
99
+
100
+ function getBroadcastHistory() {
101
+ return storage.data.broadcasts;
102
+ }
103
+
104
+ module.exports = {
105
+ broadcast,
106
+ removeBroadcast,
107
+ getBroadcastHistory
108
+ };