forked from steveseguin/ssn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_actions.js
More file actions
230 lines (199 loc) · 7.66 KB
/
custom_actions.js
File metadata and controls
230 lines (199 loc) · 7.66 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Custom User Function Template for SocialStream.ninja
// Override the default customUserFunction with your own custom processing logic
// This is to be uploaded via the menu.
window.customUserFunction = function(data) {
// Log incoming data for debugging (remove in production)
console.log("Custom function processing data:", data);
// SECTION 1: MESSAGE SOURCE FILTERING
// Process messages differently based on their source
if (data.type === "twitch") {
// Special handling for Twitch messages
if (data.chatmessage && data.chatmessage.toLowerCase().includes("hello stream")) {
// Automatically respond to greetings from Twitch
sendCustomReply(data, "Hey there, welcome to the stream!");
return data; // handled specially
}
} else if (data.type === "youtube") {
// Handle YouTube messages differently
if (data.chatmessage && data.chatmessage.toLowerCase().includes("new subscriber")) {
// Highlight new subscriber messages
data.highlightColor = "#ff9966";
}
}
// SECTION 2: CUSTOM COMMAND HANDLING
if (data.chatmessage && data.chatmessage.startsWith("!")) {
// Handle custom commands
const commandParts = data.chatmessage.split(" ");
const command = commandParts[0].toLowerCase();
switch (command) {
case "!hello":
sendCustomReply(data, `Hello, @${data.chatname}!`);
return data;
case "!time":
sendCustomReply(data, `Current time is ${new Date().toLocaleTimeString()}`);
return data;
case "!shoutout":
if (data.mod || data.admin) { // Only mods/admins can use this
const username = commandParts[1];
if (username) {
sendCustomReply(data, `Check out @${username} at https://twitch.tv/${username}`);
}
}
return data;
}
}
// SECTION 3: MESSAGE FILTERING/BLOCKING
// Block messages with specific patterns
if (data.chatmessage) {
// Block messages with too many capital letters (shouting)
const uppercase = data.chatmessage.replace(/[^A-Z]/g, "").length;
const totalChars = data.chatmessage.replace(/\s/g, "").length;
if (totalChars > 10 && uppercase / totalChars > 0.7) {
console.log("Blocking message with excessive caps");
return false; // Block the message
}
// Block messages with specific words (in addition to the built-in blocklist)
const customBadWords = ["badword1", "badword2", "badword3"];
if (customBadWords.some(word => data.chatmessage.toLowerCase().includes(word))) {
console.log("Blocking message with banned words");
return false; // Block the message
}
}
// SECTION 4: CUSTOM USER RECOGNITION
// Special handling for regular viewers/supporters
if (data.chatname) {
const regulars = ["regular1", "regular2", "supporter1"];
const vips = ["vip1", "vip2", "vip3"];
if (regulars.includes(data.chatname.toLowerCase())) {
// Add special styling for regular viewers
data.backgroundNameColor = "background-color: #3498db;";
data.textNameColor = "color: #ffffff;";
} else if (vips.includes(data.chatname.toLowerCase())) {
// Add VIP styling
data.backgroundNameColor = "background-color: #9b59b6;";
data.textNameColor = "color: #ffffff;";
// Add a crown emoji before VIP names
data.chatname = "👑 " + data.chatname;
}
}
// SECTION 5: CUSTOM CONTENT ENHANCEMENT
// Replace keywords with richer content
if (data.chatmessage) {
// Replace emotion keywords with emojis
const emotionMap = {
":smile:": "😊",
":laugh:": "😂",
":sad:": "😢",
":heart:": "❤️"
};
Object.keys(emotionMap).forEach(keyword => {
data.chatmessage = data.chatmessage.replace(
new RegExp(keyword, "g"),
emotionMap[keyword]
);
});
// Highlight specific product mentions
if (data.chatmessage.toLowerCase().includes("product123")) {
data.chatmessage = data.chatmessage.replace(
/product123/gi,
"<span style='color:#ff0000;font-weight:bold;'>Product123™</span>"
);
data.textonly = false;
}
}
// SECTION 6: EVENT TRACKING
// Track messages containing specific keywords and trigger actions
if (data.chatmessage) {
// Track questions for later follow-up
const messageText = data.textContent || data.chatmessage;
if (messageText.includes("?") && !data.bot) {
// You could store these questions in a global array
if (!window.pendingQuestions) window.pendingQuestions = [];
window.pendingQuestions.push({
name: data.chatname,
question: data.chatmessage,
time: new Date()
});
// If using as host, you could get a notification
if (data.host) {
console.log("New question from viewer:", data.chatmessage);
}
}
}
// SECTION 7: INTEGRATION WITH EXTERNAL APIS
// Example of how you could integrate with external services
if (data.hasDonation && parseFloat(data.hasDonation) >= 10) {
// Track large donations in a database or send to webhook
const donationData = {
username: data.chatname,
amount: data.hasDonation,
message: data.chatmessage,
platform: data.type,
timestamp: new Date().toISOString()
};
// Example webhook call (commented out)
/*
fetch('https://your-webhook-url.com/donations', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(donationData)
}).catch(err => console.error('Failed to log donation:', err));
*/
// Thank big donors
sendCustomReply(data, `Wow! Thank you so much for the ${data.hasDonation} donation, @${data.chatname}!`);
}
// Return data to allow normal processing to continue
return data;
};
// Helper function to send replies
function sendCustomReply(data, message) {
const msg = {};
if (data.tid) {
msg.tid = data.tid;
}
msg.response = message;
sendMessageToTabs(msg, false, null, false, false, 0);
}
// Helper function to check regex patterns against messages
function matchesPattern(text, pattern) {
return new RegExp(pattern, "i").test(text);
}
// Helper function to format time
function formatTimeAgo(date) {
const seconds = Math.floor((new Date() - date) / 1000);
if (seconds < 60) return `${seconds}s ago`;
const minutes = Math.floor(seconds / 60);
if (minutes < 60) return `${minutes}m ago`;
const hours = Math.floor(minutes / 60);
if (hours < 24) return `${hours}h ago`;
const days = Math.floor(hours / 24);
return `${days}d ago`;
}
// Example of a more complex response system using a queue
// This avoids flooding chat with too many responses
window.responseQueue = [];
window.processingQueue = false;
function queueResponse(message, delay = 3000) {
window.responseQueue.push({ message, timestamp: Date.now() + delay });
if (!window.processingQueue) {
window.processingQueue = true;
processResponseQueue();
}
}
function processResponseQueue() {
if (window.responseQueue.length === 0) {
window.processingQueue = false;
return;
}
const now = Date.now();
const nextItem = window.responseQueue[0];
if (now >= nextItem.timestamp) {
window.responseQueue.shift();
const msg = {};
msg.response = nextItem.message;
sendMessageToTabs(msg, false, null, false, false, 0);
setTimeout(processResponseQueue, 1000); // Process next item in 1 second
} else {
setTimeout(processResponseQueue, 500); // Check again in 500ms
}
}