plant that actually works
This commit is contained in:
		
							parent
							
								
									24bc79da43
								
							
						
					
					
						commit
						c9702dded7
					
				
					 1 changed files with 40 additions and 6 deletions
				
			
		| 
						 | 
					@ -49,17 +49,51 @@ utsuholights.callback = async function (msg, line, hex, bri) {
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
hf.registerCommand(utsuholights);
 | 
					hf.registerCommand(utsuholights);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const JPEG_HEADER = Buffer.from([
 | 
				
			||||||
 | 
					  0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46,
 | 
				
			||||||
 | 
					]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function fetchPlant() {
 | 
				
			||||||
 | 
					  const res = await fetch("https://nuclear.utsuho.rocks/plant/");
 | 
				
			||||||
 | 
					  const boundary = res.headers
 | 
				
			||||||
 | 
					    .get("Content-Type")
 | 
				
			||||||
 | 
					    .split(";")[1]
 | 
				
			||||||
 | 
					    .trim()
 | 
				
			||||||
 | 
					    .replace("boundary=", "--");
 | 
				
			||||||
 | 
					  let buffer = Buffer.alloc(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  let searchForNextBoundary = false;
 | 
				
			||||||
 | 
					  return await new Promise((resolve) => {
 | 
				
			||||||
 | 
					    res.body.on("data", function (data) {
 | 
				
			||||||
 | 
					      if (!searchForNextBoundary) {
 | 
				
			||||||
 | 
					        if (data.toString().startsWith(boundary)) {
 | 
				
			||||||
 | 
					          buffer = Buffer.concat([buffer, data]);
 | 
				
			||||||
 | 
					          searchForNextBoundary = true;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      } else if (searchForNextBoundary) {
 | 
				
			||||||
 | 
					        if (data.toString().startsWith(boundary)) {
 | 
				
			||||||
 | 
					          res.body.end();
 | 
				
			||||||
 | 
					          const length = Number(
 | 
				
			||||||
 | 
					            buffer.toString().match(/Content-Length:.*?(\d+)/)[1]
 | 
				
			||||||
 | 
					          );
 | 
				
			||||||
 | 
					          const headerOffset = buffer.indexOf(JPEG_HEADER);
 | 
				
			||||||
 | 
					          const data = buffer.slice(headerOffset, headerOffset + length);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          resolve(data);
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					          buffer = Buffer.concat([buffer, data]);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const plant = new Command("plant");
 | 
					const plant = new Command("plant");
 | 
				
			||||||
plant.category = CATEGORY;
 | 
					plant.category = CATEGORY;
 | 
				
			||||||
plant.helpText = "Plant cam";
 | 
					plant.helpText = "Plant cam";
 | 
				
			||||||
plant.callback = async function () {
 | 
					plant.callback = async function () {
 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
    const image = Buffer.from(
 | 
					    return {file: {file: await fetchPlant(), name: "plant.jpg"}};
 | 
				
			||||||
      await fetch("https://nuclear.utsuho.rocks/plant/").then((res) =>
 | 
					 | 
				
			||||||
        res.arrayBuffer()
 | 
					 | 
				
			||||||
      )
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
    return {file: {file: image, name: "plant.jpg"}};
 | 
					 | 
				
			||||||
  } catch (err) {
 | 
					  } catch (err) {
 | 
				
			||||||
    return "<:trollhollow:851301241417498704> where plant (Encountered an error getting plant cam)";
 | 
					    return "<:trollhollow:851301241417498704> where plant (Encountered an error getting plant cam)";
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue