repl.it
@mat1/

repl reviver hypixel skyblock

Nodejs

No description

fork
loading
Files
  • index.js
  • Packager files
  • package-lock.json
  • package.json
index.js
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
// Repl Reviver made by mat

// CHANGE THIS STUFF
const replID = 'd5db2ae8-60fb-4579-81c3-214be96b9bad';
const replURL = 'https://skyblock.matdoes.dev';
const ownURL = 'https://repl-reviver-hypixel-skyblock.mat1.repl.co'

const apiKey = process.env.key


global.WebSocket = require("ws");

const fetch = require('node-fetch');
const crosis = require('@replit/crosis');
const express = require('express');

const app = express();



if (!apiKey) {
	console.log('HOW TO USE REPL REVIVER')
	console.log('1) fork the repl')
	console.log('2) create a file called .env (that exact name)')
	console.log('3) put key=YOURAPIKEY in .env')
	console.log('4) replace the replID variable with the id of your repl')
	console.log('5) replace the replURL variable with the repl.co url of your repl')
	console.log('6) replace the ownURL variable with the repl.co url of this repl (to keep it alive)')
	console.log('')
	console.log('How to get an API key: go to https://devs.turbio.repl.co/')
	console.log('How to get a repl id: go to repl.it/data/repls/@YOURUSERNAME/YOURREPLNAME and find the part where it says the id')
	process.exit()
}
console.log('starting')
var connected = false
var isUp = true

async function connect(timeout) {
	if (connected) return
	connected = true
	console.log('connecting')
	const client = new crosis.Client();

	const token = await getToken(replID, apiKey);
	await client.connect({ token });

	const packagerCh = client.openChannel({
		service: 'packager3'	
	});
	const interpreterCh = client.openChannel({
		service: 'interp2',
		name: 'interper'
	});

	packagerCh.on('command', async(command) => {
		if (command.state)
			console.log(`packager state ${command.state}`)
		if (command.ok) {
			console.log('packager ok, now running main')
			if (!isUp) {
				await interpreterCh.request({
					runMain: {}
				});
				isUp = true
			}
		}
	});
	interpreterCh.on('command', async(command) => {
	  console.log('interpreter', command)
		if (command.state === 1) {
			connected = false
			if (timeout)
				clearTimeout(timeout)
			console.log('back up!')
			client.close()
			.catch(() => {
				console.log('uhhhh failed to close')
			})
		}
		var output = command.output
			.replace('\u001b[2J', '[clear]')
		// process.stdout.write(output)
	});

  await packagerCh.request({
		packageInstall: {}
	});


	// 
}


function getToken(replId, apiKey) {
  return fetch(`https://repl.it/api/v0/repls/${replId}/token`, {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({ apiKey })
  }).then(res => res.json());
}

function connectWithTimeout() {
	isUp = false
	var assumeDeadTimeout = setTimeout(() => {
		console.log('assumed dead, cleared timeout')
		connected = false
	}, 160 * 1000); // 2 mins
	connect(assumeDeadTimeout)
}
function checkServerForever() {
	console.log('checking server forever')
	connect().catch(()=>{console.log('uh')})
	setInterval(async() => {
		fetch(ownURL).catch(()=>{})
		fetch(replURL)
		.catch(() => {
			console.log('disconnected!!!')
		})
		.then(res => {
			if (res.status == '502') {
				console.log('502 error')

				connectWithTimeout();
			} else if (res.status == '500') {
				console.log('500')
				connectWithTimeout()
			} else if (res.status == '524') {
				console.log('524')
				connectWithTimeout()
			} else {
				isUp = true
				console.log('good', res.status)
			}
		})

	}, 5000)
}

checkServerForever()

app.get('/', (req, res) => {
  res.send('Hello world!')
});

app.listen(3000, () => {
  console.log('server started');
});
?