import asyncio, time, json, discordrpc, socket from websockets.server import serve def is_port_in_use(port: int) -> bool: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: return s.connect_ex(('localhost', port)) == 0 def main(): if(is_port_in_use(52121)): return rpc = discordrpc.RPC(app_id="863873014587326494") rpc.set_activity(details="Not doing.. anything..", large_image="small") global lastMessage; lastMessage = 0; async def echo(websocket): global lastMessage; async for message in websocket: data = json.loads(message); rpc.set_activity(state=f"Cursor position: {data["cursorPosition"]}", details=f"{data["name"]}", large_image="small") lastMessage = time.time() async def main2(): print("started ws") loop = asyncio.get_event_loop() async def heartbeatChecker(): if lastMessage != 0: if time.time()-lastMessage > 5: exit(0) loop.call_later(2, lambda: asyncio.ensure_future(heartbeatChecker())) await heartbeatChecker(); async with await serve(echo, "localhost", 52121): await asyncio.Future() asyncio.run(main2()) if __name__ == "__main__": main()