You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 25, 2023. It is now read-only.
message.reply(`Hello there!\n\nI'm @redditlive_bot. I can fetch Reddit live threads and forward to you incoming messages, in order to keep you always updated.\n\nPlease use /follow to start following a thread. If any error occurs or you have a suggestion, you can query my owner @ALCC01 (https://albertocoscia.me).`)
18
+
}
19
+
});
20
+
21
+
bot.command('follow','Start following a thread',false,(args,message)=>{
22
+
if(args[0]!=''){
23
+
updateUser(message.chat,args[0]);
24
+
}else{
25
+
fetchThreads().then((threads)=>{
26
+
vartext='❓ Please select one of the current hot threads from /r/live\n\n';
}).catch(err=>user.sendMessage(`I was unable to parse the formatting of the resources from this thread, I\'m sending you an unparsed version!\n\n${thread.resources}`,{disable_web_page_preview: true}));
121
+
if(thread.online){
122
+
user.sendMessage('🔓 This thread is currently live, I\'ll start keeping you updated. Please use /unfollow if you don\'t want to receive any update from this thread');
123
+
if(!clients[id]){
124
+
createClient(id,thread);
125
+
}
126
+
}else{
127
+
user.sendMessage('🔒 Since this thread is offline at the moment I won\'t send you any update! :(');
128
+
clearId(id)
129
+
}
130
+
},err=>{
131
+
user.sendMessage('⛔️ An error occurred while fetching this thread, please try again later!')
132
+
})
133
+
}
134
+
135
+
functionparseThread(res){
136
+
varobj=res.body.data;
137
+
cache[obj.id]=obj;
138
+
obj.online=obj.state==='live';
139
+
returnobj;
140
+
}
141
+
142
+
functioncreateClient(id,thread){
143
+
varclient=newWebSocketClient();
144
+
client.on('connectionFailed',err=>{
145
+
console.log(err);
146
+
});
147
+
148
+
client.on('connect',connection=>{
149
+
console.log(`Connection established for thread ${id}`);
150
+
151
+
connection.on('error',err=>{
152
+
// We don't know for sure if the socket is still in a clean state, so we restart it
153
+
console.log(err);
154
+
console.log(`Restarting websocket for thread ${id} due to an error`);
155
+
createClient(id,thread)
156
+
});
157
+
158
+
connection.on('close',(code)=>{
159
+
if(code===1000){
160
+
console.log(`Websocket for thread ${id} was closed with code 1000`);
161
+
clearId(id);
162
+
return;
163
+
}
164
+
// This may be due to the address expiration
165
+
console.log(`Fetching a new websocket URL for thread ${id}`);
0 commit comments